Posts which you will also like.

Thursday, July 19, 2012

Creating and Running a Linux Script file

Scripting is very powerful feature of Linux.It helps the administrator of Linux to automate the daily routine task so it is recommended to learn scripting for Linux administrators.Shell scripts are much more like MS DOS batch file.Any way I am not making you a Linux admin but in any case if you learn the scripting you will feel its benefits by yourself.
In this blog post I will describe the various methods to run a Linux script file.
A Linux script file contains the scripting commands written in it and this file has “.sh” extension(not mandatory).We can run this file in four ways.I will discuss them one by one.
1.<shell name> <script-name>
bash myscript
ksh myscript
In this case a new shell(bash or korn for the above case) will be spawned from the previous parent shell and your script will in the child shell.
2. ./<script-name>
In this case also a new shell is spawned but in this case first we will have to change the file permission of the script file to be executable using chmod command then we can run.
e.g chmod +x myscript or chmod 755 myscript
3.  . <script-name>
In this case new shell will not be spawned neither we have to set the file permission for the script.
4. Self executable script file:
In this type of run we just give the name of script and it will run automatically i.e self executable.
but for this first line of your script should contain:

#!/bin/bash   (or path of any shell)


If you do not know the path of shells or the no of shells present in your system type:

cat /etc/shells

It will show the required information.

One more thing if you want to know whether new shell was created or not you can execute the

following command at terminal before executing a shell script and after executing the shell script:

echo $$

It will show the PID of the current shell.

No comments:

Post a Comment

Your comment may wait for moderation.... Protected by Copyscape Online Plagiarism Tool