Question

In: Computer Science

Program in Bash: Write a program using bash script that can read a file from the...

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.

Solutions

Expert Solution

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. #####


Related Solutions

Program in Bash: Write a program using bash script that can read a file from the...
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.
I am attempting to write a script using bash on Linux. My program must save the...
I am attempting to write a script using bash on Linux. My program must save the long list format of the parent directory to a text file. Then, it must print how many items are in this parent directory. Then, it must sort the file in reverse order and save it to a different text file. I understand that the parent directory is accessed using cd .., and I understand that a file can be written to by echoing and...
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on...
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on the command line 2. For each file name provided, delete any line that contains the string: qwe.rty When the changes are completed, the script should display the total number of files scanned, and the total number of files changed. Example Command: assessment-script-a file.a file.b file.c file.d file.e    Example Output: 5 files scanned, 3 files changed 3. Your script should include a series of...
Write a bash script that will allow you to: Read in your Your name Course name...
Write a bash script that will allow you to: Read in your Your name Course name and Instructor’s name Then prompt the user to enter two numbers and determine which number is greater. Ex: If 10 is entered for the first number and 3 for the second, you should output Your name Course name Instructor’s name 10 is greater than 3. If 33 is entered for the first number and 100 for the second, you shou output Your name Course...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask...
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask the user to input the number of kilometers they need to travel. - Display the number of equivalent miles for the number. Use formula, Kilometers = miles/0.62137 - Every time the loop runs, it should ask the user if they want to continue, if user enters “Y” or “y”, then the loop runs again, if user enters “N” or “n”, then stop the loop...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
Use Vi text editor or ATOM to create a bash script file. Use the file name...
Use Vi text editor or ATOM to create a bash script file. Use the file name ayaan.sh. The script when ran it will execute the following commands all at once as script. Test your script file make sure it works. Here is the list of actions the script will do: It will create the following directory structure data2/new2/mondaynews off your home directory. inside the mondaynews, create 4 files sports.txt, baseball.txt, file1.txt and file2.txt. Make sure file1.txt and file2.txt are hidden...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT