$29
Questions
Question 1 (10 Marks)
Consider the following bash script test1 :
#!/ bin/ bash
# My Shell Script !
x = $1
echo $0
echo $1
echo $ #
echo $ *
echo $x
shift
echo $0
echo $1
echo $ #
echo $ *
echo $x
Explain what each numbered line of the script does, what it would output (if anything) and why.
Show the output produced by the following call to test1:
test1 a b c
Question 2 (10 Marks)
Consider the following bash script test2 :
#!/ bin/ bash
echo -n ' Input Number : '
read x
echo expr $x + 7
echo " expr $x + 7 "
echo ' expr $x + 7 '
echo ` expr $x + 7 `
Explain what each numbered line of the script does, what it would output (if anything) and why.
Show the output produced by the running test2 and inputting the number 10.
Question 3 (15 Marks)
Consider the following bash script good:
#!/ bin/ bash
echo
hour = ` date +% H `
if [ " $hour " - lt 12 ]
then
echo " GOOD MORNING "
elif [ " $hour " - lt 18 ]
10then
echo " GOOD AF TE RN OO N "
else
echo " GOOD EVENING "
fi
echo
Explain in one or two sentences what this script does.
Rewrite this script to not use elif . Make sure you include a plain text copy of the rewritten script named good2 in your submission.
Question 4 (20 Marks)
Write a bash shell script called dobackup that does the following in order:
Output the text \Input a directory name:".
Read a directory path from the user via standard input. You can assume that the path contains no spaces and you do not need to support the tilde character ( ).
Check if the directory path is valid (i.e. that the directory exists) and print an error and exit if it is invalid (does not exist).
If a directory named backup already exists in the current working directory, delete it and all the les and subdirectories it contains.
Make a copy of the directory given by the user to the current working directory and name it backup (this copy should contain all les and subdirectories of the original).
Use the tar command to make an archive of the backup directory named backup.tar in the current working directory. We did not go over the tar command in class so it is up to you to read the man page for tar. You do not need to use compression.
Rename backup.tar such that its le name contains the current date in the following format:
backup-<day-<month-<year.tar
where <day is the current day (with a leading 0 if one digit), <month is the current month (with a leading zero if one digit) and <year is the current 4 digit year. For example, if today was January 31st, 2018 the le should be renamed to backup-31-01-2018.tar
Use scp to send a copy of the tar le to a directory in your home directory named mybackups on the course server (it is assumed that this directory already exists). This command should be written so it will work on the lab computers or the course server. Rather than using your username with the scp command use the value of the $USER variable (contains the current user). It is OK if scp asks the user for their password.
Add comments to your code explaining what every line does as well as a comment at the beginning of the le (under #!/bin/bash) that contains your name, student number and a one or two sentence description of what the script does.
Test your script both on the course server and on a lab computer in MC244. Ensure that it works on both systems and that a copy of your backup archive is sent to the course server in both cases. Include a plain text copy of your script named dobackup as well as a copy in your PDF le.
Question 5 (20 Marks)
Write a bash shell script called avg that takes the average of the integer arguments given to it on the command line. The average should be sent to the standard output. Assume that all arguments are valid integers and that there can be more than 9 arguments.
Your shell script must use a loop and at least one variable.
The number you output should have two decimal places.
Your shell script should output an appropriate error if no arguments are given to it.
You should include a comment at the beginning of the le (under #!/bin/bash) that contains your name, student number and a one or two sentence description of what the script does. Also add at least two comments to lines in your script explaining what they do.
Test your script and provide the output for the following commands:
Command
Correct Output
avg
Need at least one argument!
avg 42
42.00
avg -50
0
50 100
25.00
avg
1
2
3
4 5
6 7
8 9
10
11
12 13 14
15
16
17
18 19
20
10.50
Make sure to both include your shell script in the PDF and as a plain text le named avg.
Question 6 (25 Marks)
Write a bash shell script called nums that takes zero, one or two arguments and gives the following output:
Command
Result (output to standard out)
nums
\Usage: nums option lename"
nums 0
\Error: no lename given!"
nums 1
\Error: no lename given!"
nums 2
\Error: no lename given!"
nums 3
\Error: no lename given!"
nums 0 file
Outputs the number of lines in the le.
nums 1 file
Outputs the le with all letters made upper case and all whole numbers
replaced with *s. For example: \I have 123 apples" would become \I
HAVE * APPLES".
nums 2 file
Outputs only the lines in the le that are 7 letter palindromes.
The palindromes should contain only letters (not numbers or special
characters). The line cannot contain any other text or be any length
other than 7 characters long (not counting line breaks). Capitalization
should be ignored.
nums 3 file
Outputs the 4 largest numbers in the le. The le may contain text,
in which case the text should be ignored. Numbers may be on the
same line and separated by spaces or text and/or on di erent lines.
Hint: You will need to use a combination of the commands cat, head,
tr and sort. Check the man page for sort to learn how to sort numbers
and set the order.
Any other option number
\Invalid option!"
If le does not exist
\ le does not exist!" where le is the name of the le.
You should include a comment at the beginning of the le (under #!/bin/bash) that contains your name, student number and a one or two sentence description of what the script does. Also add at least four comments to lines in your script explaining what they do.
Make sure to both include your shell script in the PDF and as a plain text le named nums.
Example File ( le.txt):
Hello World!
Racecar
deified reviver
123+456=579
ROTATOR
42
See citation (150) in Chapter 3.789.
Time to repaper the wallpaper.
Example Output:
nums
Usage: nums options filename
nums 1
Error: no filename given!
nums 4 file.txt Invalid option!
nums 1 badfilename badfilename does not exist!
nums 0 file.txt
8
nums 1 file.txt
HELLO WORLD! RACECAR DEIFIED REVIVER *+*=*
ROTATOR
*
SEE CITATION (*) IN CHAPTER *.*.
TIME TO REPAPER THE WALLPAPER.
nums 2 file.txt Racecar
ROTATOR
nums 3 file.txt 789 579 456 150
6 of 6