Assignment: bash (2)

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.

The Tasks

Processes

  1. Type script bash2 to start recording commands.

  2. Type these commands:

    sleep 15 &
    ps --forest
  3. Type these commands, one per line:

    date
    pwd
    cd
    df --human
    who
  4. Write a single echo command that shows the value of the variables HISTSIZE, HISTFILE, and HISTFILESIZE. (Hint: use $)

  5. Show the last five commands you’ve typed by entering history 5

  6. Use the fc command to show every command you have typed since the most recent ps command, and only those commands.

  7. Use the fc command with the -s option to re-execute that ps command.

  8. Use ! to reference the most recent df command you did. Do not use the history number.

  9. Use ! to redo the sixth preceding command.

  10. Use ! to redo the most recent command that contained the letters wd.

  11. Type touch file05 to create a file.

  12. Use ! with the s modifier to re-execute that command, only substituting the 5 with a 6 (see pages 316 and 317)

  13. We are skipping the vi and emacs editing modes on pages 318-319.

  14. Type ls file and then the TAB key three times to see all filenames beginning with file.

  15. Type echo $OLDP followed by the TAB key to see variable completion in action.

  16. We are skipping the material from the bottom of page 321 to the middle of page 324.

  17. Create an alias dfh which is an alias for df --human

  18. Type dfh to see if it worked.

  19. Show a list of all aliases currently available.

  20. Type cd to return to your home directory.

  21. The next few examples are very important. Type this command:

    alias dirA="echo Working Directory is $PWD"

    Because the text is enclosed in double quotes, the variable $PWD is evaluated right then and there, as the alias is created, and the alias will contain the name of your home directory.

  22. Now type this command:

    alias dirB='echo Working Directory is $PWD'

    Because the text is enclosed in single quotes, the alias will contain the literal string $PWD; it will not be evaluated right away.

  23. Change to a different directory: cd /etc

  24. Type dirA; since the home directory name was expanded when the alias was built, it shows you your home directory name, which is wrong.

  25. Type dirB; since the $PWD is in single quotes, it gets evaluated when the alias gets used.

  26. Use a single unalias command to get rid of the aliases dirA and dirB.

  27. Use the alias command with no arguments to verify that those aliases are gone.

  28. This one is just too cool to pass up: type cd /user; it should give you an error message (it’s the /usr directory, not /user).

  29. Now type this: shopt -s cdspell

  30. Now type cd /user again. It’s magic!

  31. Now turn off the cdspell option. (pages 331-332)

  32. Confirm it by typing cd /user again, and getting an error message.

  33. Type exit to stop recording your commands in the script file.

When You Finish

Change the name of your script file to the form lastname_firstname_bash2.script and upload it to Moodle.