Software Carpentry Day 4: Shell

Morning Exercises

The goal of these exercises is to get you thinking about the kinds of things you can do with basic shell commands and to give some practice using them. Ideally, you will use these simple exercises to help you understand the basic operation of the shell, and to ask more questions about how to do various kinds of operations. Don't be afraid to ask the TAs or instructors for pointers on what to try next.

Which Shell?

Are you running the bash shell? You should be for these exercises, and since most versions of Linux come with bash as the default shell, it makes sense to learn that one.

Your environment

When you run a shell (or any program really), there are a bunch of environment variables that the program can access. These are similar to global variables in a programming language. You have already seen one example of an environment variable above (SHELL).

Exploring the file system

File system structure

Creating directories

Other things you can do with ls

Try out some of the basic commands from the notes

More Shell Exercises

Remember that the goal of these exercises is to give you practice with various shell commands. Try experimenting with all of the commands and approaches mentioned in the lecture.

Wildcards

  1. List all of the files in /usr/bin that start with “py”
  2. Use a pipe to print the number of files in /usr/bin that start with “py”
  3. How many new ways can you refer to your home directory now?

Grep and pipes

The wget program downloads a web page.

  1. Use wget to download http://friendfeed.com/softwarecarpentryjuly2009.
  2. How many times does Tovey appear in the file?
  3. How many times does http appear in the file?

Processes and I/O redirection

  1. Save the output of ps -ef into a file called snapshot.txt.
  2. How many unique users are running bash? (Use sort, uniq, and cut.)
  3. Instead of using file redirection to save temporary results, use pipes.

Permissions

For this exercise you will be creating lots of files and directories. You can create an empty file using the command touch.

  1. Create a directory called testpermissions
  2. Change your working directory to testpermissions
  3. Create a directory called adir.
  4. Use the command which date to find out where the date program is located.
  5. Copy the date program into adir.
  6. Change the permissions on date to remove the executable permissions.
  7. Try running it as ./date or adir/date (depending on your current working directory)
  8. Change the permissions back so that the file is executable.
  9. Create a text file using an editor in adir, then change the permissions so you are not allowed to write to it. Then change the permissions so you can't cat it either.
  10. Change your working directory to testpermissions, and then try changing the permissions on adir. What are the minimum permissions necessary for you to be able to execute adir/date? What permissions need to be set so that you cannot remove the text file you created?

Job control

  1. Start a long running process from the shell. For example, xterm or firefox.
  2. Kill it.
  3. Start it again in the background.
  4. Bring it to the foreground.
  5. Suspend it and then resume it in the background.
  6. Start another process in the background.
  7. Run jobs to see that both processes are running.
  8. Use the commands you have learned to kill them.
  9. Start a new process in the background.
  10. Use ps to find out it's process id
  11. Use kill to terminate the process.

Experiment on your own

The best way to learn the shell is to try things out and read the man pages. Your TAs and instructors will be happy to show you their favourite tricks.