In: Computer Science
Program in Bash:
Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line.
Do not use the sort function.
RAW CODE ### WITH COMMENTS ###
NUMBERS=( $(<file.txt) ) ### Reading unique numbers from file i.e. non-repeating
for ((i = 0; i<${#NUMBERS[@]}; i++)); do ### Here {#ARR[@]} Will return the length of array
for ((j = 0; j<${#NUMBERS[@]}-i-1; j++)); do
if [ ${NUMBERS[j]} -gt ${NUMBERS[$((j+1))]} ]; then
dummy=${NUMBERS[j]} ## SWAPPING IF NUMBER AT X IS GREATER THAN (-gt) X+1.
NUMBERS[$j]=${NUMBERS[$((j+1))]}
NUMBERS[$((j+1))]=$dummy
fi
done
done
printf "%s\n" "${NUMBERS[@]}" > file_sorted.txt | uniq ### STORING SORTED VALUES IN FILE
Unique=( $(uniq <file_sorted.txt) )
rm file_sorted.txt ## Removing file, you can keep too, just remove this line for keeping.
echo ${Unique[*]} ## PRINTING UNIQUE VALUES
SCREENSHOTS (CODE, TEXT FILE AND OUTPUT)
CODE
TEXT FILE
OUTPUT
##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####