How to write a Shell Script in Unix

 

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.

  1. 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:

Ctrl +c

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:

vi FirstShellScript.sh

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. PressEsc‘, 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:

  ./FirstShellScript.sh

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:

Ctrl+c

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.

 

 

Advertisements

About Swapratim

Blog Writer in Medium for Chatbots Magazine - world's biggest chatbot magazine. Also he writes in Bot Publication and Chatbots Journal on regular basis (https://medium.com/@swapratimroy). Experienced in IT development & support pursuing different roles & responsibilities. He has worked on several innovative projects. Android apps developed by him – In Time SMS, AndroLogic which are freely available in Google Play Store. He has several in house tools built available for free usage on internet. Apart from this, he likes to share his learning with everyone which he had learned over years. Sole purpose of this blog is to share the author's knowledge in IT, passion for travel and recent trends with you. Hope you will enjoy his contents.
This entry was posted in Various Scripting Tutorials. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s