JMU
Lab: Using the BASH Shell


Instructions:Omitted

Getting Ready:Omitted

1. Conventions: Throughout this lab, the following typographic conventions are used:

[Enter] refers to the "Enter" key.

[Tab] refers to the "Tab" key.

[Down Arrow], [Up Arrow], etc... refer to the down arrow key, up arrow key, etc...

[Ctrl] refers to the "control" key and [Ctrl]+k means that you should press the "control" key and the "k" key at the same time.

The phrase "Enter the command xyz" means that you should type xyz in the terminal/command-shell window and then press [Enter].

2. Working with Directories: This part of the lab will help you learn how to work with directories/folders.
  1. Start a terminal/command-shell window. (For example, click on the "Terminal".)
  2. Enter the command pwd
  3. What output was generated?


    The current working directory which, in this case, is your home directory. (From here on in, this directory will be denoted by home.)
  4. Enter the command mkdir cs
  5. Enter the command cd cs
  6. Enter the command pwd
  7. What output was generated?


    home/cs
  8. Enter the command mkdir labs
  9. Enter the command cd labs
  10. How can you determine the current working directory?


    It may be included in the prompt, but you can always use the pwd command. (Note: You can change the BASH prompt by changing the value of the shell variable named PS1. For example, the command export PS1="[\w]$ " will change the prompt to the current working directory in square barckets, followed by a dollar sign.)
3. Compiling and Executing Java Applications: This part of the lab will help you learn how to compile and execute Java applications (a.k.a., programs) in a command shell.
  1. Enter the command javac WhatAnEgo.java to compile the source code.
  2. What file do you know was created?


    WhatAnEgo.class
  3. Enter the command java WhatAnEgo to execute the byte code.
  4. What run-time error was generated?


    After printing the String "Prof. ", the following error was generated:

     Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
       at WhatAnEgo.main(WhatAnEgo.java:15)
    

  5. Why was this run-time error generated?


    Because WhatAnEgo uses args[0] and we did not supply the program with any command-line arguments.
  6. Enter the command java WhatAnEgo Bernstein to execute the byte code with a single command-line parameter.
  7. What output was generated?


    Prof. Bernstein is the best!
    
  8. Is the belief expressed in the output correct?

    Yes!

4. Executing a Java Application Multiple Times: Especially when testing, it is sometimes necessary to run the same Java application multiple times with different command-line parameters. This part of the helps you do that.
  1. Start jGRASP.
  2. Click on File-New-Plain Text.
  3. Copy the following lines into the newly created file:
    #!/bin/bash
    java WhatAnEgo Biggle
    java WhatAnEgo Broflovski
    java WhatAnEgo Cartman
    java WhatAnEgo McCormick
    

    and not that each of this lines is a valid BASH command.

  4. Save the file as southpark.sh in the directory that contains WhatAnEgo.class
  5. In the BASH shell, enter the command chmod +x southpark.sh to make this file executable.
  6. In the BASH shell, enter the command ./southpark.sh
  7. What happened?


    The program WhatAnEgo was executed four times.
  8. What output was generated?


    Something like:

    
    Prof. Biggle is the best!
    Prof. Broflovski is the best!
    Prof. Cartman is the best!
    Prof. McCormick is the best!
    
    

5. Reducing the Amount of Typing: This part of the lab will help you learn how to work more efficiently and reduce the amount of typing required to accomplish many tasks.
  1. Enter the command nkdir skills_bash (Note: This is nkdir not mkdir!)
  2. What output was generated?


    Command not found.
  3. Press the [Up Arrow] key (or [Ctrl]+p).
  4. What happened?


    The previous command was re-typed.
  5. Type [Ctrl]+a
  6. What happened?


    The cursor moved to the first character on the line.
  7. Type [Ctrl]+e
  8. What happened?


    The cursor moved to the end of the line.
  9. Type [Ctrl]+a to go back to the first character.
  10. Type [Ctrl]+d
  11. What happened?


    The first character was deleted.
  12. Type m (which will insert an m as the first character).
  13. Press [Enter]
  14. Change the current working directory to your home directory by entering the command cd ~
  15. Enter the command pwd
  16. What output was generated?


    The name of your home directory.
  17. You now need to go back to the skills_bash directory. Save yourself some work by entering the command cd - (which takes you back to the previous directory).
  18. What is the full path of your current working directory?


    home/cs/labs/skills_bash
  19. Move up one level in the directory hierarchy by entering the command cd ..
  20. Enter the command pwd
  21. What output was generated?


    home/cs/labs
  22. You now need to go back to the skills_bash directory. Save yourself some work by typing cd sk[Tab] (which tries to "complete" the directory name that starts with s).
  23. What happened?


    The command changed to cd skills_bash
  24. Press [Enter] to change to the skills_bash directory.
  25. Type pdw but do not press [Enter]. (Note: This is pdw not pwd.)
  26. Type [Ctrl]+t to "twiddle" the last two characters.
  27. What happened?


    The "d" and the "w" were reversed.
  28. Press [Enter].
  29. Type mkdir temporary garbage but do not press enter.
  30. Type [Ctrl]+w
  31. What happened?


    The last argument (i.e., the word "garbage") was deleted.
  32. Press [Enter].
  33. Enter the command pwd (to see the name of the current working directory).
  34. Enter the command ls (to list all of the files in this current working directory).
  35. You now need to remove the temporary directory using the rmdir command. You could use the [Up Arrow] a couple of times to re-type the mkdir command. However, save yourself some work by typing [Ctrl]+r but do not press [Enter].
  36. What happened?


    The prompt "reverse-i-search" appeared.
  37. Press the m key.
  38. What happened?


    The first command that started with an m was re-typed.
  39. Type [Ctrl]+a to go to the first letter of the command.
  40. Type [Ctrl]+d twice to delete the m and k.
  41. Type rm to insert the letters r and m.
  42. Press [Enter].
