The first script that we will use will be a simple script that will output the text “Hello Unix”. I would suggest using vi to create and edit the file. To create and edit the file, run the following command:
If you don’t know how to use vi, I would suggest that you read a quick tutorial by searching for “vi tutorial” with the search bar above. To enter input mode in vi, press “i”. Now, for the code in this first script, enter the following code in the file:
#!/bin/sh # This is my first script. echo "Hello Unix"
Now save the file and close it by hitting Escape followed by “:wq” and Return. The first line of the file tells unix which shell to use to execute the file. /bin/sh is the default location for the bourne shell. In Linux this will normally point to the bourne again shell, which is a remake of the original unix shell and works pretty much the same. The second line of the file is just a simple comment. Comments are ignored by the shell interpreter but are very useful when developing large and complex scripts. Everyone forgets what their original logic or intention was when coding a script and it’s much easier to read a comment than it is to try to understand large and complex sections of code. Before we can run this script, we must first make it executable. To do this we will use the unix chmod command:
chmod u+x hello.sh
Now our script is executable. This command basically tells unix to set the x (executable) flag for the user level access of the file. Now we are able to run the file. If you don’t have “.” in your unix PATH environment variable, then you will need to proceed the name of the script with “./” to execute it. It is generally considered to be a security risk to put “.” in your PATH evironment variable, so we will assume that you don’t have it. Now you can execute your script by using the following command:
You will see the text “Hello Unix” output to the console, congratulations, you have created your first unix script! Now you can move on to the next topic where we will discuss using variables in a unix script.