This article gives a practical exposure to beginners about shell scripting on UNIX platform. Shell script is a script for the shell command-line interpreter. Shell programming is fun and is used to automate highly repetitive time-consuming manual tasks like environment setup, post processing and configuration value changes that involve file manipulation. We can run shell scripts by installing Cygwin and use Emacs as the editor.
We point to the interpreter (i.e) the shell in the very first line of the script. This is called shebang. #! bin/sh is the default shebang if nothing is mentioned.
Writing a simple Shell Script:
Step1: In the shell command prompt, navigate to your home directory
sh-3.2$ cd $HOME
cd means change directory. $HOME variable refers to a directory on the operating system containing the user’s files. It can be represented in short using ~ symbol.
Step2: In the home directory create a new file named date.sh
Step3: Modify the access rights on the file so that you can edit/save it using chmod command.
sh-3.2$ chmod 777 date.sh
Everyone has read, write and execute permissions on the file
sh-3.2$ chmod 755 date.sh
Everyone can read and execute this file but I alone should be able to modify it.
Step 4: Now let us create a simple shell script to display the date and hostname.
We store the hostname value in a variable called HOST. We use the date function to display date in the format %m-%d%-%Y. And echo command is used for displaying/printing the output on the command line.
echo “Date:$(date +”%m-%d-%Y”) Hostname:$HOST”
Step 5: Run this shell script using the command ./date.sh from the home directory where you saved it.