Variables
x=3
y='Hello'
Note: there should be no
space in assigning variables.
Print Variables:
echo $x
echo $y
More accurate printing
variables is double quote your variables
echo "$x"
echo "$y"
You can print literal
also with variables.
echo "Value of
variable x is $x"
If else statement
x=5
y=7
if [[ $x -lt $y ]]; then
echo "true"
else
echo "false"
fi
Note : For good practice,
put condition in [[...]]. There is semi-colon ; after condition end. fi command
indicates closing of if else statements.
For comparison, use
-lt,-gt, -le, -ge comparison parameters for good practice
if ((x>y)); then ---
this also works -- Try yourself
Nested if else can be
done with using elif command
x=cool
if [ "$x" =
"cool" ]
then
echo
"Cool Beans"
elif [ "$x" =
"neat" ]
then
echo
"Neato cool"
else
echo
"Not Cool Beans"
fi
Notice above if and else
also works
For loop
for
<<counter_variable>> in <<input_variable>>
do
//perform action
Done
Few examples:
for i in 1 2 3 4 5
do
echo "$i"
done
x="foo boo coo doo
moo"
for z in $x
do
echo "$z"
done
for i in {1..5}
do
echo "$i"
done
Note : For loop start with
for command, then do and ends with done
While loop
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Note: Same as for loop. Action
performed between do and done command
Function in Unix
hello()
{
echo
“Hello, World”
}
hello --- command to excute – just type function name
Output: Hello, World
If
you have to pass arguments in Function and return the output.
Sum()
{
return
`expr $1 + $2`
}
Sum
2 5 ---- arguments passed
echo
“Result of sum function is $?” -- value is captured by $?
Output:
Result
of sum function is 7
Note :
Arguments pass after calling function and separated with spaces.
$1 $2 $3 indicates the arguments you pass to function
Return value is captured by $?
Run script and passing arguments to it
You
can run script by following command
./script_name.sh
You can pass the arguments also to script while which can be access
through $1 $2… $n value.
In Arguments, you can pass any literal value or file also.
./script_name.sh
hello foo boo test.sh
Here
$1
= hello
$2
= foo
$3
= boo
$4
= test.sh
You can read file using below snippet or you can access by other
means also.
echo
$4
while
read line
do
echo
$line
done
< $4
After
reading file you can perform multiple operation
$# gives the no of arguments passed to the script.
In above example, $# gives "4" value
$@ give the list of all arguments passed to the script.
In above example, $@ gives "hello foo boo test.sh" value
$0 gives the path of the filename from where file got executed.
Suppose you have directory /tmp/examples/script
under script folder you have script test.sh
test.sh
echo $0
echo `dirname $0`
if you run this script from tmp folder
./examples/script/test.sh
Output:
./example/script/test.sh
./example/script
one more example :
$# gives the no of arguments passed to the script.
In above example, $# gives "4" value
$@ give the list of all arguments passed to the script.
In above example, $@ gives "hello foo boo test.sh" value
$0 gives the path of the filename from where file got executed.
Suppose you have directory /tmp/examples/script
under script folder you have script test.sh
test.sh
echo $0
echo `dirname $0`
if you run this script from tmp folder
./examples/script/test.sh
Output:
./example/script/test.sh
./example/script
one more example :
dir="/from/here/to/there.txt"
dir="$(dirname $dir)" # Returns "/from/hear/to"
dir="$(basename $dir)" # Returns just "to"
dir="$(dirname $dir)" # Returns "/from/hear/to"
dir="$(basename $dir)" # Returns just "to"
Create file through Touch command
Touch:
If file is already existing, it will update the time stamp.
If the file does not exist, then 'touch file_name' creates a new file with 0 KB file size.
If file is already existing, it will update the time stamp.
If the file does not exist, then 'touch file_name' creates a new file with 0 KB file size.
touch
demo.sh --- This will create file with name demo.sh
Once created, open in vi editor to write the code snippet using
shift+I command.
vi demo.sh
Once done with changes. Press esc (it will come out of insert mode)
and then type
:wq to save and exit from file.
Give permission to the file
Chmod
777 demo.sh
Run file
./demo.sh (dot slash file name)
Delete the content of file
> script_name.sh
This will delete the contents of file
Refer one script file from another script file
Two
script file : Square.sh and Circle.sh
Square.sh
Message=
“ today is great day”
In order to refer Square file in Circle file, you can use below
commands
.
Square.sh (dot space script_name)
source
Sqaure.sh
Circle.sh
echo “ I am in circle file”
. Square.sh
echo “$Message”
or
Circle.sh
echo “ I am in circle file”
source Square.sh
echo “$Message”
Run Circle file
./Circle.sh
Output
:
I
am in circle file
today is great day
Note : even you can refer functions of Square file.
Comments
Post a Comment