How to start shell script writing


This is actually, I want to share, how I learned the shell scripting. It may be helpful for beginners. I am writing it step by step so that it will easy to understand:

STEP 1: Do your task manually & prepare the steps.

If you know the basic Linux commands, it will help you to write a shell script. I am not writing the basic Linux commands here, you can see check here http://www.comptechdoc.org/os/linux/usersguide/linux_ugbasics.html or you can Google it also.

Now you know some basic Linux commands.

Remember a thing that the commands you can also find in internet by googling or by searching in any other website. But it is important to remember the steps & logic of doing your task.

For example: If you need to copy some text files from one directory to other directory. Then divide your task in steps. Like:

1.       Create the directory where you want to copy your text files.

2.       Go to the directory where the text file exists.

3.       Copy the text files from that directory to new directory.

You need three commands there, first to create a new directory, second change your current directory & third copy text files to new directory. If you know the commands than good but if you don’t know then no need to worry, search it on Google, it will definitely give you required commands.

Ok, I am writing the commands here to complete above task:

1.       Create the directory where you want to copy your text files.

mkdir -p /home/rahul/newdir

2.       Go to the directory where the text file exists.

cd /home/rahul/olddir

3.       Copy the text files from that directory to new directory.

cp *.txt /home/Rahul/newdir

After third command, your task is completed & you wrote all the steps of doing it.

STEP 2: Combine your commands to do your task in one command.

It means the above task can be done in one command by combining your three commands. You can combine your commands as follows:

mkdir -p /home/rahul/newdir; cd /home/rahul/olddir; cp *.txt /home/Rahul/newdir

or

mkdir -p /home/rahul/newdir && cd /home/rahul/olddir  && cp *.txt /home/Rahul/newdir

You can see the “;” or “&&” are used as command separator. If semicolon “;” is used as a command separator, it will allow all the given commands to execute even if the command is failed or passed. But if “&&” is used, it allow the next command only if the first command is passed.

That means if “newdir” is not created, “;” will change the directory & try to copy text files in the “newdir” which does not exist. But in case of “&&”, it will not even try to change the directory.

Now you learned that how to run a command individually & how to run it as a combined command. But the most important thing is to remember your steps to do your task.

STEP 3: Start writing a script file.

I try it with a simple way. I just write all the commands, to complete a task, in a file. For example: Open a file with any text editor like vim or gedit, write your commands line by line.

You can see all the written commands in attached screenshot & above every command you can also see the line started with hash#”. It denotes that the line is commented that means it will not executed & used to write the information of the command. Start the use of “#” from first day & make it as habit while writing a script, it will help you to remember a command.

You can write in file in the same way, you give the commands on terminal.

Now your first script is ready, save it with name “myscript” by using “<ESC> :wq” in vim & by File->Save in gedit.

STEP 4: Execute your script.

To execute a script, give the command

sh myscript

You can include this “sh” in your shell script by starting your shell script with “#!/bin/sh”.

That means it will use /bin/sh to execute the commands written in the shell script. After this change, you can execute your shell script by using its absolute path, like a new command.

/home/rahul/myscripts

It will execute all the three commands written in the script one by one like in the semicolon “;” example written in STEP 2.

It is very basic, but I think it is enough to start your shell script writing. Make it as a habit to execute your commands by using scripts instead of using individual command.

I will definitely write its other part.

Please share it with others, if you like it………

Advertisements

7 thoughts on “How to start shell script writing

  1. Pingback: How to start shell script writing « Linux Explore

  2. Pingback: How to start shell script writing « Brain Knowledge

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s