CIT050 Index > Assignment: The Shell (1)

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

Assignment: The Shell (part 1)

Read everything before doing anything!

Before you start

Connect to the server, and do these commands to copy some files to your home directory:

cp ~linux199/myscript.sh ~
cp ~linux199/numbering.pl ~

Change their permissions to be readable, writeable, and executable by the user (you).

If you do not have a bin directory (type ls -d $HOME/bin to find out), then create it by typing mkdir $HOME/bin. The special shell variable $HOME contains your home directory’s name, such as /home/linux199 or whatever your number happens to be.

Part One - Commands

  1. Log in and type script shell_script at the prompt. The system will respond with

    Script started, file is shell_script

    This means that the system will start “recording” all your input and output into a file named shell_script.

  2. Type this command to see the status of all filesystem:

    df
  3. Get the “human-readable” form by typing:

    df -h
  4. Type the following to show that multiple single-letter options can be given individually, in a group, or in any order:

    df -h -T
    df -T -h
    df -hT
  5. Use the long option form to do the same thing:

    df --human-readable
  6. Find out if you have to use the entire string in the long option by typing these three commands:

    df --human
    df --hu
    df --h

    What conclusion do you draw from this?

  7. Display your current $PATH by typing:

    echo $PATH
  8. Add the $HOME/bin directory to your path (if it is already in the path, don’t worry about having it there twice).

  9. Now display the $PATH variable’s contents again.

  10. Change to your home directory.

  11. Type myscript.sh

    You will get you an error message. When you enter a command that isn’t built into the shell, the shell presumes it’s the name of an executable file, and it tries to find that file. The shell looks through all the directories in your $PATH, one at a time, and does not find myscript.sh in any of them. (Your current directory isn’t in the $PATH, so the shell doesn’t look there.)

  12. You could add . (your current directory) to the $PATH, but that is a bad idea. Instead, you can tell the shell where the script is by giving the script’s pathname, either relative or absolute. Try these; they should all work:

    ./myscript.sh
    ~/myscript.sh
    /home/linux1nn/myscript.sh # where nn is your user number
  13. Move the myscript.sh into the bin directory.

  14. Now type myscript.sh

    This time it works, because the file is found in your bin directory, which you added to the $PATH six steps ago.

  15. Type exit at the command line to save the script file you started in step 1.

Part Two - Redirection

Let us now redirect our attention (yes, I intended that) to making the shell’s standard input and output come from different places. The book talks about standard input and standard output, but it does not talk about standard error, which is where error messages go. Ordinarily this is the screen, but we will experiment with redirecting it to files.

You use < to redirect standard input and > to redirect standard output. You use 2> to redirect standard error. Do not leave a space between the 2 and the greater than sign. Why the 2? Because Linux allocates numbered file handles to manipulate flies. Standard input is file handle number zero, standard output is file handle number one, and standard error is file handle number two.

You may use any or all of these with a Linux command.

For example, if I wanted to run a program named analyse and redirect its input from a file named infile.dat, I could write:

./analyse < infile.dat

If I wanted to run the program with input from infile.dat and standard output to result.txt, I would write:

./analyse < infile.dat > result.txt

In the preceding command, any error messages would still come to the screen. I could divert those error messages to the “bit bucket” (device /dev/null) with this command:

./analyse < infile.dat > result.txt 2> /dev/null
  1. Make sure you are in your home directory.

  2. Type the following command to continue saving your commands in the script file you started in part one:

    script -a shell_script
  3. Redirect the output of the following command to a file named ftpservices in your home directory.

    grep ftp /etc/services
  4. Redirect the output of the df command to a file named filesystems.txt.

  5. Set the noclobber option.

  6. Redirect the output of the date command to the file named filesystems.txt. If you did the previous step correctly, you will get an error message.

  7. Append the output of the date command to the file named filesystems.txt (see page 121). You should be able to do this without an error message.

  8. Unset the noclobber option.

  9. Run file numbering.pl (remember, it’s in your home directory, so you need to give a path name). This program takes input from standard input and echoes it with line numbers. standard input. Type a line and press ENTER to see it at work Repeat until bored, then press CTRL-C to exit the program.

  10. Run file numbering.pl again, redirecting standard input to come from file /etc/mtab

  11. Type the following command. The first and last files exist; the second file does not, and generates an error message.

    ls /etc/passwd /blah /usr/bin/perl
  12. Retype the preceding command, redirecting standard output to /dev/null Notice that the error output is not redirected.

  13. Retype the preceding command, redirecting standard error to /dev/null Notice that the standard output is not redirected.

  14. Retype the preceding command, redirecting standard output to file ok.txt and standard error to file err.txt

  15. Show the contents of ok.txt (use cat).

  16. Show the contents of err.txt (use cat).

  17. Retype the previous ls command, redirecting both standard output and standard error to all.txt

    The notation 2>&1 means “redirect standard error to the same place as standard output” (remember, it’s file handle number 1).

    The command that you type should end up reading like this, if it were in English: “List the three files, sending the standard output to all.txt and sending the standard error to the same place as standard output.”

    Note: You will want to cat all.txt to make sure that you got something reasonable as a result.

    Please note that the following will not work; you end up with two “people” trying to write the same file at the same time. Try it and see what happens.

    ls /etc/passwd /blah /usr/bin/perl > mixed.txt 2> mixed.txt
    cat mixed.txt
  18. Type exit at the command line to save the script file.

