This site will look much better in a browser that supports web standards, but should be accessible to any browser or Internet device.

>> More on Part One >>

Week Six: Part One

Shell Scripting

My First Shell Script

Script

#/bin/sh
echo "The date and time is:"
date
echo
echo "Your username is: `whoami`"
echo
echo "Your shell is: $SHELL"
echo
echo "Your current directory is: "
pwd 

Output

-bash2.05b@gemini [~/shellscripts] $pico -w ~/shellscripts/myfirstscript.sh
-bash2.05b@gemini [~/shellscripts] $sh myfirstscript.sh
The date and time is:
Fri Jul  2 00:55:32 EDT 2004

Your username is: myusername

Your shell is: /bin/bash

Your current directory is:
/home/myusername/shellscripts

Setting Permissions

Note: In the lesson page when we get to chmod path mysteriously changes from:

~/shellscripts
to
~/unix-test/shellscripts

Error?? In Week 5 our shellscripts/ directory was to be created in our test file. When I hit the first comments in Week six about hoping it was created under the home directory and saw the path ~/shellscripts I moved it up a level with << cp >>. I am sticking there for now.

Permission set to 700

-bash2.05b@gemini [~/shellscripts] $chmod 700 myfirstscript.sh
-bash2.05b@gemini [~/shellscripts] $ls -l
total 12
-rwx------    1 myusername   myusername   609 Jan  2 11:03 extracturl
-rwx------    1 myusername   myusername  2201 Jun  3  2002 fmtlinks
-rwx------    1 myusername   myusername   160 Jul  2 00:55 myfirstscript.sh

export PATH and use << source >>

scripts05b@gemini [~/shellscripts] $export PATH=$PATH:$HOME/shell
-bash2.05b@gemini [~/shellscripts] $source ~/.bash_profile
-bash2.05b@gemini [~/shellscripts] $echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/ [line amended]
sbin:/home/myusername/shellscripts:/usr/sbin
-bash2.05b@gemini [~/shellscripts] $sh myfirstscript.sh
The date and time is:
Fri Jul  2 01:36:55 EDT 2004

Your username is: myusrname

Your shell is: /bin/bash

Your current directory is:
/home/myusrname/shellscripts

Interesting. First time I tried. I accidently typed the command with out the initial << sh >> like this:

-bash2.05b@gemini [~/shellscripts] $myfirstscript.sh

and that called the script as well. Not until we got to the next section did I realize it was supposed to have << sh >> in there. Wonder if I am in the wrong spot? It does work both ways for me though. Possibly that is because I was inside the directory where it resides?

Functions

Functions bundle a variety of commands which can be used throughout a script.

Lines startig with a hash mark [ # ] are comment lines not executed by the script.  Lines are separated by hard returns.

Before creating a function, make sure it is not already in use.

-bash2.05b@gemini [~/shellscripts] $greetings
-bash: greetings: command not found
-bash2.05b@gemini [~/shellscripts] $function greetings { echo Hi $LOGNAME ; }

When writing the function, note that there must be one blank space between the opening curly bracket and the actual command, the semi-colon is required if the closing curley bracke tis on the same line, but not if it is on a new line.

Script with greeting function and another  function created called byebye

#/bin/sh
#define function greetings
#use it to display info

function greetings { echo Hi $LOGNAME ; }

greetings
echo "The date and time is:"
date
echo
echo "Your username is: `whoami`"
echo
echo "Your shell is: $SHELL"
echo
echo "Your current directory is: "
pwd

# bye bye greeting created plus space above it

function byebye { echo Happy Trails, $LOGNAME Do come back soon ; }

echo
byebye

Output

-bash2.05b@gemini [~/shellscripts] $pico ~/shellscripts/myfirstscript.sh
-bash2.05b@gemini [~/shellscripts] $sh myfirstscript.sh
Hi myusername
The date and time is:
Fri Jul  2 04:29:18 EDT 2004

Your username is: myusername

Your shell is: /bin/bash

Your current directory is:
/home/myusername/shellscripts

Happy Trails, myusername Do come back soon

Simple Menu Script

A menu script with several functions was downloaded. I attempted a << wget >> or << ftp >> but due to the password protection on the classroom, access was denied. Opted to go the lazy route and download to hd and upload.

-bash2.05b@gemini [~/shellscripts] $sh simplemenu
Fri Jul 2 02:33:52 EDT 2004

                        My Personal Menu

                Please Select:

                         1. Directory display
                         2. Current Activity
                         3. Who is logged on
                         0. Exit
Select a menu entry by typing a number and then pressing ENTER
1
total 16
-rwx------    1 myusername   myusername        609 Jan  2 11:03 extracturl
-rwx------    1 myusername   myusername       2201 Jun  3  2002 fmtlinks
-rwx------    1 myusername   myusername        267 Jul  2 02:00 myfirstscript.sh
-rw-r--r--    1 myusername   myusername       1930 Jul  2 02:28 simplemenu
Press Enter to return to the menu
2
Fri Jul 2 02:34:07 EDT 2004

                        My Personal Menu

                Please Select:

                         1. Directory display
                         2. Current Activity
                         3. Who is logged on
                         0. Exit
Select a menu entry by typing a number and then pressing ENTER
2
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Mar21 ?        00:02:35 init
root         2     1  0 Mar21 ?        00:02:42 [keventd]
root         3     1  0 Mar21 ?        00:00:09 [ksoftirqd_CPU0]
root         4     1  0 Mar21 ?        00:00:00 [ksoftirqd_CPU1]
root         5     1  0 Mar21 ?        00:00:02 [ksoftirqd_CPU2]
root         6     1  0 Mar21 ?        00:00:00 [ksoftirqd_CPU3]
root         7     1  0 Mar21 ?        01:45:23 [kswapd]
root         8     1  0 Mar21 ?        00:01:57 [kscand/DMA]

[rest of long list deleted]

   My Personal Menu

                Please Select:

                         1. Directory display
                         2. Current Activity
                         3. Who is logged on
                         0. Exit
Select a menu entry by typing a number and then pressing ENTER
3
myusername   pts/3        Jul  2 00:22 (user-1120gs2.dsl.mindspring.com)
Press Enter to return to the menu
0
-bash2.05b@gemini [~/shellscripts] $

<< functions >> I saw in this script:

  1. amenu
  2. PressEnter
  3. DirectoryDisplay
  4. CurrentActivity
  5. WhoIsLoggedOn

>> More on Part One >>