In: Computer Science
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
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.