Question

In: Computer Science

Part 2 – Scripting Goals:  Write a bash script  Use linux shell commands within...

Part 2 – Scripting
Goals:
 Write a bash script
 Use linux shell commands within your script
 Provide user feedback
 Use loops
 Use conditionals
Remember to use chmod +x to make your file executable!
Each script is 5 points in the Specifications portion of the rubric. Don’t forget to maintain good standards and comments.
Script 1 – Echo-back some information
Write a script name hello.sh that will take the user’s first name as a command line argument and say hello!
Use Case: ./hello.sh Bob
Output: Hello Bob, I am a BASH script!
Script 2 – Make a Backup Folder
Write a short script named bakThatUp.sh this script will make a backup of a folder given through the command line. This script should take in two parameters, but have multiple options use If-Statements to make it work.
Use Cases:
./bakThatUp.sh Archive
o Creates a backup folder named Archive_bak with all contents
./backThatUp.sh –t Archive
o Creates a backup folder named with a date stamp (using +%F) and the name:
 2018-08-04_Archive
 date +%F gives the appropriate date stamp
./backThatUp.sh Archive –t
o Same result as above
./backThatUp.sh Archive ArchiveBackUpFolder
o Creates a backup folder named ArchiveBackUpFolder with all contents

Solutions

Expert Solution

Script 1. Open a text editor such as gedit, write the following contents and save the file as hello.sh.

       #!/bin/bash

       echo " Hello $1, I am a BASH script! "  

    

It is a simple shell script that uses the echo command to print a message along with the arguments given in the terminal.

So, now perform chmod +x hello.sh to make the hello.sh file as executable file.

Then Run the hello.sh file using ./hello.sh Bob to get the desired answer which is Hello Bob, I am a BASH script!

In the given shell we have passed one argument therefore script uses $1 as a variable to store the first argument passed through the terminal. Similarly $2 is used when two arguments are passed and we need to use the second argument along with the $1 for the first argument, ......etc.

Moreover, if you want to take all the arguments together, then use $* in the shell script.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Script 2.      Again write and save the following lines of shell command that together form a shell script, in a text file named as bakThatUp.sh

     #!/bin/bash
     if[ $2 -eq 0 ] 
     then 
         DestFolder= "$2_bak"
         SourceFolder="$2"
         mkdir DestFolder
         cp -r SourceFolder DestFolder 
    if [ $2 -eq "Archive" -a $1 -eq "-t"]
    then 
        DateStamp= $(date +"%F") 
        DestFolder= "$DateStamp_$2"
        SourceFolder="$2"
        mkdir DestFolder
        cp -r SourceFolder DestFolder 
    if [ $2 -eq "-t" -a $1 -eq "Archive"]
    then
       DateStamp= $(date +"%F")
       DestFolder= "$DateStamp_$1"
       SourceFolder="$1"
       mkdir DestFolder
       cp -r SourceFolder DestFolder 

   if [ $2 -eq " ArchiveBackUpFolder " -a $1 -eq "Archive"]
       then
         DestFolder= "$2"
         SourceFolder="$1"
         mkdir DestFolder
      cp -r SourceFolder DestFolder

Therefore, in this shell script we have used $1 and $2 to take the first and second arguments given through the terminal and simply used the if condition to select among the conditions. mkdir is used to create a directory for the destination folder and cp -r is used to copy the contents of the source folder in to the destination recursively.


Related Solutions

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.
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
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...
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...
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.
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...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in the current directory whose names end in 't' or 'z'. For such files only, your program should list certain LINES of the file(s). These lines are those that have at least 4 x's somewhere in the line. Note that the x's may be upper- or lower-case, and may be separated by other characters; so the following 3 lines DO match: XXxX and more things...
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...
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
this is bash scripting. a. Explain the difference between grep and egrep? b. Write a command...
this is bash scripting. a. Explain the difference between grep and egrep? b. Write a command to list files where third letter is x or y? c. Write command to remove array element with id 5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT