CIT050 Index > Assignment: bash (1)

Assignment: bash (1)

The most up to date version of this assignment is at Moodle.

Read everything before doing anything!

Please go through this exercise once to try everything out; when you turn in your script file, I don’t want to see every bad attempt you made to get something to work. I just want to see the results of working commands. Hint: Write down what you think the correct answers are, then try them. Once you get everything working, go through all the steps and record them in a script file.

Before You Start

  1. In your home directory, create a file called, and use vi to insert the following lines. On the last line of the script, that is a digit one before the two closing parentheses, not the lowercase letter "l".

    echo "Sample script."
    echo "The date and time is $(date)"
    echo "Number of processes: $(( $(ps -ef | wc --lines) - 1 ))"

    Note: the word echo and the quote marks are part of the script. You must enter the entire lines, exactly as they are shown above, or the script will not work properly.

  2. Copy ~linux199/ to your home directory, and use vi to replace the name Federico with your first name.

  3. Use chmod to make executable. Do not make executable.

  4. At the end of your ~/.bashrcfile, add this line:

    echo "Starting shell on $(date)"

The Tasks

  1. Type script bash1.script to start recording your commands.

  2. Do a single lscommand to determine which of the following exist:~/.bash_profile, ~/.bash_login, and ~/.profile.

  3. Do an su to your own username; that is, if you are user linux199, type su linux199

    When asked for your password, enter it. You should get the message you entered in step 4 of “Before You Start.”

  4. Type exit to go back to your original shell.

  5. Try typing ./; this should generate an error message, because you have not made the script executable.

  6. Run the commands in by typing source

  7. Run the commands in by typing ./; that should work fine.

  8. Do a single ls -l command on the files /etc/passwd,/etc/nosuchfile, and /etc/services. Redirect the standard output to a file named filelist.txt and standard error to a file named errors.txt

  9. cat filelist.txt and then cat errors.txt putting both commands on one line (hint: see page 282).

  10. Type the following commands:

    pushd /etc
    pushd /usr/lib
    pushd /bin
  11. Now use the popd command to return to your home directory (~). You may have to use it more than once!

  12. Create a variable named me whose value is your full name. If your name is Joe Doakes, then the variable must have the string Joe Doakes in it, including the blank.

  13. Using the variable you created in the previous step, write an echo command that will produce the output:

    My name is full name
  14. With one command, declare a variable named quantity to be of type integer and set it to the value 1789. Verify that you have done this correctly by typing echo $quantity

  15. Assign the string "hello" to variable quantity, and then repeat echo $quantity to see what happens when you assign a non-number to a variable that is declared to hold integers.

  16. Save your primary and secondary prompt variables by typing the following:

    save1=$PS1; save2=$PS2
  17. Set your primary prompt to the current time of day in 12-hour AM/PM format followed by a space, followed by the pathname of the working directory, followed by a space, a greater than sign, and a space (see page 300 [2nd edition]/ page 310 [3rd edition]).

  18. Set your secondary prompt to two greater than signs, followed by a space, then verify it by typing (on two separate lines):

    echo "command that needs
     secondary prompt"
  19. Restore your primary and secondary prompt variables by typing the following:

    PS1=$save1; PS2=$save2
  20. Type cd to return to your home directory.

  21. Type cd init.d; this should fail, since there is no directory named init.d in your home directory.

  22. Make a directory called testdir (it may already exist; if so, that’s fine).

  23. Set your CDPATH variable to /etc

  24. Type cd testdir

  25. Type cd init.d; it should succeed, since CDPATH has been changed.

  26. Type exit to end the script.

When You Finish

Change the name of your script file to the form lastname_firstname_bash1.scriptand upload it to Moodle.