CSC 325 Operating Systems with an Emphasis on UNIX
Assignment 6

In this assignment we begin to explore Linux shell programming. You already know that the word "shell" in Linux is used to the command interpreter, but it is also used to refer to a full-fledged programming language which is called (not surprisingly) the shell programming language or the shell language. Although Linux supports a C programming environment as well as other languages, often programs are written in one of the shells because it is so easy to do so. In addition, any command that can be executed at the command line can be used as a line of code in a shell program, so powerful shell programs can be created. In the last lab we created new single line Linux commands, but through shell programming, we can create new multiple line Linux commands.

  1. Open a text document, and then use ssh to logon to your Linux account on cs.berea.edu.

  2. As many of you will know from previous assignments, bash (the shell we are using) is an acronym for Bourne-Again SHell. This shell is an sh-compatible shell which incorporates features from the Korn shell and the C shell. In your text document, list where the bash program is located on your system. Use the --version option to find out which version of bash you are running. Which version are you running?

  3. Using knowledge from the previous assignment, explain why bash does not exit when you type Ctrl+C on the command line.

  4. First we will use the set command to help us to explore shell positional parameters. Try typing set date at the prompt. Next type echo $1. Then type echo $#. Next type set ls -l and type echo $1. Next type echo $2. Next type echo $3. Then type echo $#. You may want to read the help (not man) pages for set to understand what is going on--The last portion of this help page refers to positional parameters. Describe in your text document what you see echoed.

  5. Try typing set `date` at the prompt (where the ` is the lower case of the ~ key). Then type echo $1. Then type echo $2. Then type echo $3. Then type echo $4. Then type echo $5. Then type echo $6. Then type echo $7. Then type echo $#. Describe in your text document what the set command does, and how the ` symbol changes the resulting output.

  6. Type echo $$ to see what this shell parameter gives us. You will see that it gives us a number which is a PID. Use top with u and your username to determine what PID this shell parameter gives us. Explain in your text document which process this shell parameter gives the PID of.

  7. While in top note the Priority (PRI) and Nice (NI) values of the bash process as well as the top process. Explain in your text document how this information tells you that you are not on a Linux machine using the TRADITIONAL Linux scheduler.

  8. Next type nice -n 19 top and see how this runs top but affects the priority assigned to the top process. If you are interested, you can visit the man page for nice. In your text document explain what differences you see in top this time from when you ran just top without nice.

  9. Next try typing set `cal` at the prompt. Then type echo $1. Then type echo $5. Then type echo $10. Then type echo $15. Then type echo $#. Then type echo $*. In your text document, describe what you got in the shell positional parameters and how the output differed (if at all) from what you expected to get.

  10. Next we explore the shift command (which has no man page but has a nice help page). Execute the shift command. Then type echo $1. Then type echo $2. Then type echo $3.... Continue until you understand what has occurred. Then type echo $*. Then type echo $#. Try shift 2, and try shift -2 Then describe in your text document what the shift command does and how you think it could be useful for using many parameters.

  11. The PATH variable is a shell parameter which tells Linux where to search for executable files. Type echo $PATH to see that /home/username/bin is already listed with your username. Type ls -l to see that you currently have no directory named bin in your home directory. Create a new directory called bin which is a subdirectory of your home directory. The bin directory is where you should put all of your shell programs because this directory is already in your search path. In your text document, type the command you use to create this directory.

  12. Use your favorite editor or some other method to enter the following short shell program into a file called showargs located inside of your bin directory:
  13. In order to run this program, you must change the file permission to make the file executable. Recall that you must use chmod to make showargs executable. In your text document, type the command you used to make showargs executable.

  14. Try your program by typing all of the following: showargs a test of the echo commands Include a copy of your output in your text document.

  15. Recall that we have viewed specific shell parameters such as $TERM, $HOME, $RANDOM in a previous lab. In this shell program , we have new shell parameters $0, $1, $2, etc. Try your program a few more times with different words following the command name showargs until you understand exactly what the program is doing and why. In your text document, explain what each of the shell parameters $1, $2, $3, $4, $* and $# does, and why you believe that they are called shell positional parameters.

  16. Create a new shell program called dr which redirects a LONG listing of your current directory into a file whose name you specify by using the single positional parameter $1, and then tells the user "filename is created" where "filename" is the filename that the user specified. Be sure to include meaningful comment lines in your program. Use the chmod command to make your program executable, and be sure to locate dr in the /bin directory. Test your dr program to see if it works correctly from several subdirectories by creating several files with different names. Copy and paste the code of dr (including comment lines) into your text document.

  17. Have you ever regretted throwing something away and wished you could retrieve it from the trash? In a previous lab we created an alias called del which prompts you before removing a file. In this lab we will improve on this idea. Let's create a trashcan directory where we put files to be deleted later. Create a new directory in your home directory named .trash Note the dot that precedes the directory name makes the directory a hidden directory. (After all, who wants to look at their own trash?) Next use ls .* to see that you have created the desired directory.

  18. Create another new shell program called dl which moves any number of files from any of your directories into the .trash directory without prompting you. Be sure to include comment lines in your program and to make your program executable before you run it. Test your dl program from several different subdirectories. Hint: Use the ~ to designate the path to .trash from your home directory. Copy and paste the code of dl (including all comment lines) into your Word file.

  19. Clean up your Linux account and move all of your unneeded files into your new .trash directory by using your new dl program. Feel free to delete all old homework files except .bashrc, dr, dl, and .plan. Then type cd .trash Now type rm -i to permanently delete all of the files in the .trash --the computer should prompt you at each file to be certain that you are not trashing something important.

  20. In your text document, write one or two lines about what you think of Linux shell programming so far.

  21. Save your text file as yourlastnameA6.txt.