shell-scripts

Shell-Scripting:

First look how to print the text using script

[root@shellscripting ~]# touch one
[root@shellscripting ~]# cat > one
echo “Hello every one Thanks for reading”
[root@shellscripting ~]# sh one 
Hello every one Thanks for reading
[root@shellscripting ~]# bash one
Hello every one Thanks for reading

—————————————————————————————————–

[root@shellscripting ~]# touch second
[root@shellscripting ~]#
[root@shellscripting ~]# cat >> second 
#!/bin/sh
this will the sample script with any setof rules

[root@shellscripting ~]# sh second 
second: line 2: this: command not found
[root@shellscripting ~]# cat second
#!/bin/sh
this will the sample script with out fallowing any set of rules

so we are getting second: line 2: this: command not found

[root@shellscripting ~]# vim second 

use echo as a command to display the output

[root@shellscripting ~]# cat second 
#!/bin/sh
echo this will the sample script with  set of rules

[root@shellscripting ~]# sh second
this will the sample script with any set of rules

so i hope u understood what is a rule ,so i shell script all rules must be fallowed in order to make proper script

—————————————————————————————————–
[root@shellscripting ~]# cat firstshellscript 
#!/bin/bash
echo “hello this the first shell script program hope u will understand”
echo “The first line is called as magic line or shebang i.e., “#!/bin/bash” as per the knowledge what we have if any line is started with # it will be treated as comment but if it precedding with #!/bin/bash or #/bin/sh or #!/bin/csh i i will demot e that the file is a script file and
it will it executed in that particular shell as per requirement “
echo ” in this case i would like to run the script in a bash shell so i am using #!/bin/bash”
echo “hope u understood”
echo ” scripting id nothing but a group of commands”
echo ” i would like to print some output of some basic command”
pwd
who
cal
echo “file should be given execute permission in order to run the script file & .sh as an extension for file not mandatory”
echo “use chmod +x filename”
echo ” for running script u can use sh filename or bash filename or ./ filename”
[root@shellscripting ~]# chmod +x firstshellscript 
 
[root@shellscripting ~]# ls -lh firstshellscript 
-rwxr-xr-x. 1 root root 907 Mar 21 08:21 firstshellscript
[root@shellscripting ~]# sh firstshellscript 
hello this the first shell script program hope u will understand
The first line is called as magic line i.e., #!/bin/bash as per the knowledge what we have if any line is started with # it will be treated as comment but if it precedding with #!/bin/bash or #/bin/sh or #!/bin/csh i i will demot e that the file is a script file and it will it executed in that particular shell as per requirement
in this case i would like to run the script in a bash shell so i am using #!/bin/bash
hope u understood
scripting id nothing but a group of commands
i would like to print some output of some basic command
/root
root     pts/0        2016-03-21 08:05 (172.16.28.1)
March 2016
Su Mo Tu We Th Fr Sa
1  2  3  4  5
6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

file should be given execute permission in order to run the script file & .sh as an extension for file not mandatory
use chmod +x filename
for running script u can use sh filename or bash filename or ./ filename

—————————————————————————————-
usage of   read in shell scripting

[root@shellscripting ~]# touch third
[root@shellscripting ~]# cat > third 
echo “enter the name”
read my_name
echo “Hello $my_name,How are u”

[root@shellscripting ~]# sh third 
enter the name
shaik mohammed rafi
Hello shaik mohammed rafi,How are u

—————————————————————————————————————-

                                                       Positional Parameters

[root@shellscripting ~]# x=123
[root@shellscripting ~]# echo $x
123
[root@shellscripting ~]# x=”I want to change this”
[root@shellscripting ~]# echo $x
I want to change this
—————————————————————————————————-
                              Renaming a file using position parameter

[root@shellscripting ~]# touch test
[root@shellscripting ~]# cat > test 
i  want to rename the file using this script
[root@shellscripting ~]# touch renaming
[root@shellscripting ~]# cat > renaming 
mv $1 $2
cat $2
[root@shellscripting ~]# cat test1
cat: test1: No such file or directory

[root@shellscripting ~]# sh renaming test test1
i  want to rename the file using this script

[root@shellscripting ~]# cat test1 
i  want to rename the file using this script

[root@shellscripting ~]# cat test

