2014-01-09

shell script:- using linux command in scripting

Pranaam to all bhai ji _/\_
in this tutorial we will study about , how to use Linux commands in shell script .
as i mentioned in first tutorial that basically shell script is a file contains linux commands which start executing one by one when we run file .
so we are going to discuss , how to define commands in shell script
before proceeding to it , let me tell something about quote symbol meaning in shell script, which we should know :P

Double quote  "   if we write anything and enclose it in double quotes , it will be consider as constant (other                                then \ and $)
single quote    '    whatever we enclose in single quotes, its meaning will remain same

Back quote  `     we use back quote to tell our shell script that the value enclosed in back quotes is a                                       command, please execute it :P .

so one thing is clear, we will use back quotes for defining and execution of commands
how ???? lets start :)
i am going to run command whoami using shell script
and wrote this code in cmd.sh file

echo -e " pranaam bhai ji _/\_, i am going to run command whoami \n"

echo `whoami`

echo -e "\n bye \n"

here line 1 and 3 are just for printing text where as line 2 is our main line which will execute command  whoami
echo `whoami`  , as we know keyword enclosed in back quotes are treated as command in shell script execution


this is how we run single command in shell script
lets run multiple commands ;)
this time i am using command whoami , pwd and ls -l

code is

echo -e "\n we are executing more then one command  \n"

echo -e "i am user  `whoami` and my present working directory is `pwd` \n"

echo -e "\n files in my directory are \n  `ls -l` \n"


here command
whoami will print the username with which we are logged in
pwd will print the full path of present working directory
ls -l  will list all the files with its info(like owner, permissions, size of file ) other then hidden files .

lets see, save it and run


script is working fine and output is same as we expected ^_^

So this is how we use commands in shell script .
we can define command dynamically too . how ??? its easy :)
if you know how to define dynamic variables , same thing with little change
for defining dynamic variable we use read function , store the value of user input in a variable and the we can use it as command
lets start
code is

echo -e "\n this script will accept user command at run time   \n"

echo -e "type the command which you want to execute, i am waiting for your inout ..... \n"
read  cmd

echo -e "\n command entered by you is $cmd and its out put is given below \n"

echo `$cmd`


this script will ask to user for command which he want to run, read function will take input from user and will store it in variable cmd (line number 3)
line number 4 will print the command that we entered
line number 5 will execute the command which we entered , because $cmd contains the value which we entered by terminal and $cmd is enclosed in back quotes which will treat that input as command and will try to execute it  :)

script is waiting for input command


i want to run command whoami , typed command and press enter


script is working fine ;)
this is how we can define Linux command in shell script :)
Thank you

Greetz to :- Guru ji Zero , code breaker ica, Aasim shaikh,Reborn, Raman kumar rana,INX_r0ot,Darkwolf indishell, Chinmay Pandya,L0rd Crus4d3r,Hackuin ,Silent poison India,Magnum sniper,Atul Dwivedi,ethicalnoob Indishell,Local root indishell,Irfninja indishell Hardeep bhai,Mannu,Viki and AR AR bhai ji <3




Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

© 2009 Start With Linux | Mannu Linux
Designed by cyb3r.gladiat0r
Posts RSSComments RSS
Back to top