Linux Shell Scripting
Linux Shell Scripting
Linux Shell Scripting
Contents
more
Email Print Favorite
Variables in Linux
Sometimes to process our data/information, it must be kept in
computers RAM memory. RAM memory is divided into small locations,
and each location had unique number called memory location/address,
which is used to hold our data. Programmer can give a unique name to
this
You can see system variables by giving command like $ set, Some of
the important System variables are
$ echo $USERNAME
$ echo $HOME
Do not modify System variable this can some time create problems.
Syntax: variablename=value
$ nu=10 # this is ok
$ vech=Truck
one or more Alphanumeric character. For e.g. Valid shell variable are
as follows
HOME
SYSTEM_VERSION
vech
no
(2) Don't put spaces on either side of the equal sign when assigning
value to variable. For e.g.. In following variable declaration there will
be no error
$ nu=10
$ nu =10
$ nu= 10
$ nuo = 10
(3) Variables are case-sensitive, just like filename in Linux. For e.g.
$ nu=10
$ Nu=11
$ NU=20
$ nU=2
Above all are different variable name, so to print value 20 we have to
use $ echo $NO and Not any of the following
(4) You can define NULL variable as follows (NULL variable is variable
which has no value at the time of definition) For e.g.
$ vech=
$ vech=""
Try to print it's value $ echo $vech , Here nothing will be shown
because variable has no value i.e.
NULL variable.
Syntax: $variablename
$ echo $vech
Caution: Do not try $ echo vech It will print vech instead its
value 'Truck' and $ echo n, It will print
$ x=10
$ echo $x
$ xn=Rani
$ echo $xn
$ echo 6 + 3
Where, op1 and op2 are any Integer Number (Number without
decimal point) and operator can be
+ Addition
- Subtraction
/ Division
integer calculation)
\* Multiplication
$ expr 6 + 3
$ expr 6+3
$x=20
$ y=5
$ expr x / y
$ x=20
$ y=5
$ z=`expr x / y`
$ echo $z
are using cat command you can use any of the above text
editor. First type following vi command with filename
myfirst.sh ( It is better to add an extention to the script file so
that you can easily identify those files)
$ vi myfirst.sh
Chmod +x myfirst.sh
./myfirst.sh
Syntax: ./your-shell-program-name
For e.g.
$ ./myfirst.sh
For e.g.
$ bash myfirst.sh
command
$ /bin/sh /home/manoj/myfirst.sh
Now every time you have to give all this detailed as you work
in other directory, this take time and you have to remember
complete path. There is another way, if you notice that all of
our programs (in form of executable files) are marked as
executable and can be directly executed from prompt from any
directory (To see executables of our normal program give
command $ ls -l /bin or ls –l /usr/bin) by typing command like
$ bc
$ cc myprg.c
$ cal
etc, How this happed? All our executables files are installed in
directory called /bin and /bin directory is set in your PATH
setting, Now when you type name of any command at $
prompt, what shell do is it first look that command in its
internal part (called as internal command, which is part of
Shell itself, and always available to execute, since they do not
need extra executable file), if found as internal command shell
will execute it, If not found It will look for current directory, if
found shell will execute command from current directory, if not
found, then Shell will Look PATH setting, and try to find our
requested commands executable file in all of the directories
mentioned in PATH settings, if found it will execute it,
otherwise it will give message "bash: xxxx :command not
found", Still
there is one question remain can I run my shell script same as
these executables. Yes you can, for this purpose create bin
directory in your home directory and then copy your tested
version of shell script to this bin directory. After this you can
run you script as executable file without using $ ./shell
$ cd
$ mkdir bin
$ cp first ~/bin
$ first
$ mkdir bin
Now created bin directory, to install your own shell script, so that script can be run as
independent program or can be accessed from any directory
$ cp first ~/bin copy your script 'first' to your bin directory $ first Test whether script is
running or not (It will run) In shell script comment is given with #
character. This comments are ignored by your shell. Comments
are used to indicate use of script or person who
creates/maintained script, or for some programming
explanation etc. Remember always set Execute permission for
you script.
Options
-n Do not output the trailing new line.
\a alert (bell)
\b backspace
\n new line
\r carriage return
\t horizontal tab
\\ backslash
\ and $).
For eg.
$ echo "Today is date"
Now it will print today's date as, Today is Tue Jan ....,See the
`date` statement uses back quote,
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ echo `expr 6 + 3`
$ ls grate_stories_of
Here the name of command is tail, and the arguments are +10
and myf.
Now try to determine command and arguments from following
ommands:
$ ls foo
$ cp y y.bak
$ mv y.bak y.okay
$ mail raj
$ sort -r -n myf
$ date
$ clear
ls 1 foo
cp 2 y and y.bak
mail 1 raj
date 0
clear 0
$ rm {file-name}
$ ls -a /*
myshell it is $0
foo it is $1
bar it is $2
$# Number of argument
specified in command line
(5)Exit Status
$ rm unknow1file
$ ls
$ echo $?
$ expr 1 + 3
$ echo $?
$ echo Welcome
$ echo $?
$ wildwest canwork?
$ echo $?
$ date
$ echo $?
$ echon $?