cat: test: No such file or directory
———————————————————————————————————————
                           Changing permissions with Position Parameters

[root@shellscripting ~]# touch ch_permission

[root@shellscripting ~]# cat >> ch_permission 
ls -l $1
echo “permission before changes”
chmod 777 $1
ls -l $1
echo “permissions After changes”

[root@shellscripting ~]# sh ch_permission test1 
-rw-r–r–. 1 root root 45 Mar 21 10:17 test1
permission before changes
-rwxrwxrwx. 1 root root 45 Mar 21 10:17 test1
permissions After changes
—————————————————————————————————————-
                               setting the parameters with set command       
                         
[root@shellscripting ~]# set shell scripting is a good language
[root@shellscripting ~]# echo $1
shell
[root@shellscripting ~]# echo $2
scripting
[root@shellscripting ~]# echo $3
is
[root@shellscripting ~]# echo $4
a
[root@shellscripting ~]# echo $5
good
[root@shellscripting ~]# echo $6
language
[root@shellscripting ~]# echo $7

[root@shellscripting ~]#

[root@shellscripting ~]# echo $*
shell scripting is a good language

——————————————————————————————–  –

                         Reverse Quotes Or Accent Graves
In order to use output of one command to another command

[root@shellscripting ~]# touch renaming_file
[root@shellscripting ~]# cat >> renaming_file 
name=$1
set `who am i`
mv $name $name.$1

[root@shellscripting ~]# ls -l test*
-rwxrwxrwx. 1 root root 45 Mar 21 10:17 test1

[root@shellscripting ~]# sh renaming_file test1 

[root@shellscripting ~]# ls -l test*
-rwxrwxrwx. 1 root root 45 Mar 21 10:17 test1.root

—————————————————————————————————–

                                       Count The no of positional parameters

[root@shellscripting ~]# set hello to dispaly no of positional parameters passed we can use echo$#
[root@shellscripting ~]# echo $#
12
[root@shellscripting ~]# touch count
[root@shellscripting ~]# cat > count 
echo No Of items in current directory is=$#
[root@shellscripting ~]# sh count 
No Of items in current directory is=0
[root@shellscripting ~]# sh count *
No Of items in current directory is=27
[root@shellscripting ~]# ls -l |wc -l
28
——————————————————————————————————
                                              Math On Integers Using ‘expr’
[root@shellscripting ~]# touch arthematic
[root@shellscripting ~]# cat  >> arthematic 
a=40 b=10
echo `expr $a + $b`
echo `expr $a – $b`
echo `expr $a \* $b`
echo `expr $a / $b`
echo `expr $a % $b`
[root@shellscripting ~]# sh arthemetic 
50
30
400
4
0
————————————————————————————————————
                                            Operator Precedence        
         
 [root@shellscripting ~]# touch arthemetic1
[root@shellscripting ~]# cat >> arthemetic1
a=30 b=15 c=5 d=10
echo `expr $a \* \( $b + $c \) / $d`
 
[root@shellscripting ~]# sh arthemetic1 
60
——————————————————————————————————-
                                               Escape Sequences
\n new line
\t tab space
\b back space
\c To print the output immdiately after it

[root@server-rhel6 ~]# touch sample

[root@server-rhel6 ~]# cat >> sample 
echo -e “Hello how are u \nI am fine”
[root@server-rhel6 ~]# sh sample 
Hello how are u
I am fine

                                         
[root@server-rhel6 ~]# cat sample 
echo -e “Hello how are u \tI am fine”
[root@server-rhel6 ~]# sh sample 
Hello how are u I am fine

[root@server-rhel6 ~]# cat sample 
echo -e “Hello how are u \b\b\b\bI am fine”
[root@server-rhel6 ~]# sh sample 

Hello how arI am fine
————————————————————————————————-
                                  To get the output in bold use this syntax

[root@server-rhel6 ~]# cat sample 
echo -e  “\033[1mHello how are u I am fine\033[0m”
[root@server-rhel6 ~]# sh sample
Hello how are u I am fine

—————————————————————————————————-
                                  To Get background black use this
[root@server-rhel6 ~]# cat sample 
echo -e  “\033[7mHello how are u I am fine\033[0m”
[root@server-rhel6 ~]# sh sample
Hello how are u I am fine
—————————————————————————————————
                                                  tput command 
[root@server-rhel6 ~]# touch tput
[root@server-rhel6 ~]# cat tput 
tput clear
echo “Total no of rows on screen=\c”
tput lines
echo “Total number of columns on screen=\c”
tput cols
tput cup 5 10
tput bold
echo “This should be in bold”
[root@server-rhel6 ~]# sh tput 
Total no of rows on screen=\c
24
Total number of columns on screen=\c
80
          This should be in bold
———————————————————————————————–
                                               Variable Description

$0 The filename of the current script.
$n These variables correspond to the arguments with which a script was invoked. Here n
is a  positive decimal number corresponding to the position of an argument (the first argument is           $1, the    second argument is $2, and so on).
$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two arguments, $* is
equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives two arguments, $@ is                 equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number of the current shell. For shell scripts, this is the process ID under which                they are executing.
$! The process number of the last background command.
——————————————
$?

[root@server-rhel6 ~]# mkdir one
[root@server-rhel6 ~]# echo $?
0
[root@server-rhel6 ~]# mkdir one
mkdir: cannot create directory `one’: File exists
[root@server-rhel6 ~]# echo $?

1

———————————————

                                            If -Then statements

[root@server-rhel6 ~]# touch test

 

[root@server-rhel6 ~]# touch if-then
[root@server-rhel6 ~]# cat if-then 
echo “enter the target file name”
read source target
if mv $source $target
then
echo “your file has been sucessfully renamed”
fi

[root@server-rhel6 ~]# sh if-then 
enter the target file name
test test1
your file has been sucessfully renamed
—————————————————————
                                       If-Then-Else Statement

[root@server-rhel6 ~]# touch if-then-else

[root@server-rhel6 ~]# cat if-then-else 
echo “enter the target file name”
read source target
if mv $source $target
then
echo “your file has been sucessfully renamed”
else
echo ” your file has not been created”
fi
[root@server-rhel6 ~]# sh if-then-else 
enter the target file name
hai bye
mv: cannot stat `hai’: No such file or directory
your file has not been created
[root@server-rhel6 ~]# sh if-then-else 
enter the target file name
one two

your file has been sucessfully renamed
—————————————————————–
                                           ELIF STATEMENT


lt ——>lessthan
gt——>greater than
eq——>equal to
nq——>not equal to
le——->less than or equal to
ge——>greater than or equal to [root@server-rhel6 ~]# touch elif

[root@server-rhel6 ~]# cat elif 
echo -e “enter a number between 10 and 20:\c”
read num
if [ $num -lt 10 ]
then
echo “the  number is less than 10”
elif [ $num -gt 20 ]
then
echo “that the value is greater than 20”
else
echo “the number is between 10 and 20”
fi
[root@server-rhel6 ~]# sh elif 
enter a number between 10 and 20:21 
that the value is greater than 20

[root@server-rhel6 ~]# sh elif 
enter a number between 10 and 20:11
the number is between 10 and 20

[root@server-rhel6 ~]# sh elif 
enter a number between 10 and 20:9
the  number is less than 10

————————————————————————————
                                          Run Checks On Files
-f to check for file
-d to check for directory
-c character special files
-b to check for block files
-r to check for read permission
-w to check for writepermission
-x  to check for execute permission
-s  to check the size grater than zero

[root@server-rhel6 ~]# cat >> filechecks 
echo -e “enter a name:\c”
read fname
if [ -f $fname ]
then
echo “u entered a file name”
else
echo “it’s  not a file name”
fi

[root@server-rhel6 ~]# sh filechecks 
enter a name:sample1
u entered a file name

[root@server-rhel6 ~]# sh filechecks 
enter a name:one
it’s  not a file name
—————————————————————————————–
                               Append Text to a File Through Shell Script

[mohammed@server-rhel6 ~]$ touch test1
[mohammed@server-rhel6 ~]$ touch append
[mohammed@server-rhel6 ~]$ cat append 
echo -e “enter the name of file:\c”
read fname
if [ -f $fname ]
then
if [ -w $fname ]
then
echo “type matter to append .To quit press crtl+d ”
cat >> $fname
else
echo ” you do not have permission to write”
fi
fi

[mohammed@server-rhel6 ~]$ sh append 
enter the name of file:test1
type matter to append .To quit press crtl+d 
hello how are u
[mohammed@server-rhel6 ~]$
[mohammed@server-rhel6 ~]$ cat test1 
hello how are u
[mohammed@server-rhel6 ~]$ ls -l test1 
-rw-rw-r–. 1 mohammed mohammed 16 Mar 22 08:16 test1
[mohammed@server-rhel6 ~]$ chmod -w test1 
[mohammed@server-rhel6 ~]$ ls -l test1 

-r–r–r–. 1 mohammed mohammed 16 Mar 22 08:16 test1
[mohammed@server-rhel6 ~]$ sh append
enter the name of file:test1
you do not have permission to write

———————————————————————————–
                                                  Run Checks On string

-n to check the length of string grater than zero
-z to check the string is empty or not

[root@server-rhel6 ~]# cat string-checks 
str1=” hello what ur name”
str2=”my name is mohammed”
str3=””
[ “$str1” = “$str2” ]
echo $?
[ “$str1” != “str2”  ]
echo $?
[ -n “$str1” ]
echo $?
[ -z “$str3” ]
echo $?

[root@server-rhel6 ~]# sh string-checks 
1
0
0
0

——————————————————————————–
AND Logical Operator

-a and operation
-o or operation

[root@server-rhel6 ~]# cat operators 
echo -e “enter a number between 50 and 100:\c”
read num
if[ $num -le 100 -a $num -ge 50 ]
then
echo ” u have enterd number in limits”
else
echo ” u have crossed limits”
fi

[root@server-rhel6 ~]# sh operators 
enter a number between 50 and 100:75
u have enterd number in limits

[root@server-rhel6 ~]# sh operators 
enter a number between 50 and 100:500
u have crossed limits

—————————————————-
[root@server-rhel6 ~]# cat count 
echo -e “enter a chracter:\c”
read var
if [ `echo $var | wc -c` -eq 2 ]
then
echo “u entered a char”
else
echo “invalid input”
fi
[root@server-rhel6 ~]# sh count 
enter a chracter:j
u entered a char
[root@server-rhel6 ~]# sh count 
enter a chracter:cscbhj
invalid input
—————————————————–
                                                   OR Operator
[root@server-rhel6 ~]# cat or-operator 
echo -e ” enter a low-case charecter:\c”
read var
if [ `echo $var | wc -c` -eq 2 ]
then
if [ $var = a -o $var = e -o $var = i -o $var = o -o $var = u ]
then
echo “u entered vowel “
else
echo ” u didnot entered vowel”
fi
else
echo “invalid input”
fi
 
[root@server-rhel6 ~]# sh or-operator 
 enter a low-case charecter:a
u entered vowel
[root@server-rhel6 ~]# sh or-operator 
 enter a low-case charecter:k
 u didnot entered vowel
———————————————————-
                                                                Case Statement
[root@server-rhel6 ~]# cat case 
echo -e “enter a character:\c”
read var
case $var in
[a-z])
echo ” you entered a lower case alphabet”
;;
[A-Z])
echo “you have entered a upper case alphabet”
;;
[0-9])
echo “u have entered a number”
;;
?)
echo “you have entered a special character”
;;
*)
echo “you have entered more than one character”
;;
esac
[root@server-rhel6 ~]# sh case 
enter a character:k
 you entered a lower case alphabet
