Starting from:

$29.99

HW2 Shell Programming

CS507 Computing Foundation for Computational Science HW2
Shell Programming

Submission command: submit minlong CS507 HW2
Instruction to electronic submission: http://cs.boisestate.edu/∼cs221/SubmissionProcedure.html
• The written assignment can be done in a pure text format (*.txt, for example, problem1.txt) on
Onyx.
• The programming assignment should be presented with source codes.
• Each problem should have its own working directory, such as HW2/prob1, HW2/prob2 ... For
example, the following table shows the structure of HW1 from the user “student1” and how to
submit HW to us through Onyx
1 [ student1@onyx :HW1] $ l s −l
2 drwxr−x−−−. 2 s t u d e n t 1 S tuden t s 13 Sep 19 2021 prob1
3 drwxr−xr−x . 3 s t u d e n t 1 S tuden t s 14 Sep 19 2021 prob2
4 [ student1@onyx :HW1] $ cd prob1 /
5 [ minlong@onyx : prob1 ] $ l s
6 −rw−r−−−−−. 1 s t u d e n t 1 S tuden t s 943 Sep 19 2021 problem1 . t x t
7 $ cd . .
8 [ student1@onyx :HW1] $ pwd
9 /home/ s t u d e n t 1 /CS507/HW1
10 [ student1@onyx :HW1] $ submit minlong CS507 HW1
Listing 1: A sample structure of homework and submission procedure.
• Your source codes (if any) must compile and run on Onyx.
• Documentation is important and proper comments are expected in your source code.
– comments giving description of: purpose, parameters, and return value if applicable
– other comments where clarification of source code is needed
– proper and consistent indentation
– proper structure and modularity
Don’t ask us or your classmates directly for solutions (it happened); just try as much as possible.
Be patient and enjoy coding!
1
Programming Problems
1. (10 pts) Write a shell script to check to see if the file “/home/myname/CS507/HW1/readme.txt”
exists or not. Next, check to see if you can write (i.e., have ”w” permission or not) to the file or
not. Hint, you may need to use test construct ([ or [[) and flags (e.g, -f, -d, ...) (see Page 37 in
Lec02).
2. (20 pts) Create 5 txt files (a.txt, b.txt, c.txt, e.txt, f.txt) in your working directory.
(a) Write a Shell script count1.sh without using any logic control structures to count the total
number of .txt files in the current directory, and print out the number.
(b) Write a Shell script count2.sh using for-loop to count the number of .txt files in the current
directory and print out the number.
(c) Write a Shell script count3.sh to print the name of files and the total number of files in the
current directory.
(d) Write the shell script that renames all txt files to begin with today’s date in the following
format: YYYY-MM-DD. For example, rename “a.txt” to “2022–09–21-a.txt”. (Hint: try
”date +%F command”, %F is a flag showing the required format).
3. (20 pts)
(a) Use for-loop to write a shell script sum1.sh that takes an unspecified number of command
line arguments of integers and finds their sum. That is, the for loop should read all input
arguments and count all arguments and add them together.
(b) Use a different style of for-loop to write a shell script sum2.sh to compute the summation
between 2 numbers, for example, P100
i=1 i. Obviously, the above method doesn’t work, since
it’s not easy to list the entire numbers as augments.
(c) Use a while-loop to write a shell script sum3.sh to compute P100
i=1 i.
(d) Write a script that executes the command “bash sum4.sh”. If the command return a 0 exit
status, report “command succeeded” and exit the test construct with a 0 exit status. If the
command returns a non-zero exit status, report “Command failed” and exit with a 1 exit
status.
1 $ bash sum1 . sh 1 10 20
2 31
3 $ bash sum2 . sh 1 100
4 5050
5 $ bash sum3 . sh 1 100
6 5050
7 $ bash e xi t −s t a t u s . sh
8 bash : sum4 . sh : No such f i l e o r d i r e c t o r y
9 Attn : Command f a i l e d
Listing 2: Sample output.
4. (10 pts) The default rm command will not confirm before it deletes any regular files. Write a
short script called saferm.sh, such that it will make a copy before deleting a single file, by do the
following:
(a) (3 pts) Take one and only one argument at the command line. That is, if the number of
arguments is more than 1, print out a warning and use shell command exit to exit the script.
2
(b) (3 pts) Create a directory “removed files” in the current folder if it is not already created.
Otherwise print out a warning and continue.
(c) (2 pts) Copy the file indicated by the first argument to this “removed files” folder.
(d) (2 pts) Remove this file in the current working directory.
5. (10pts) Let’s do a guess game. $RANDOM holds a random integer in shell. Let’s use it to
generate a smaller random number in a range [0, 50] (Hint: use modulus %). Write a shell
script which can randomly set up a number as answer and read input from user (Hint: use read
command). When user’s guess number doesn’t match the answer, it will prompt the user to input
another guess till they match.
1 $ echo $RANDOM
2 24459
3
4
5 $ bash numbergame . sh
6 The magic number i s between 0 and 5 0.
7 Make your g u e s s : 2 8
8 28 i s t o o low
9 The magic number i s between 29 and 5 0.
10 Make your g u e s s : 3 0
11 30 i s t o o low
12 The magic number i s between 31 and 5 0.
13 Make your g u e s s : 5 0
14 50 i s t o o hi gh
15 The magic number i s between 31 and 5 0.
16 Make your g u e s s : 4 0
17 40 i s t o o low
18 The magic number i s between 41 and 5 0.
19 Make your g u e s s : 4 5
20 45 i s t o o low
21 The magic number i s between 46 and 5 0.
22 Make your g u e s s : 4 8
23 48 i s t o o hi gh
24 The magic number i s between 46 and 4 7.
25 Make your g u e s s : 4 7
26 47 i s t o o hi gh
27 The magic number i s between 46 and 4 7.
28 Make your g u e s s : 4 6
29 You g o t i t i n 8 g u e s s e s s !
Listing 3: Sample output.
3

More products