User:Pegeraki/HB Shell

From WolfTech
Jump to navigation Jump to search

WolfTech Webteam Handbook

Shell Scripts

A shell script is a file that contains a series of actions for a Unix/Linux system to perform, much like a BAT file for Windows. Shell scripts are almost always run from the command line and basic shell scripts use normal filesystem commands in order to generate their output.

For a list of all the filesystem commands that may be helpful to you while working with shell scripts in a Linux environment, vist http://www.ss64.com/bash.

Let's take a quick look at a basic shell script. First, open a Linux console session (you may need to use PuTTy to connect via SSH to a Linux server) and open your favorite Linux text editor. If you have no preference, use:

unity% pico -w test.sh

Note: The unity% prompt will already be there, so you will never need to type it in.

This will open a blank text document called test.sh using the Pico/Nano text editor. Though the .sh extension isn't necessary, it will allow us to keep track of our shell script much easier.

In your text editor, type the following:

echo "Hellow World";
pwd

When you are done with that, press Ctrl+X to exit. Pico will ask if you want to save the changes; press Y for yes and confirm the file name. Now that we have our script made, we need to give it propper permissions in order to run. This is done with the chmod command. Type the following at the promt:

unity% chmod 755 test.sh

Now it's time to run the xcript. At the console type the following:

unity% ./test.sh

The output of the script should look similar to:

Hello World
/afs/unity.ncsu.edu/users/a/abstein2

In your script the /a/abstein2 should become the first letter of your Unity ID and then your full Unity ID. This line is actually the current (pwd=present working directory) directory that the test.sh file is in, which is what pwd outputs.

Another important thing to understand when working with shell scripts is how to redicret output. A good source of information on this can be found at: http://www.linuxsa.org.au/tips/io-redirection.html