[root@server-rhel6 ~]# sh case 
enter a character:9
u have entered a number
[root@server-rhel6 ~]# sh case 
enter a character:&
you have entered a special character
——————————————————————————————————-
                                          Case statement usage

[root@shellscripting ~]# cat wordcheck 
echo -e “enter a word:\c”
read word
case $word in
[aeiou]* |  [AEIOU]*)
echo “you have entered a word starting with vowel ”
;;
[0-9]*)
echo “u have entered a word with digit ”
;;
*[0-9])
echo ” u entered a word ending with digit”
;;
???)
echo “you enterd a three letter word”
;;
*)
echo “i dont know what u have entered ”
;;
esac
——————————–
[root@shellscripting ~]# sh wordcheck 
enter a word:t
i dont know what u have entered
[root@shellscripting ~]# sh wordcheck 
enter a word:a
you have entered a word starting with vowel
[root@shellscripting ~]# sh wordcheck 
enter a word:A
you have entered a word starting with vowel
[root@shellscripting ~]# sh wordcheck 
enter a word:12
u have entered a word with digit
[root@shellscripting ~]# sh wordcheck 
enter a word:basubj9
u entered a word ending with digit
[root@shellscripting ~]# sh wordcheck 
enter a word:htr
you enterd a three letter word
[root@shellscripting ~]# sh wordcheck 
enter a word:xv
i dont know what u have entered
————————————————————————————————————————–
                                                        While Loop

