Question

In: Computer Science

Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the...

Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the files from two named directories into a new third directory. Timestamps must be preserved while copying files from one directory into another.

Task Requirements

  1. Three directory names must be supplied as arguments to your script when it is executed by the user.

  2. In the beginning of your script you need to check that the first two directory names provided (e.g. dir1 and dir2) exist under the current directory. If they do not exist, your script must display an error message and then it should exit.

  3. The third directory argument to the script will specify a new directory name (for example, dir3). This directory must not currently exist (i.e. the creation of e.g. dir3 should be one of the actions of your script). If the directory already exists, your script should display an error message and exit.

  4. The script will first copy all of the files from dir1 into dir3. Your script output must indicate which files have been copied.

  5. The script will then copy every file from dir2 to dir3 subject to the following conditions:

    1. only copy if the file from dir2 is not already present in dir3; or

    2. only copy if the file from dir2 is newer than the same file in dir3. The newer file

      will overwrite the existing version in dir3.

    Your script output must indicate which files have been copied.

  6. Clean up - remove all temporary files created (if any) at the end of your script.

  7. Your script must generate output similar to the example output shown below.

Solutions

Expert Solution

Below is the shell script code. You can save it to a file with name cp2.sh. I have written the script code to cover all the requirements you have mentioned. I have added the comments as well to demonstrate that what the particular code will be doing.

# Setup input arguements into the variables
DIR1=$0
DIR2=$1
DIR3=$2
DIR4=$PWD
# Check if directory are sub-directories of current directory
if [[ ! "$DIR4" == "$DIR1"* ]]; then
  echo "DIR1 does not exist under the current directory. Exiting the script"
  exit 0
elif [[ ! "$DIR4" == "$DIR2"* ]]; then
        echo "DIR2 does not exist under the current directory. Exiting the script"
fi
# Check if directory already exists else create the directory
if [ -d $DIR3 ] 
then
echo "Directory already exists. Exiting the script"
exit 0
else 
mkdir $DIR3
fi
#Copy all files from DIR1 to DIR3
cp -p -v $DIR1/* $DIR3
#Copy all files from DIR2 to DIR3
cp -p -u -v $DIR2/* $DIR3
exit 0

Hope it helps.


Related Solutions

LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt...
LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt the user to "Enter a number between 1 and 10". (Hint: Use the 'echo' and 'read' commands in the script. See the slide about the 'read' command) If the number is less than 5, print "The number is less than 5" (Hint: You will read input into a variable; e.g. read NUM. In the IF statement, enclose $NUM in quotes; e.g. "$NUM". Also, remember...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all the files in a directory. b. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file? c. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
Dominion Consulting in Sydney needs a shell script (to run on the Bourne shell) to maintain...
Dominion Consulting in Sydney needs a shell script (to run on the Bourne shell) to maintain its employee records file, which contains the following information (fields) about each employee: telephone number (8 digits, first digit non-zero); family name (alphabetic characters); first name (alphabetic characters); department number (2 digits) and job title (alphabetic characters). This script should let users add, delete, search for, and display specific employee information. Task Requirements Create a text file named records containing the following records with...
Design a shell program to remove all the shell programming files ending with sh on your...
Design a shell program to remove all the shell programming files ending with sh on your home directory when a SIGINT signal is received.
Please write a shell script called "myfilechecking" to determine whether a file belongs to one of...
Please write a shell script called "myfilechecking" to determine whether a file belongs to one of the following categories: A directory; A scrip file (readable and executable); An executable file (not readable but executable, such as a.out); A regular ascii file (only readable, such as a C source code file); The file cannot be recognized. Your script should display the corresponding information based on the category of the file; The file name should be provided along with your script invocation,...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell script to take a directory as an argument and display the contents of that directory (10pts) Write a shell script that takes any command as a parameter and displays help on that command (e.g. the result of the execution of man <command>). (10pts) Write a shell script that requires two file names as parameters and copies content of one file into another without asking...
Write a complete shell script that first asks the user to enter a URL. The script...
Write a complete shell script that first asks the user to enter a URL. The script should read the URL into a variable named url. The shell script should then retrieve the file associated with the URL by using the curl command. The output of the curl command should be redirected to a file named html_file. The shell script should then use the grep command to search the file named html_file for the word manhattan. Finally, the shell script should...
Create a shell script called (Main.sh). The script must call the following scripts (FileS.sh), (Work.sh), (Ether.sh),...
Create a shell script called (Main.sh). The script must call the following scripts (FileS.sh), (Work.sh), (Ether.sh), (Arch.sh) and (Buzz.sh). The first script Main.sh must have two subroutines. The First Subroutine will display the following messages on the screen: 1-Display User login name, date and time. 2-Display System boot time. 3-Working path and Shell type 4-Display Home directory and number of files and directories in your Home. 5-Message describing briefly the requited tasks.
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
Perl is a programming language that can be used on Linux. Write a Perl shell script...
Perl is a programming language that can be used on Linux. Write a Perl shell script named phone.pl that prompts the user to enter first or last or any portion of person’s name, so that can be found the appropriate entry in the phone directory file called “phones”. If the user tries to enter name as the argument on the command line, he/she will get a warning message “You need to provide name when prompted by this script!” If the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT