JMU JMU - Department of Computer Science
Help Tools
Lab: Using the BASH Shell


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Download the following files:
    to an appropriate directory/folder. (In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above and then selecting Save as... or Save link as....)

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.)
    Expand
  4. Enter the command mkdir cs
  5. Enter the command cd cs
  6. Enter the command pwd
  7. What output was generated?


    home/cs
    Expand
  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.)
    Expand
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. Change the working directory to the one that contains the files you downloaded. (Note: cd ~ will change the directory to your home directory.)
  2. Enter the command javac Tripler.java to compile the source code.
  3. What file do you know was created?


    Tripler.class
    Expand
  4. Enter the command java Tripler to execute the byte code.
  5. Enter the command java Tripler.
  6. What was output?


    10 tripled is 30
    Expand
4. Redirecting Input: It can be tedious to test programs that accept input from a user because you often have to run them many times, typing the user's responses each time. One can, instead, tell the program to read from a file rather than the console. This is called input redirection. This part of the lab will help you understand input re-direction.
  1. Enter the command cat test1.inp to see the contents of the file named test1.inp. (More on this command later.)
  2. What is in the file?


    It has a single line:
    10
    
    Expand
  3. Enter the command java Tripler < test1.inp.
  4. Given the title of this section, what do you think the < does?


    It tells Tripler to read from the file named test1.inp rather than from the console.
    Expand
  5. What was output?


    Enter your number: 10 tripled is 30
    
    Expand
  6. Why aren't the two parts of the output on different lines?


    Because the first call to print() does not include a \n character.
    Expand
  7. Why were the two parts of the output on different lines when the user typed the response (rather than it bein redirected)?


    Because the user pressed the Enter key.
    Expand
5. 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 Tripler < test1.inp
    java Tripler < test2.inp
    

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

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


    The program Tripler was executed two times.
    Expand
6. Redirecting Output: Sometimes the output from a program can be long and confusing. In such situations, one can tell the program to redirect the output to a file rather than the console. This is called output redirection. This part of the lab will help you understand output re-direction.
  1. Enter the command java Tripler > test1.out.
  2. Type 10 and press Enter.
  3. Enter the command cat test1.out.
  4. What was displayed?


    The contents of the file test1.out, which is:
    Enter your number: 10 tripled is 30
    
    Expand
  5. Why wasn't the prompt displayed on the console?


    Because all output was re-directed to the file.
    Expand
  6. How could one use this to test your programs?


    If one had a file with the expected output then one could use a file comparison utility (like Meld or Kdiff3) to compare the actual output and the expected output for differences.
    Expand
  7. For practice, use a file comparison utility (like Meld or Kdiff3) to compare test1.exp (the expected correct output) and test1.out.
  8. For more practice, Change Tripler.java so that it no longer produces the correct output, run the program again (again redirecting the output to a file), and compare this output with the expected output.
  9. Note that, because of the file names, it is fairly easy to overwrite an existing file when redirecting output. Hence, you may want to use the chmod command to make the .in and .exp files read-only. For example, chmod a-w *.in will remove write access on all files that end with .in for all users.
7. 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.
    Expand
  3. Press the [Up Arrow] key (or [Ctrl]+p).
  4. What happened?


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


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


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


    The first character was deleted.
    Expand
  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.
    Expand
  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
    Expand
  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
    Expand
  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
    Expand
  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.
    Expand
  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.
    Expand
  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.
    Expand
  37. Press the m key.
  38. What happened?


    The first command that started with an m was re-typed.
    Expand
  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].
8. 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.
    Expand
  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.
    Expand
  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.
    Expand
  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.
    Expand
  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 -
    Expand
9. 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.
    Expand
  3. What does the cat command do?


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


    Searches files for strings.
    Expand
  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.
    Expand
  6. What does the sort command do?


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


    Searches a set of database files containing short descriptions of system commands for keywords.
    Expand
10. 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.
11. 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.
    Expand
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.
12. 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 2020