[root@shellscripting ~]# cat while.loop 
x=1
while [ $x -le 10 ]
do
echo $x
x=`expr $x + 1`
done

[root@shellscripting ~]# sh while.loop
1
2
3
4
5
6
7
8
9
10
—————————————————————————————————————–
                                                             Until loop

[root@shellscripting ~]# cat until 
count=1
until [ $count -gt 5 ]
do
echo $count
count=`expr $count + 1`
done
[root@shellscripting ~]# sh until 
1
2
3
4
5
———————————————————————————————————————–
                                                              For Loop
 
[root@shellscripting ~]# cat for.loop 
for item in *
do
if [ -d $item ]
then
echo $item
fi
done
[root@shellscripting ~]# sh for.loop 
Desktop
Documents
Downloads
logs
Music
Pictures
Public
scripting
shellscripting
Templates
Videos
——————————————————————————————————————-
                                                        2 as exist value Indicate

[root@shellscripting ~]# cat exist.value 
a=4.5
b=4.5
[ $a -eq $b ]
echo $?

[root@shellscripting ~]# sh exist.value 
exist.value: line 3: [: 4.5: integer expression expected

2

Note:If the exist or value is 2,that indicates that ure doing illegal operation,or result is not what u expecting.

[root@shellscripting ~]# cat exist.value 
a=4.5
b=4.5
[ “$a” = “$b” ]
echo $?

[root@shellscripting ~]# sh exist.value 
0

———————————————————————————————————————
                                     Internal Field separator

root@shellscripting ~]# cat field.separator 
line=” shell scripting is fun”
set $line
echo $1
echo $2
echo $3
echo $4
[root@shellscripting ~]# sh field.separator 
shell
scripting
is
fun
[root@shellscripting ~]# cat field.separator 
line=” shell scripting is fun”
IFS=:
set $line
echo $1
echo $2
echo $3
echo $4
[root@shellscripting ~]# sh field.separator 
shell scripting is fun

[root@shellscripting ~]# cat field.separator 
line=”shell:scripting:is:fun”
IFS=:
set $line
echo $1
echo $2
echo $3
echo $4

[root@shellscripting ~]# sh field.separator 
shell
scripting
is
fun
——————————————————————————————————————-
                      Retrieving  Data from passwd file using shell script

[root@shellscripting ~]# cat data
echo -e “enter the usename:\c”
read logname
line=`grep $logname /etc/passwd`
IFS=:
set $line
echo “username:$1”
echo “userid:$3”
echo “groupid:$4”
echo “comment:$5”
echo “home directory:$6”

echo “default shell:$7”

[root@shellscripting ~]# sh data
enter the usename:mohammed
username:mohammedrafi
userid:500
groupid:500
comment:mohammedrafi
home directory:/home/mohammedrafi
default shell:/bin/bash
[root@shellscripting ~]# sh data
enter the usename:root
username:root
userid:0
groupid:0
comment:root
home directory:/root
default shell:/bin/bash
operator
———————————————————————————————————-
                                                      Sleep command
 
sleep 5 means 5-sec after it will do next process
 
[root@shellscripting ~]# cat sleep 
echo -e “enter the sentence:\c”
read str
for word in $str
do
echo $word
sleep 5
done
[root@shellscripting ~]# sh sleep 
enter the sentence:hai how are u
hai
how
are
u
——————————————————————————————–
                                            Count No of lines and words
[root@shellscripting ~]# cat count
echo -e “enter a file name:\c”
read fname
terminal=`tty`
exec < $fname
nol=0
now=0
while read line
do
nol=`expr $nol + 1`
set $line
now=`expr $now + $#`
done
echo “Number of lines $nol”
echo “NUmber of words $now”
exec < $terminal
[root@shellscripting ~]# sh count 
enter a file name:count
Number of lines 15
NUmber of words 42
—————————————————————————————————–
                                              Sample program 
