Learning Unix can be a fun if you start writing Shell script. If you are working on Linux, HP-UX, Solaris, Tru64 or other flavors of Unix – being a good administrator or a middleware or database administrator – you should have a basic knowledge of shell scripting to play around with your environment. For a beginner, you should have some basic Unix commands mapped in your brain. But don’t worry, we’ll learn them during this tutorial.
- If you are using putty to login into Unix environment, then open it with the server name and login with your username and password. You can download the Putty from internet.
It is free to download: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
If you have Reflection-X installed in your system, you can use that for login into the server.
2. Once logged into, you will land into your home directory (/home/’username’) if you are logging as user. In case you are a root user, you will land into /root directory. Check your current directory by writing command – pwd.
3. Now you are ready to create the first shell script. Type:
cat > FirstShellScript.sh <this will create a file named FirstShellScript.sh>
It will open bash command prompt to enter more input. Type:
Your first shell script has been created. But it is empty now. Let’s put some language there to compile.
4. Open the shell extended file in Vi editor. Type:
5. Write the following lines: Press ‘i’ in vi editor to insert lines and write the following lines. The first line is called Shebang which interprets which shell prompt it is (bash/ksh etc):
#!/bin/bash echo 'Say Hello World to all!!!' exit
6. Press ‘Esc‘, then press :wq! (that will save and exit from vi editor).
Now put necessary permission to the file. Write:
chmod 755 FirstShellScript.sh
7. That’s it. Technically you have finished you first Shell Script. It’s the sweet time to taste the flavor. Lets run your first shell script. Run FirstShellScript.sh from the same directory where you have created it:
Celebrate the first success with Champagne. In between, some shortcuts to write shell script:
1. Create a shell script file and write the code directly within the input. It’ll save the time to use vi editor.
cat > FirstShellScript.sh #!/bin/bash echo 'Say Hello World to all!!!' exit
Get out of the prompt to save the file, type:
Now provide permission and run it quickly.
Have some play around with shell script to grab its control a little more. It has enormous power to control and automate your environment. Play safe with the daemon. Have fun.