Shell Processes   «Prev  Next»

Unix Path Variable - Exercise

Script Names and PATH Variable

Objective:Observe how the value of PATH affects which command is run

Exercise Scoring:

This exercise is autograded and worth 2 points.

Instructions

In this exercise you will use script names that will cause problems when creating your own scripts. This will show you what happens when you do this, so that you will not do it in the future. You will log into your shell account, create a script, give the script various names, and run it.

1) log into your UNIX account.
  1. Create a sample script by performing the following actions.
  2. Create a file called ls. This will be your script.

vi ls
  1. Your script should look like the code below. Type in this script using vi.

#! /bin/sh
echo "This is my ls script"
  1. Change permissions[[the chmod command]] on your script so that you can run it
    Do people know that command yet? If not, I think we should briefly explain how/why it is being used?
  2. This is a basics of UNIX command. I will insert a sidebar here to clarify this one command.
chmod +x ls
  1. Try running your script.

ls

Do you see the problem here? The shell locates the regular ls command in your PATH before it looks in your current directory to find your script. Your script is not run.
  1. Rename the script and
    Rename itanything you want?
    
  2. it again.

mv ls myscript
myscript

It should run with no problems.
  1. Another problem you may encounter is that your PATH variable does not contain the current directory (the . directory). Let us try changing PATH to see the problem this can cause.

a) Change the PATH variable to include only the /usr/bin directory, then try running your script.
PATH=/usr/bin
myscript

Your shell will not be able to locate your script because your current directory is missing from your PATH.
b) Reset the PATH to include the . directory.
<pre>
PATH=/usr/bin:. myscript<pre>

Your script will run again.
  1. Logout of your account and then login again. This will rerun your startup files and reset the PATH to its original value.

Submitting your exercise


This exercise is auto-scored; when you complete the exercise, click the Submit button to receive full credit.