Simple BASH Programming
For this assignment, you are going to write bash shell scripts. Each of your script should be named according to the following convention:
As_2_YourName_ problem*.sh
where YourName should be replaced by the first letter of your first name combined with your last name and where star ‘*’ in problem* should be replaced with the problem number for which your script provides a solution.
For example, if I write a script that is an answer to problem 2, I would name it As_2_jkusyk_problem2.sh. Store all your scripts in As_2_YourName directory and compress the directory using zip command in your Linux terminal asAs_2_YourName.zip file. (Use, for example, $ zip -r As_2_YourName.zip As_2_YourName command, whereAs_2_YourName.zip is the name of compressed file to be created, As_2_YourName is the directory where you store all the scripts for this assignment (you may have to provide an absolute or a relative pathname to it), and option -r tells the zip command to compress everything recursively in that directory. Submit As_2_YourName.zip file as an attachment via Blackboard.
Make sure that for each script you:
1. Write what shell should run it (first line of the script starting with #!).
2. Write short comment about with your name and what it is supposed to do (rephrase question of the problem it is responding to).
3. Whenever I ask you to print (to a screen/terminal) a value of a parameter/variable, make sure that you include some text informing what you are actually printing (e.g., do not use echo “$my_var” statement but rather something more informative, like echo “Variable my_var is: $my_name”.
4. Save it with an appropriate name (see above).
5. Change permissions so I (others) can read and execute it.
I will give partial credits for scripts that perform some but not all of the required functions.
Problem 0
Write a simple bash script, as described on slide 9 (page 5) of the lectures notes 03_BashShellProg, name it As_2_YourName_problem0.sh, save it in a folder As_2_YourName, change its permissions, and run it.
Problem 1
Write a bash script that:
a. Creates a variable my_name that contains your first and last names
b. Creates an array variable cars_I_know that consist at least four brands of cars
c. Prints to a standard output (screen/terminal) (use echo command) :
i. A single line with your name and one of the names of cars obtained from the cars_I_knowarray variable by accessing its respective location/index
ii. All the elements of the array cars_I_know in a single line
iii. Length of the 3rd element of the array cars_I_know
iv. How many elements are there in the array cars_I_know.
d. Prints to the standard output (screen/terminal) any statement informing that your script finished running (e.g., put as the last statement echo “It is the end of my script to problem 1 of assignment 2”)
Problem 2:
Write a bash script that prints to the standard output (screen/terminal):
i. Number of arguments that were passed to it as positional parameters
ii. The first and third argument passed to it as one of the positional parameters
iii. The last arguments that was pass to it as the positional parameters or a message to the standard error that “No arguments has been received,” if it is a case.
For this problem, you may find useful to use bash variable ${!name}, which means the value of the parameter whose name is $name. This works when $name is either a named parameter or a number (denoting a positional parameter). Another way would be to use bash variable BASH_ARGV or shift command.
Problem 3:
Write a bash script that:
a. Prints to the standard output (screen/terminal):
i. A value of the variable holding a path to your home directory
ii. Whatever your PATH variable holds
iii. A value of the variable holding your secondary script
b. Make your secondary script to be a question mark followed by a greater sign (i.e., ?>) and print to the standard output (screen/terminal) the information what is your secondary prompt now by accessing PS2 variable.
Problem 4:
Write a bash script that prints to the standard output (screen/terminal):
i. The name of your Login shell
ii. The name of the shell from which your script started (spawned/forked)
iii. PID of the shell from which your script started (spawned/forked)
iv. PID of the process that executes your shell (PID of spawned/forked shell
Note that the command echo $0 may not be helpful when you call the script with a relative or absolute pathname. Consider using commands ps and cut as well as some Keyword Variables.
/docProps/thumbnail.jpeg