Sunday, March 8, 2015

Some scripting practice

# vi mycase.sh
#!/bin/sh
#
if [ $# -lt 1 ]
then
        echo
        echo "Invalid usage: Usage is $0 <interface>"
        echo
        exit
fi
if [ -f $0 -a -x $0 ]
then
        ifconfig $1 | grep "inet addr" | cut -d ":" -f2 | cut -d " " -f1
else
        chmod u+x $0
        ifconfig $1 | grep "inet addr" | cut -d ":" -f2 | cut -d " " -f1
#       echo check the permission
fi

[nepal@localhost lab1]$ cat mycase.sh
#!/bin/sh
echo "Please select from the list below"
echo "start|restart|reload - to restart the process"
echo "stop - to stop the process"
echo "Please enter the selection"
read choice
echo
case $choice in
start|restart|reload)
        service httpd restart;;
stop)
        service httpd stop;;
*)
        echo "Invalid selection"
esac
[nepal@localhost lab1]$



[nepal@localhost lab1]$ cat mypara.sh
#!/bin/sh
a=$1
b=$2
echo first value is $1
echo Second value is $2
if test  $a == $b
then
        echo "A equal to B"
elif [ $a -lt $b ]
then
        echo "A is less than B"
elif [ $a -gt $b ]
then
        echo "A is greater than B"
else
        echo "Selection does not meet the condition"
fi
[nepal@localhost lab1]$


[nepal@localhost lab1]$ more mycase.sh
#!/bin/sh
echo "Please select from the list below"
echo "start|restart|reload - to restart the process"
echo "stop - to stop the process"
echo "Please enter the selection"
read choice
echo
case $choice in
start|restart|reload)
        service httpd restart;;
stop)
        service httpd stop;;
*)
        echo "Invalid selection"
esac
[nepal@localhost lab1]$

[nepal@localhost lab1]$ more mycmd.sh
#!/bin/sh
#
echo the first value is $1
echo the fifth valie is $5
echo  the 10th value is ${10}
echo  the 10th value is shift  $9
echo The 14th value is $14
echo
echo the file name of the script is $0
echo all value passed on the command are: $*
[nepal@localhost lab1]$

