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
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
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
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
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
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
Case statement usage
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 ~]# 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
[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
line=” shell scripting is fun”
set $line
echo $1
echo $2
echo $3
echo $4
[root@shellscripting ~]# sh field.separator
shell
scripting
is
fun
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
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 ~]# write newuser
hello how are u
[root@shellscripting ~]#
[newuser@shellscripting ~]$
Message from root@shellscripting on pts/0 at 06:53 …
hello how are u
EOF
[root@shellscripting ~]# cat functions
youtube()
{
echo “Goodmorning”
}
bye()
{
cal
}
[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

One thought on “shell-scripts”