Process Lab: Processes and Variables

NAME:____________________________________________________________________

User-defined variables are local to the shell or process in which they are set.

If you don't give export an argument,  you will see the variables that are already exported.
$ export

How many lines of output did the export command produce? __________

What command did you use to count them? _________________________

Shell Scripts Creating a Simple Shell Script

Create a simple shell script which returns the date. This can be done without an editor by using the cat command and redirecting output to a file.

$ cat > datescript
date
<Ctrl>d
Invoking Shell Scripts

There are several ways to execute a script.

===> 1. Use a shell to run the command datescript

===> 2. Make the shell script executable and run it directly
$ chmod 755 datescript

The permission characters would look like this:
    -rwxr-xr-x
    /   |   \
owner group other

  • Users must have both read and execute permission on the script file to invoke it like this:
    $ datescript
    bash: datescript: command not found

    Why do you get the "command not found" error message?

    __________________________________________________________________________

    What is the definition of your current path?
    $ echo $PATH

    __________________________________________________________________________

  • Is the current directory in your path? __________________________
    $ pwd

    Preceed your command with a ./ to tell bash to look in the current directory.

    $ ./datescript
    Tue Oct 16 17:55:23 PDT 2001

    ===> 3. Use the dot (.) or source Command
    Execute the script the normal way and notice the output:
    $ ./datescript

    What is the value of the today variable? _____________________________________

    Check the value of today in your current shell. Remember it was set in the script.
    $ echo $today

    Does the current shell have a value defined for today?____________________

    Why or why not? _____________________________________________________

    ___________________________________________________________________

    Return Codes from Commands

    After a command has completed (or attempted to complete) it sends a return code (or exit code) to the parent process.

    Change to the directory above your current directory.
    $ cd ..

    Now attempt to execute datascript even though it doesn't exist in the current directory.
    $ ./datescript

    What is the return code?____________

    What do you think the two different return codes mean?_______________________________________________________

    _____________________________________________________________