[nepal@localhost lab1]$ cat king.sh
#!/bin/sh
#
if [ $# -lt 1 ]
then
        echo "Please supply the name of a file: usage $0 <filename>"
        exit
else
        . ./$1
        echo "$raja was a king of Nepal"
fi
[nepal@localhost lab1]$ cat raja
raja="Birendra Shah"
[nepal@localhost lab1]$

[nepal@localhost lab1]$ cat raja
raja="Birendra Shah"
rani="Aisharya Shah"
[nepal@localhost lab1]$ cat king1.sh
#!/bin/sh
#
# sourcing
. ./raja
echo "$raja was king of Nepal"
echo "$rani was a queen of Nepal"
[nepal@localhost lab1]$

[nepal@localhost lab1]$ cat myip
myip() {
ifconfig eth0 | grep "inet addr" | cut -d ":" -f2 | cut -d " " -f1;
}
[nepal@localhost lab1]$ cat myip.sh
. ./myip
echo The ip address of `hostname` is `myip`
[nepal@localhost lab1]$ sh myip.sh
The ip address of localhost.localdomain is 192.168.10.29
[nepal@localhost lab1]$



[nepal@localhost lab1]$ cat myip.sh
. ./myip
echo The ip address of `hostname` is `myip`
[nepal@localhost lab1]$ vi myip.sh
[nepal@localhost lab1]$ sh myip.sh
The ip address of localhost.localdomain is 192.168.10.29
Birendra Shah and Aisharya Shah were popular in Nepal
[nepal@localhost lab1]$ cat myip.sh
. ./myip
. ./raja
echo The ip address of `hostname` is `myip`
echo "$raja and $rani were popular in Nepal"


[nepal@localhost lab1]$ cat myip
myip() {
ifconfig eth0 | grep "inet addr" | cut -d ":" -f2 | cut -d " " -f1;
}
[nepal@localhost lab1]$ cat raja
raja="Birendra Shah"
rani="Aisharya Shah"
[nepal@localhost lab1]$


[nepal@localhost lab1]$ cat listfiles
listfiles() {
ls -l | grep "^-" | tr -s " " | cut -d " " -f9
}

[nepal@localhost lab1]$ listfiles
-bash: listfiles: command not found
[nepal@localhost lab1]$ ls -l listfiles
-rw-rw-r--. 1 nepal nepal 66 Mar  8 07:56 listfiles
[nepal@localhost lab1]$ chmod u+x listfiles
[nepal@localhost lab1]$ listfiles
-bash: listfiles: command not found
[nepal@localhost lab1]$ sh -xv listfiles
listfiles() {
ls -l | grep "^-" | tr -s " " | cut -d " " -f9
}
[nepal@localhost lab1]$ listfiles
-bash: listfiles: command not found
[nepal@localhost lab1]$ . ./listfiles
[nepal@localhost lab1]$ listfiles
cmdp.sh
king1.sh
king.sh
listfiles
mycase.sh
mycmd.sh
myfile1.3324
myfile.3324
myip
myip.sh
mypara.sh
mypat.sh
myshell.sh
raja
usage.sh
[nepal@localhost lab1]$ cat

[nepal@localhost lab1]$ cat >lmfiles
lmf() {
ls -l | grep "^-"| awk '{print $9}'
}
[nepal@localhost lab1]$ lmfiles
-bash: lmfiles: command not found
[nepal@localhost lab1]$ . ./lmfiles
[nepal@localhost lab1]$ lmfiles
-bash: lmfiles: command not found
[nepal@localhost lab1]$ lmf
cmdp.sh
king1.sh
king.sh
listfiles

[nepal@localhost lab1]$ cat lmfiles
lmf() {
ls -l | grep "^-"| awk '{print $9}'
}
users() {
cat /etc/passwd | awk -F: '{print $1}'
}
[nepal@localhost lab1]$ lmf
cmdp.sh
king1.sh
king.sh
listfiles
[nepal@localhost lab1]$ users
root
bin
daemon

Find the list of users who owned directory content
$ ls -lh | awk '{print $3}' | sort | uniq -c | sort -nk 1

[nepal@localhost lab1]$ cat >forsed
/usr/local/bin/myfile
/usr/local/bin/yourfile
/usr/local/bin/cool

[nepal@localhost lab1]$ sed 's//usr/local//opt/' forsed
sed: -e expression #1, char 8: unknown option to `s'

[nepal@localhost lab1]$ sed 's/\/usr\/local/\opt/' forsed
opt/bin/myfile
opt/bin/yourfile
opt/bin/cool
[nepal@localhost lab1]$ sed 's/\/usr\/local/\/opt/' forsed
/opt/bin/myfile
/opt/bin/yourfile
/opt/bin/cool
[nepal@localhost lab1]$

[nepal@localhost lab1]$ cat forsed
/usr/local/bin/myfile
/usr/local/bin/yourfile
/usr/local/bin/cool
[nepal@localhost lab1]$
[nepal@localhost lab1]$ sed 's/\/usr\/local/\/opt/' forsed
/opt/bin/myfile
/opt/bin/yourfile
/opt/bin/cool
[nepal@localhost lab1]$ sed 's/\/usr\/local/(\/opt)/' forsed
(/opt)/bin/myfile
(/opt)/bin/yourfile
(/opt)/bin/cool
[nepal@localhost lab1]$
[nepal@localhost lab1]$ cat names
Ram Sing
Sam bdr
Ajay Sing
Bir Bal

[nepal@localhost lab1]$ sed 's/Sing/Singh/; s/Ram/Shyam/; s/Bir/Bir Bahadur/' names
Shyam Singh
Sam bdr
Ajay Singh
Bir Bahadur Bal

[nepal@localhost lab1]$ sed -e 's/Sing/Singh/' -e 's/Sam/Shyam/' -e 's/Ajay/Ajay Pratap Singh/' names
Ram Singh
Shyam bdr
Ajay Pratap Singh Singh
Bir Bal

[nepal@localhost lab1]$ cat >names.f
s/Sing/Singh/
s/Sam/Shyam/
s/Ajay/Ajay Pratap Singh/
[nepal@localhost lab1]$ sed -f names.f names
Ram Singh
Shyam bdr
Ajay Pratap Singh Singh
Bir Bal
[nepal@localhost lab1]$ cat names
Ram Sing
Sam bdr
Ajay Sing
Bir Bal
[nepal@localhost lab1]$
[nepal@localhost lab1]$ ls -l | grep "^-"
[nepal@localhost lab1]$ ls -l | grep "^-" | awk '{print $9 $5}'
[nepal@localhost lab1]$ ls -l | grep "^-" | awk '{print $9 "\t"  $5}'

[nepal@localhost lab1]$ cat names
,Ram Sing
,Sam bdr
,Ajay Sing
,Bir Bal
l

[nepal@localhost lab1]$ awk -F, '{print "Name:"  $2}' names
Name:Ram Sing
Name:Sam bdr
Name:Ajay Sing
Name:Bir Bal

[nepal@localhost lab1]$ awk '/VA/' address
[nepal@localhost lab1]$ awk '/VA/' new
newaddr     newhile.sh
[nepal@localhost lab1]$ awk '/VA/' newaddr
Barry Chu, 22 East Broadway, Richmond, VA
Harry Peters, 328A Brook Road, Roanoke, VA
[nepal@localhost lab1]$ awk '/VA/ {print $1}' newaddr
Alice
Harry
[nepal@localhost lab1]$ awk -F, '/VA/ {print $1}' newaddr
Marry Chu
Harry Peters
[nepal@localhost lab1]$
[nepal@localhost lab1]$ sed 's/Marry Chu/Barry Johnson/' address >address1;mv address1 address

[nepal@localhost lab1]$ sed 's/Alia Thomas/Anita Ghose/' address >adress1;mv adress1 address; cat address | awk '{print $1}'
Mike
Barry
Anita


No comments:

Post a Comment