6. Working with Files: This part of the lab will help you learn how to work with files more efficiently.
  1. Enter the command cd (or cd ~) to move to your home directory.
  2. Enter the command ls
  3. What happened?


    All of the files in the home directory were listed.
  4. Enter the command ls -l
  5. What happened?


    All of the files in the home directory were listed, along with attributes of those files.
  6. Enter the command ls > filenames-short.txt (which will redirect the output of the ls command to a file named filenames-short.txt)
  7. Enter the command ls -l > filenames-long.txt (which will redirect the output of the ls -l command to a file named filenames-long.txt)
  8. Enter the command ls -R > filenames-all.txt (where the -R option instructs the ls command to look in all subdirectories)
  9. Enter the command ls filenames* (where the * is called the wildcard character).
  10. What happened?


    All of the files that begin with "filenames" were listed.
  11. You now need to move filenames-long.txt and filenames-all.txt to the directory for this lab. Save yourself some work by entering the command mv filenames-{long,all}.txt cs/labs/skills_bash
  12. How could you have used the [Tab] key to avoid having to type the directory names?


    After typing cs you could have pressed [Tab] twice.
  13. To make sure you have moved the right files, enter the command ls filenames*
  14. To delete/remove the remaining file, enter the command rm filenames-short.txt
  15. Quickly/effciently move to the skills_bash directory.
  16. How did you do it?


    cd cs[Tab][Tab] or cd -
7. Getting Help: This part of the lab will help you learn how to get help in the BASH shell.
  1. Get the "manual pages" for the ls command by entering the command man ls. (Note: You can get help on how to use "manual pages" by pressing h. [Ctrl]+n and [Ctrl]+p work as expected, q will quit/exit, / and ? can be used to search forward and backward.)
  2. What does the clear command do? (Hint: Use the "manual pages".)


    It clears the screen.
  3. What does the cat command do?


    It concatenate files and prints the result on the standard output.
  4. What does the grep command do?


    Searches files for strings.
  5. What does the less command do?


    It displays the contents of a file one page at a time, pausing between pages. It can also be used to search for strings in the file using the / key.
  6. What does the sort command do?


    Sorts lines of text files.
  7. What does the apropos command do?


    Searches a set of database files containing short descriptions of system commands for keywords.
8. Looking At and Inside Of Files: This part of the lab will help you look at the contents of files and search for strings within files.
  1. Show the contents of the filenames-all.txt file by entering the command cat filenames-all.txt. (Note: Make sure you are in the correct directory.)
  2. Search for all occurrences of the string "Feb" in all .txt files in the current working directory by entering the command grep Feb *.txt
  3. Use the less command to search for the first occurrence of the string "Feb" in filenames-all.txt.
9. Using the GUI Too: This part of the lab will give you a convenient skill.
  1. Insert a thumb drive (if you have not already done so).
  2. Type cd in the command shell window.
  3. Click and drag the icon for the thumb drive into the command shell window.
  4. What happened?


    The OS completed the cd command with the name of the thumb drive.
Note: This part of the lab allows you to "go further" on this material. It is neither required nor for extra credit. It will, however, help you gain a deeper understanding of the material.
10. Other Shells:
  1. Learn more about BASH from one of the links on the course help page.
  2. Learn about the standard MS Windows command shell (called cmd) and how it differs from BASH at the course help pages.
  3. Are you using MS Windows but would like to have a BASH shell? Install MinGW and use MSYS. Follow the instructions in the section on the "Graphical User Interface Installer". (You might as well install some other programming languages while you are at it, like, Ada, C++, Fortran, abd Objective-C. You probably won't use them now but you may use them later.)

Copyright 2013