Note:Here we are using this script to give man paged for a group of commands
root@shellscripting ~]# cat help 
for cmd in `cat listofcommands`
do
man $cmd >> helpfile
done
[root@shellscripting ~]# sh help
 
[root@shellscripting ~]# cat listofcommands 
pwd
cal
who
date
—————————————————————————————————————-
                                                      Nested Loops
[root@shellscripting ~]# cat loops 
a=1
while [ $a -le 3 ]
do
b=1
while [ $b -lt 3 ]
do
c=1
while [ $c -lt 3 ]
do
echo “$a$b$c”
c=`expr $c + 1`
done
b=`expr $b +1`
done
a=`expr $a +1`
done
————————————————————————————————————————-
                                                        While Loop
[root@shellscripting ~]# cat break 
count=1
while [ $count -le 10 ]
do
echo $count
count=`expr $count + 1`
done
echo “we are out of loop”
[root@shellscripting ~]# sh break 
1
2
3
4
5
6
7
8
9
10
we are out of loop
[root@shellscripting ~]# cat break 
count=1
while [ $count -le 10 ]
do
if [ $count -eq 6 ]
then
break
fi
echo $count
count=`expr $count + 1`
done
echo “we are out of loop”
[root@shellscripting ~]# sh break 
1
2
3
4
5
we are out of loop
———————————————————————————————————————-
                                                        Continue 
[root@shellscripting ~]# cat continue 
count=0
while [ $count -le 9 ]
do
count=`expr $count + 1`
if [  $count -eq 5 ]
then
continue
fi
echo $count
done
echo ” we are out of the loop”
[root@shellscripting ~]# sh continue 
1
2
3
4
6
7
8
9
10
 we are out of the loop
———————————————————————————————————————
                                                          Meta characters
[root@shellscripting ~]# cat metachar 
pwd;cal;echo “hello”
[root@shellscripting ~]# sh metachar 
/root
     March 2016
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
hello
———————————————————————————————————————-
                                                   Check for late logged in user
[root@shellscripting ~]# useradd newuser
[root@shellscripting ~]# passwd newuser
Changing password for user newuser.
New password:
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[root@shellscripting ~]# cat checkfor.userlogin
echo -e “enter username:\c”
read logname
time=0
while true
do
who | grep “$logname” > /dev/null
if [ $? -eq 0 ]
then
echo “$logname has logged in”
if [ $time -ne 0 ]
then
echo “$logname was $time minutes late”
fi
exit
else
time=`expr $time + 1`
sleep 60
fi
done
[root@shellscripting ~]# sh checkfor.userlogin 
enter username:newuser
Note In another terminal make login with newusername and wait for result in previous terminal
At terminal one u can see
newuser has logged in
newuser was 2 minutes late
—————————————–
[root@shellscripting ~]# sh checkfor.userlogin 
enter username:root
root has logged in
[root@shellscripting ~]# w
 06:43:00 up  2:32,  2 users,  load average: 0.00, 0.08, 0.07
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    172.16.28.1      04:11    0.00s  0.55s  0.04s w
newuser  pts/1    172.16.28.1      06:36    6:12   0.07s  0.07s -bash
——————————————————————————————————————
                                                         Write Command

[root@shellscripting ~]# write newuser
hello how are u
[root@shellscripting ~]#

Note :Other user must be logged in
Note2:If  any error msgages disables at second user as go for mesg y

[newuser@shellscripting ~]$
Message from root@shellscripting on pts/0 at 06:53 …
hello how are u
EOF

——————————————————————————————————————–
                             Create ur own command using Functions

[root@shellscripting ~]# cat functions 
youtube()
{
echo “Goodmorning”
}
bye()
{
cal
}

Note :U can not run the script above script with sh as we did till now like sh functions
alternative is use . function so that it will it will  make entry in shell’s memory.

[root@shellscripting ~]# youtube 
Googmorning

[root@shellscripting ~]# bye 
March 2016
Su Mo Tu We Th Fr Sa
1  2  3  4  5
6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

[root@shellscripting ~]# unset youtube 
[root@shellscripting ~]# youtube
-bash: youtube: command not found

——————————————————————————————————–

Advertisements

One thought on “shell-scripts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s