Linux Shell Introduction II

1. How to test variable

1) Example:

#String test
test str1=str2    #Test if str1.equals(str2)
test str1!=str2   #Test if !str1.equals(str2)
test str1             #Test if str1 != null
test -n str1         #Test if str1 != null
test -z str1         #Test if str1 == null

#Integer test
test int1 -eq int2  #Test if int1 == int2
test int1 -ne int2  #Test if int1 != int2
test int1 -ge int2  #Test if int1 >= int2
test int1 -gt int2   #Test if int1 > int2
test int1 -le int2   #Test if int1 <= int2
test int1 -lt int2    #Test if int1 < int2

#File test
test -d file    #Test if file type is directory
test -f file     #Test if file type is file
test -x file    #Test if file is executable
test -r file     #Test if file is readable
test -w file   #Test if file is writable
test -a file    #Test if file exists
test -s file    #Test if file size != 0

#We can use
#echo $? to decide the test result
#If equals 0, that means test result is true,
#If not, that means test result if false.
$ ls -lt | grep .sh
-rwxr-xr-x ... expr.sh

$ test -d expr.sh
$ echo $?
1

$ test -x expr.sh
$ echo $?
0

$ test -w expr.sh
$ echo $?
0

$ chmod 555 expr.sh
$ test -w expr.sh
$ echo $?
1

2) "test" is commonly used together with "if"

#!/bin/bash
#A demo for usage of test

if [ -d $1 ]
then
    echo "Is directory"
fi

# "test -d $1" is the same as "[ -d $1 ]"

#Sample Input:
#sh test.sh Templates
#Sample Output:
#Is directory

 

2. if ... then ... else ... fi

#!/bin/bash
#A demo for usage of if statement

if [ -d $1 ]
then
    echo "Is directory"
else
    echo "Not directory"
fi

#Sample Input
#bash test.sh Templates
#Sample Output
#Is directory

#Sample Input
#bash test.sh test.sh
#Sample Output
#Not directory
#!/bin/bash
#A demo for usage of complex if statement

if [ -d $1 ]
    then
    echo "Is directory"
elif [ -f $1 ]
    then
    echo "Is file"
elif [ -c $1 ]
    then
    echo "Is device file"
else
    echo "Is unknown file"

#Sample Input
#sh test2.sh test2.sh
#Sample Output
#Is file

#Sample Input
#sh test2.sh donotexist.sh
#Sample Output
#Is unknown file
#!/bin/bash
#A demo for usage of more complex if statement

if [ -d $1 ]
    then
    echo "Is directory"
elif [ -f $1 ]
    then
    echo "Is file"
    if [ -r $1 ]
        then
        echo "File is readable"
    else
        echo "File is not readable"
    if
    if [ -w $1 ]
        then
        echo "File is writable"
    else
       echo "File is not writable"
    fi
    if [ -x $1 ]
        then
        echo "File is executable"
    else
        echo "File is not executable"
    fi
elif [ -c $1 ]
    then
    echo "Is a device file"
else
    echo "Is an unknown file"
fi

#Sample Input
#sh test2.sh test2.sh
#Sample Output
#Is file
#File is readable
#File is writable
#File is executable
#!/bin/bash

if [ $# -ne 2 ]
    then
    echo "Not enough params"
    exit 0
if [ $1 -eq $2 ]
    then
    echo "$1 equals $2"
elif [ $1 -gt $2 ]
    then
    echo "$1 greater than $2"
elif [ $1 - lt $2 ]
    then
    echo "$1 less than $2"
else
    echo "You will never get here"
fi

#Sample Input
#sh compare.sh 1 2
#Sample Output
#1 less than 2
#Sample Input
#sh compare.sh 2 1
#Sample Output
#2 greater than 1
#Sample Input
#sh compare.sh 2 2
#Sample Output
#2 euqlas 2

 

3. AND OR

1) -a: AND

2) -o: OR

 

4. Exit

1) exit 0: means normal exit process

 

5. for ... in ... do ... done

#!/bin/bash

for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday
do
    echo "The day is $DAY"
done

#Sample Output
#The day is Sunday
#The day is Monday
#The day is Tuesday
#The day is Wednesday
#The day is Thursday
#The day is Friday
#The day is Saturday

 

6. Example

#!/bin/bash
#"if ... else" usage
#Using this program to show your system's services

echo "Now, the web services of this Linux System will be detected..."
echo 

#Detect www service
web=`/usr/bin/pgrep httpd`
if [ "$web" != "" ]
then
    echo "The Web service is running."
else
    echo "The Web service is NOT running."
    /etc/rc.d/init.d/httpd start
fi

 

7. awk

#detect user whose UID=0 (means SUPER USER)
$grep root /etc/passwd
#format:
#username:password:UID:GID:userinfo:homefolder:shell
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

$awk -F: '$3==0 {print $1}' /etc/passwd

#-F --> assign the seperator in each line, the default seperator is space, here is ":"
#'$3==0 {print $1}' means that if the third segment equals 0, then we will print out the first segment
#/etc/passwd means the file that will be processed


$ps -el
PID    PPID  PGID   WINPID TTY UID  STIME      COMMAND
5212 1       5212    5212     con 500 20:36:31 /bin/sh
4008 5212 4008    4448     con 500 21:03:03 /bin/ps

$ps -el | awk '$1==5212 {print $8}'
/bin/bash
$ps -el | awk 'length($2)==1 {print $8}'
/bin/sh
$ps -el | awk 'length($2)==4 {print $8}'
COMMAND/bin/ps/bin/sh

#detect user whose password is empty
$awk -F: 'length($2)=0 {print $1}' /etc/shadow

 

8. Example

#!/bin/bash

/bin/echo "Please input username"
read username
/bin/grep $username /etc/passwd > /dev/null 2> /dev/null
if [ $? -eq 0 ]
then
    /bin/echo "Username is: $username"
else
    /bin/echo "User $username doesn't exist"
    exit 1
fi
/bin/echo

#list /etc/passwd info
#grep ^$username:x --> means that find lines which start with $username:x
#grep $username
#root:x:0:0:root:/root:/bin/bash
#operator:x:11:0:operator:/root:/sbin/nologin
#grep ^$username:x
#root:x:0:0:root:/root:/bin/bash --> Here is the valur of $userinfo
userinfo=`/bin/grep ^$username:x /etc/passwd`
userid=`/bin/echo $userinfo | /bin/awk -F: '{print $3}'`
groupid=`/bin/echo $userinfo | /bin/awk -F: '{print $4}'`
homedir=`/bin/echo $userinfo | /bin/awk -F: '{print $6}'`
shell=`/bin/echo $userinfo | /bin/awk -F: '{print $7}'`

/bin/echo "userid = $userid"
/bin/echo "groupid = $groupid"
/bin/echo "homedir = $homedir"
/bin/echo "shlle = $shell"

 

9. killuser

#!/bin/bash
#The script to kill logined user

username="$1"

#find all processes that belongs to this user
/bin/ps -aux | /bin/grep $username | /bin/awk '{print $2}' > /tmp/temp.id
killid=`cat /tmp/temp.pid`

#kill all the processes that started by this user
for PID in $killid
do
    /bin/kill -9 $PID 2> /dev/null
done
#once we have killed all the processes that started by a specific user, that means the user is no longer logged in current system.

你可能感兴趣的