Question

In: Computer Science

UNIX/TERMINAL ASSIGNMENT - DIFFICULT Could you please type line by line what I should be entering...

UNIX/TERMINAL ASSIGNMENT - DIFFICULT

Could you please type line by line what I should be entering in the terminal command line to complete the assignment as instructed?

INSTRUCTIONS AS FOLLOWS:

In this lab you will create a script to backup certain number of oldest or newest files in the current directory into a compressed archived file. The objective of this lab is to familiarize and gain proficiency with the following:

• Use of command line arguments

• Use of conditional statement

• Use of for loops

You need to write a script named backup.sh which will receive 3 command line arguments. The arguments are:


• The 1st argument is the string “old” or “new” indicating whether the oldest files or the newest files needs to be backed up

• The 2nd argument is an integer indicating how many files needs to be backed up.

• The 3rd argument is the name of archive file (tar file).

Your script should perform the following functions

• Check whether 3 arguments have been passed or not.

• If three arguments have not been passed display appropriate message and exit.

• Check whether the 1st argument is not the string “old” or “new” display appropriate message and exit.

• Create a temporary directory under current directory • Using a “for loop” copy all the required files in the temporary directory.

• Create an archive file of the copied files. The name of the archive files is the 3rd argument. Compress the archived file.

• Delete the temporary directory along with copied files.

Example execution: If user type the following ./backup.sh old 5 backup.tar The script will create a compressed archive file named backup.tar.gz in the current directory containing five oldest file in the current directory. ./backup.sh new 4 backup.tar The script will create a compressed archive file named backup.tar.gz in the current directory containing four newest t file in the current directory

How would I create a script named backup.sh and do the 3 arguments?

Have some Python and C++ experience, but Unix in Terminal is completely foreign to me so as much detail as possible would be appreciated.

Thanks!

Solutions

Expert Solution

Please find the below script, let me know if you face any issues

$ gedit backup.sh or vi $ backup.sh

then copy paste the below script save and execute it as ./backup.sh old 5 backup.tar from the terminal

like mentioned in the question

  1. the validation is handled for the input arguments
  2. tmp directory is created
  3. for loop is used to copy the files to tmp directory -- though for loop is not needed
  4. compress the archive using tar command
  5. delete the tmp directory finally
#!/bin/bash
if [ -z "$1" ]
then
    echo 'Input new or old files! ex. ./backup.sh old 5 backup.tar'
    exit 0
fi

if [ -z "$2" ]
then
    echo 'please Input number of files to backup ! ex. ./backup.sh old 5 backup.tar'
    exit 0
fi
if [ -z "$3" ]
then
    echo 'please Input name of file to backup ! ex. ./backup.sh old 5 backup.tar'
    exit 0
fi

mkdir ../tmp

if [ "new" == $1 ]
then
    for f in $(ls -tr | tail -"$2") ; do cp -r $f ../tmp ; done
        
else
        for f in $(ls -t | tail -"$2") ; do cp -r $f ../tmp ; done
fi

tar -cpzf $3 . ../tmp
rm -rf ../tmp

tar -C tmp -zxvf backup.tar commande used to untar the backup file


Related Solutions

UNIX ONLY -- DIFFICULT LAB ASSIGNMENT This is an extremely difficult subject for me. Could you...
UNIX ONLY -- DIFFICULT LAB ASSIGNMENT This is an extremely difficult subject for me. Could you please give step by step instructions on how to write a script called "encrypt_password.sh" and say line by line what the code should be to complete the assignment according to the instructions and examples below? The assignment will be done entirely in Terminal on Mac Our Lab assignment is as follows: In this lab, you will write a script called encrypt_password.sh to encrypt passwords....
(Please TYPE. Handwriting on here has been difficult to decipher, thanks) Could someone please give examples...
(Please TYPE. Handwriting on here has been difficult to decipher, thanks) Could someone please give examples of the below? Thanks! Question: What factors/circumstances/conditions could prevent the world from operating as described in the Median Voter Theorem?
Could you please answer this question? which type of farms should be assisted by government policy...
Could you please answer this question? which type of farms should be assisted by government policy and why? How they are different?
Please provide step by step detailing on how i should do this assignment. In Angel, you...
Please provide step by step detailing on how i should do this assignment. In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary...
What could you do if you wanted to reduce your chances of Type I Error?
What could you do if you wanted to reduce your chances of Type I Error?
Type I and Type II Errors . Please discuss Type I and Type II errors. What...
Type I and Type II Errors . Please discuss Type I and Type II errors. What are they? Discuss their relationship with hypothesis testing. Answer all parts of question!!! Do not plagiarize!! Write out the answer on here, don't post a picture of it! Answer must be long!
What type of statistical test should I use for the following? Where relevant, please indicate if...
What type of statistical test should I use for the following? Where relevant, please indicate if I should use a one or a two sided test. Please don’t do the tests themselves! (2 points each) A researcher has collected head width and body weight for 45 ants. She wants to know whether the two variables have a linear relationship. You want to know whether Biology majors or P.E. Majors run faster. You get a group of 20 Biology and 20...
What Statistical Test Should I Use Please indicate the type of test needed to analyze the...
What Statistical Test Should I Use Please indicate the type of test needed to analyze the data described. Hints: Think about the IV and DV. How many levels of the IV? Are they repeated measures? Are the data categorical? a. z-test b. One-Sample t-Test c. Dependent Measures t-test d. Independent Measures t-test e. One-way ANOVA f. Post-hoc tests g. Between-Subjects Factorial ANOVA h. Chi-Square test of Independence _________ A researcher is interested in finding out if the color of the...
I need a step by step for entering in excel. For this assignment, your group will...
I need a step by step for entering in excel. For this assignment, your group will utilize the preliminary data collected in the Topic 2 assignment. Considering the specific requirements of your scenario, complete the following steps using Excel. The accuracy of formulas and calculations will be assessed. Select the appropriate discrete probability distribution. If using a binomial distribution, use the constant probability from the collected data and assume a fixed number of events of 20. If using a Poisson...
Could you please explain to me when I should use the two proportion tests? A two...
Could you please explain to me when I should use the two proportion tests? A two proportion f test tests variance/standard deviation. So if it is testing how much the two proportions vary then isn't it doing what a linear regression t test does? Whats the difference between a linear regression t test and goodness of fit test statistic? Is there such thing as a two proportion chi squared test or is that just an f test? The only difference...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT