Question

In: Computer Science

Examine the following shell script and describe its function line-by-line: #!/bin/bash echo -e "Which file would...

Examine the following shell script and describe its function line-by-line:

#!/bin/bash

echo -e "Which file would you like to copy> --> \c"

echo -e "Which file would you like to copy? --> \c?"

read FILENAME

mkdir /stuff || echo "The /stuff directory could not be created." && echo "The /stuff directory could not be created."

cp -f $FILENAME /stuff && echo "$FILENAME was successfully copied to /stuff"

Solutions

Expert Solution

echo -e "Which file would you like to copy> --> \c"

This line prints out Which file would you like to copy> -->.

The \c is used to supress new line character so that the next echo statement also prints on same line.

The next line echo -e "Which file would you like to copy? --> \c?" also echos-

Which file would you like to copy? -->  

Again, the \c is used to supress newline, so that the next statement will start in the same line.

read FILENAME

this line takes input , and stores the input in the variable called FILENAME.

The next line-

mkdir /stuff || echo "The /stuff directory could not be created." && echo "The /stuff directory could not be created."

This first tries to make the directory stuff in the home directory (mkdir command is used to create directories).

The || is used for piping, that is if the first statement (mkdir /stuff) returns false i.e if the mkdir command fails, the next command is executed, where the line echo "The /stuff directory could not be created"

will be executed twice due to the presence of &&.

mkdir can fail if the shell script does not have appropriate permissions.

The last line tries to copy the file stored in the FILENAME variable to the stuff directory which has been created previously. If the copy successfully passes, only then do we move on to the next statement after && which is

echo "$FILENAME was successfully copied to /stuff".

If for any reason we have provided a FILENAME which does not exist or the /stuff directory was not created , the error will be printed and the next echo statement will not be executed as the first statement returns false.

So all in all the script asks us to enter a FILENAME, and tries to copy the file to the /stuff directory.


Related Solutions

Examine the following shell script and describe its function line-by-line: #! /bin/bash #This script backs up...
Examine the following shell script and describe its function line-by-line: #! /bin/bash #This script backs up the Oracle DB rm -f /SAN/backup-oracle* if tar -zcvf /SAN/backup-oracle- 'date +%F'.tar.gz/oracledb/* then echo "Oracle backup completed on 'date'" >>/var/log/oraclelog else echo "Oracle backup failed on 'date'" >>/var/log/oraclelog mail -s ALERT [email protected] </var/log/oraclelog fi
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.
Please answer the following using bash shell. 1. The students.txt file consists of rows of students...
Please answer the following using bash shell. 1. The students.txt file consists of rows of students where each row contains a student’s first name, last name, major, and 5 test scores. Write a script that uses a while read statement to input all of the students, line by line, computes the average test score and determines the letter grade for that student. Of the 5 test scores, the 5th test is worth double so that you add each test score...
Create shell scripts, each in its own .sh file. Please include a shebang line at the...
Create shell scripts, each in its own .sh file. Please include a shebang line at the top of the script as well as appropriate comments through out. Write a script that reads sunspot data from data-shell/data/sunspot.txt and uses awk to print a list of the total number of sunspots for each year on record as well as the twelve monthly averages across the record. Note: "awk allows you to index arrays using strings, so as you traverse the data file,...
Linux Question: Describe how you created above file and its contents in VI. a) Bash/VI commands:...
Linux Question: Describe how you created above file and its contents in VI. a) Bash/VI commands: 20pts; b) Non-brute-force solution: 10pts). Note: Brute-force solution means that entering characters one by one Install JDK 1.8 by the following command: $ sudo dnf –y install java-1.8.0-openjdk-devel.x86_64 Use VI to create a file “YourFirstNameHomework.java” (e.g., “MollyHomework.java”) and add the following contents into it. (grading details: file name format:10 pts, a screenshot of the file content in the VI environment: 10pts, paragraphs: 10pts, empty...
Describe in words what the function of each line is in the following SQL query The...
Describe in words what the function of each line is in the following SQL query The lyrics database is provided under question 3 for context 1. select studioID, studioname, base from salespeople sa inner join studios st on (sa.salesID = st.salesid) where base < 300 2. SELECT artistName FROM Artists WHERE artistID IN (SELECT artistID FROM Titles) 3. select m.lastname, m.firstname, s.lastname         from members m inner join salespeople s using (salesID)         order by m.lastname asc; The lyrics database...
1. Describe the gene and protein E. coli beta-glucuronidase. What is its biological function? 2. Describe...
1. Describe the gene and protein E. coli beta-glucuronidase. What is its biological function? 2. Describe the pET28a plasmid.  
Define a function file_to_hist() which takes a string representing a filename, opens the file, reads its...
Define a function file_to_hist() which takes a string representing a filename, opens the file, reads its contents, closes the file,* and returns a histogram based on the letter frequencies in the given file. If no such file exists, your function should return an empty histogram (i.e., an empty dictionary {}). So for example, if the file nash.txt was in the same directory as char_hist3.py and had the following contents: I have never seen a purple cow, And I never hope...
Which of the following statements about the tryptophan (trp) operon does not properly describe its function?...
Which of the following statements about the tryptophan (trp) operon does not properly describe its function? Select one: a. The trp operon is activated in the cell when tryptophan levels rise        b. The enzymes expressed by the trp operon allow the cell to make tryptophan when cellular levels are low      c. The trp operon is activated in the cell when tryptophan levels fall        d. The trp repressor remains bound to the operon when allosterically bound by tryptophan in the...
IN PYTHON File Data --- In file1.txt add the following numbers, each on its own line...
IN PYTHON File Data --- In file1.txt add the following numbers, each on its own line (20, 30, 40, 50, 60). Do not add data to file2.txt. Write a program. Create a new .py file that reads in the data from file1 and adds all together. Then output the sum to file2.txt. Add your name to the first line in file2.txt (see sample output) Sample Output Your Name 200 use a main function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT