Linux Shell Introduction III

1) select var in keywords do ops done

#!/bin/bash
#select regards each item in keywords as form, and do ops interactiv ely

echo "What is your favourite OS?"

select var in "LINUX" "UNIX" "WINDOWS" "OTHER"
do
    break
done

echo "You have selected $var"

#Sample Input
#sh select.sh
#What is your favourite OS?
# 1) LINUX
# 2) UNIX
# 3) WINDOWS
# 4) OTHER
# #? 1
# You have selected LINUX

2) case var in str1) ops ;; str2) ops ;; str3) ops ;; ... *) ops esac

#!/bin/bash
#Demo for case usage

echo "************"
echo "Please select your operation:"
echo " Press C to Copy"
echo " Press D to Delete"
echo " Press B to Backup"
echo "************"

read op
case $op in
    C)
    echo "You have selected Copy"
    ;;
    D)
    echo "You have selected Delete"
    ;;
    B)
    echo "You have selected Backup"
    ;;
    *)
    echo "Invalid selection"
esac

3) while *** do ops done

#!/bin/bash
#Demo for while usage

NUM=1
SUM=0

while [ $NUM -le 10 ]
do
    SUM='expr $NUM + $SUM`
    NUM=`expr $NUM + 1`
done

echo "Sum is $SUM"

#Output Sample
#Sum is 55
#!/bin/bash
#Demo for while break usage

while [ -d /etc ]
do
    ls -ld /etc
    break
done

4) batch add user

#Normal add user
#useradd davy
#passwd davy
#Changing password for user davy
#New UNIX password:
...

#How to escape the interactive process when set password for user?
#Example below will solve this problem:
$echo 123455 | passwd --stdin davy

#!/bin/bash
#Demo for batch add user

#Read user
echo "Please input user prefix"
read userprefix
echo "Please input user count"
read count
#Set user
n=1
while [ $n -le $count ]
do
    useradd $userprefix$n
    n=`expr $n + 1`
done

#Read password
echo "Please input password prefix"
read pwdprefix
#Set password
n=1
while [ $n -le $count ]
do
    echo $pwdprefix$n | passwd --stdin $userprefix$n
    n=`expr $n + 1`
done

5) batch delete user

#!/bin/bash
#Demo for batch delete user

echo "Please input userprefix"
read userprefix
echo "Please input user count"
read usercount

echo "Start batch deleting user..."
n=1
while [ $n -le $usercount ]
do
    usr/sbin/userdel -r $userprefix$n
    n=`expr $n + 1`
done
echo "Finished batch deleting user..."

6) until ... do ops done

#!/bin/bash
#Demo for until usage

N=1
COUNT=51
SUM=0

until [ $N -eq $COUNT ]
do
    SUM=`expr $SUM + $N`
    N=`expr $N + 1`
done

echo "SUM IS $SUM"

7) break/continue

#!/bin/bash
#Demo for break/continue usage

while true
do
    echo "Please select operation"
    echo "1 COPY"
    echo "2 DELETE"
    echo "3 BACKUP"
    echo "4 QUIT"
    read op
    case $op in
        1)
        echo "YOUR SELECTION IS COPY"
        ;;
        2)
        echo "YOUR SELECTION IS DELETE"
        ;;
        3)
        echo "YOUR SELECTION IS BACKUP"
        ;;
        4)
        echo "QUITTING>>>"
        break;
        ;;
        *)
        echo "INVALID SELECTION"
        ;;
    esac
done

8) shift

1) It is used to move all the params left by one position.

    The params that moved out of space can no longer be accessed.

2) Shift one position will cause param count decrease by one

3) Shift is useful when input param number is more than 9

#!/bin/bash
#Demo for shift usage

echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3"
shift
echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3"
shift
echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3"
shift

#Sample Input
#sh shift.sh 1 2 3
#Sample Output
#Number of args is 3
#arg1=1, arg2=2, arg3=3
#Number of args is 2
#arg1=2, arg2=3, arg3=
#Number of args is 1
#arg1=3, arg2=, arg3=
#!/bin/bash
#Demo for shift usage

echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, arg7=$7, arg8=$8, arg9=$9
shift
echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, arg7=$7, arg8=$8, arg9=$9
shift
echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, arg7=$7, arg8=$8, arg9=$9

#Sample Input
#sh shift.sh 1 2 3 4 5 6 7 8 9 10 11
#Sample Output
#Number of args is 11
#arg1=1, arg2=2, arg3=3, arg4=4, arg5=5, arg6=6, arg7=7, arg8=8, arg9=9
#Number of args is 10
#arg1=2, arg2=3, arg3=4, arg4=5, arg5=6, arg6=7, arg7=8, arg8=9,  arg9=10
#Number of args is 9
#arg1=3, arg2=4, arg3=5, arg4=6, arg5=7, arg6=8, arg7=9, arg8=10,  arg9=11
#!/bin/bash
#Demo for shift usage

if [ $# -le 0 ]
then
    echo "Not enough param"
    exit 1
fi

COUNT=0
SUM=0
while [ $# -gt 0 ]
do
    COUNT=`expr $COUNT + 1`
    SUM=`expr $SUM + $1`
    shift
done

echo "SUM IS $SUM
echo "COUNT IS $COUNT"

#Sample Input
#sh shift2.sh
#Sample Output
#Not enough param
#Sample Input
#sh shift2.sh 1 2 3
#Sample Output
#SUM IS 6
#COUNT IS 3
#Sample Input
#sh shift2.sh 1 2 3 4
#Sample Output
#SUM IS 10
#COUNT IS 4
#Sample Input
#sh shift2.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14
#Sample Output
#SUM IS 105
#COUNT IS 14

9) function

1) The reason why we use function is to make shell script more human readable.

2) We can totally do not use function at all, but that will mess our script up

3) There is no local variable inside a function, all the variable in a function is global visiable, except special variables and position variables

4) The special variables and position variable inside a function is only local visiable, it is passed when we call this function. 

4) We can add param when we call a function

#An apache httpd example

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
....

reload() {
    echo -n $"Reloading $prog:"
    if ...
    else ...
    fi
}

3) define function syntax

function_name(){
    ops..
}

4) use function syntax

function_name param1 param2 param3 ...

5) Sample

#!/bin/bash
#Demo for function usage

help(){
echo "Incorrect param number"
echo "Usage: sh function.sh \$1 \$2 \$3"
}

if [ $# -ne 3 ]
then
    help
else
    echo "Thank you for your correct input"
fi

#Sample Input
#sh function.sh 1
#Sample Output
#Incorrect param number
#Usage: sh function.sh \$1 \$2 \$3
#Sample Input
#sh function.sh 1 2 3
#Sample Output
#Thank you for your correct input
#!/bin/bash
#Demo for function usage

test(){
# $1 is only visiable in current function
# $# is only visiable in current function
echo "Param1=$1"
echo "Param number=$#"
}

test 1 2 3

#Sample Input
#sh function2.sh
#Sample Output
#Param1=1
#Param number=3
#/bin/bash
#Demo for function usage

test(){
echo "Param1=$1"
eccho "Param number=$#"
}

test $1 $2 $3

#Sample Input
#sh function3.sh
#Sample Output
#Param1=
#Param number=0

#Sample Input
#sh function3.sh 2 2 2
#Sample Output
#Param1=2
#Param number=3

#Sample Input
#sh function3.sh 2 2 2 2 2 2
#Sample Output
#Param1=2
#Param number=3

10) sh -n ***.sh --> Only check the syntax error

      sh -x ***.sh  --> Will show every step of script execution

11) If a user need to execute a script, the permissions required are as follows:

1) User have r permission for the script
2) User have rx permission for the directory that stores the script

# If the user don't have the permission of x for the script, he can still execute the script with syntax: sh script.sh ...
# If the user have the permission of x for the script, then he can execute the script with syntax: /.../script.sh ...

猜你喜欢

转载自davyjones2010.iteye.com/blog/1986711