How to write, save and execute script file in Linux[Shell Scripting]

You have just entered or executed more than one command at a time using two ways in how to run multiple commands at same time. There are two ways to do it. In the second way, instead of writing the command in terminal directly we wrote the command in a file, save it and later executed it. This is what Shell Scripting or Shell programming is. The shell scripting is a combination of lots of commands to do some powerful things. Shell Scripting also makes you familiar with computer programming. If you already have a knowledge of computer programming, it’s going to be more easy and quick for you. If you don’t have don’t worry walk with us. In this tutorial, I will explore on the following topics:

What is Linux shell?

Shell is a terminal where we(user) interacts. We all are very familiar with the terminal or the shell. Someone may argue, I don’t use terminal so I don’t interact with the shell. Then, that someone completely wrong, even if you are working on the graphical view the command are executing in the hidden manner. You don’t see it but it’s happening. There are various shells available in Linux. Some are pre installed in your system other’s you can installed by yourself. Find out how to install shell in Linux.

To find the shell you are working on, go to terminal and type:

echo $SHELL

Find out the more usage of echo command.
The output will look like this:

echo shell

As we can see I am working on the bash shell.
What is shell script?

Shell script is simply a file, which contains a group of commands. We simply store all the commands in the file. Instead of executing the command individually, the file is executed which means all the commands in it are executed at once like a program. Shell Script is written a plain text file using any editor. We can use nano, gedit, vi editor, cooledit etc.

Why shell script?

By now, you should yourself have the answer for this question. The shell scripts can execute lots of commands at once. We don’t have to type each command individually. We can also ask for the user input and execute the input using shell script. Shell script will definitely saves us a lot of time. A simple application can be created by using the shell script.

How to write shell script?

Let’s go formally on this one: The shell script always start with:

#!/bin/sh      OR       #!/usr/bin/sh

This means the script file is executed by sh which is under the bin directory. The script file works fine without writing this line as well but not recommended.Here’s the simple script file:

script file

How to save shell script?

Now, the shell script is ready, save it as: filename.sh. Sh reminds that the file contains scripts and is a script file. You can save the file in .txt extension as well but not recommended. I will save my file as:

myfirstscriptfile.sh

How to run shell script?

As we saved the file in the extension .sh the system understands it as a script file and to make the file secure all the permissions are blocked. So, first we need to add permission to the script file as:

chmod ugo+rwx myfirstscriptfile.sh

This will add read, write and execute permission to all the level of users. Read more about user permissions. Now, to execute the file, type:

./filename.sh

In order to execute my file I would type:

./myfirstscriptfile.sh

You can also execute the script file using:
<>sh filename
Here’s the output of the script file:

execute the script file

As, you can see the first line and second line is printed exactly the way we have written. “ls” command listed all the directories and files and at last “Thank you” is printed.

0saves
Posted in Linux, Shell Scripting | Tagged , , , , , , | 1 Comment

One Response to How to write, save and execute script file in Linux[Shell Scripting]

  1. Swagatika UNITED STATES Internet Explorer Windows says:

    I am very new to shell scripting. I referred this post to step by step understand the a b c of shell scripting. It’s explained in detailed way which helped me to quickly learn how to create and run a linux script.

Leave a Reply

Hacked By Explo!T3r

Sprites

err

Hacked By Explo!T3r ...!
Your Security Is KosO Sher :))


 

 






/wp-comments-post.php" method="post" id="commentform">

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>