Part 3: Pipes and Processes

  1. Log in and type script -a shell_script at the prompt. The system will respond with

    Script started, file is shell_script

    The -a will append, that is, continue to “record” all your input and output into a file named shell_script.

  2. The wc (word count) command counts the number of lines, words, and bytes in a file. To see the number of lines in /etc/services, type:

    wc --lines /etc/services
    or
    wc -l /etc/services
  3. If you do not give a filename, wc takes its input from the keyboard. Type this:

    wc

    And then type these three lines:

    Line one
    Line two
    Line three

    and press CTRL-D. Your output should be the number of lines, words, and bytes.

  4. ps -ef will list all the processes running on your system. Try typing it now.

  5. If you want to know how many processes are running, you could count them all, but why do that? Machines can count better than we can. Pipe the output of the ps command to wc:

    ps -ef | wc --lines

    That number, minus one (for the header line) is the number of processes running.

  6. Set up a filter to find out how many processes are being run by the root user:

    ps -ef | grep 'root' | wc --lines
  7. Use the tee command, which sends output to both standard output and a filename of your choosing:

    who | tee who.out | wc --lines
    cat who.out
  8. Run file ~linux199/task.pl This program will print a message every twenty seconds. If you press ENTER, you won’t get your command prompt back, because the task is running in the foreground, and that task doesn’t accept keyboard input.

  9. Press CTRL-C to stop the task.

  10. Now run file ~linux199/task.pl in the background. Notice that, when you press ENTER, you get your command prompt back right away.

  11. Type pwd (print working directory) to verify that you can type other commands while the task is in the background.

  12. Bring the task to the foreground

  13. Put the task in the background again by pressing CTRL-Z and then typing:

    bg
  14. Kill the background task by typing:

    kill %1
  15. Type exit at the command line to save the script file.

Part 4: Filenames and pathnames

  1. Type script -a shell_script to continue accumulating your input into the shell_script file.

  2. Make the following files by typing this:

    touch main spain rainy paint gainer
    touch bed bid bud bad bsd
  3. Use the ? wildcard to list all files that start with the letter b and end with the letter d.

  4. Use the * wildcard to list all files beginning with the letter b

  5. Use the * wildcard to list all files ending with the letters ain

  6. Use the * wildcard to list all files with ain in them. Hint: you may need to use more than one *

  7. Use wildcards to list all files that end with ain followed by exactly one character.

  8. Use a character class to list all files that start with the letter b followed by a vowel and end with the letter d.

  9. Remove the files you created by typing this:

    rm main spain rainy paint gainer
    rm bed bid bud bad bsd
  10. Type exit at the command line to save the script.

When You Finish

Rename the shell_script file in the form lastname_firstname_cit50_shell and email the file to the instructor. Your email’s subject line must read as follows, or your assignment will not be graded. (Replace nnn with the number portion of your user name.)

CIT050 Assignment Shell - linuxnnn

How to Send email from the Server

From the shell prompt start the pine email client by typing:

pine

If this is the first time you’ve used pine, you will get a greeting message that describes the program. Press E to exit the greeting.

You will now see the main menu. Either use the arrow keys to move up to the COMPOSE MESSAGE choice, or press C to compose a message.

You will see a page that lets you compose your message:

To      :
Cc      :
Attchmnt:
Subject :
----- Message Text -----

On the To line, enter the instructor's email address. On the Attachmnt line, enter the pathname to the file (a relative pathname should do fine; if not, use an absolute pathname). Please do not leave the Subject line blank! It would be nice if you left a brief message in the Message Text area, like “Here is the file.”

When you have completed your message, press CTRL-X (shown as ^X to send the message and answer Y when prompted. You will return to the main menu, where you may press Q to quit the program.

Important: pine is a text-based email client. It won’t respond to mouse clicks. Don’t even try.