Basic Shell Scripting-step by step
Basic Shell Scripting-step by step
If you are not a beginner go to summery page
What is a script?
Group of commands in one file for a specific or a number of actions to achieve a result
1. Select your editor
You must have a text editor to write shell programs
Select an editor in which you are comfortable
Most common editors are Vi and Emacs
2. Structure of a script
a)First line
“#!/path/to/shell”
b) Comments
Purpose of script, author, created date, modifications, bug fix notes etc
c) Body of script
Commands to be executed
This line will decide which shell to be used for executing the commands in script
If the line is not present, system will use default shell
E.g.:- 1
------------------------------------
#!/bin/bash
echo “Hello world”
E.g. Explained
------------------------------------
The above script will execute
echo “Hello world”
in bash shell
3. Execute a script
If you logged in as root, the home directory will be /root/
We will create a file in /root using vi editor
[root@localhost]# vi hello.sh
The files have an extension of “.sh” so that others can easily identify the file as script
-------------------------------------------------------------------------------------------------------------------------------
Note: - The “file” is a command used to identify, what kind of file it is.
Suppose we have file with name “abcd” How can I say it is a shell script or an executable binary or else another type..?
Here we will use “file” command
Eg:-
[root@localhost]# file abcd
ASCII Long line text
-------------------------------------------------------------------------------------------------------------------------------
We are in vi now (I’m going ahead with the assumption that you have basic knowledge of VI editor)
Type the text mentioned in e.g.: 1
#!/bin/bash
echo “Hello world”
Save the file
Now we have created a” bash shell script” named as “file.sh”
To execute a file that need execute permission
So give execute permission to “file.sh” using chmod command
[root@localhost]# chmod 755 file.sh
Now we can execute the script as
[root@localhost#]./file.sh
Hello world
[root@localhost#]
Summary
1. Create a script that will print “Hello world”
[root@localhost#]
[root@localhost#]
[root@localhost#]vi file.sh
#!/bin/bash
echo “Hello world”
[root@localhost#]chmod 755 file.sh
[root@localhost#]./file.sh
Hello world
[root@localhost#]
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks and keep rocking.
ReplyDeleteMobile Testing Training in Chennai
Mobile Testing Training in Tambaram
Mobile Testing Training in Adyar
Manual Testing Training in Chennai
Manual Testing courses in Chennai
Manual Testing Course
Mobile Testing Training in Chennai
Mobile Testing Training
The article is so informative. This is more helpful for our
ReplyDeletebest software testing training in chennai
best software testing training institute in chennai with placement
software testing training
courses
software testing training and placement
software testing training online
software testing class
software testing classes in chennai
best software testing courses in chennai
automation testing courses in chennai
Thanks for sharing.