1. 程式人生 > >8.2磁盤2

8.2磁盤2

磁盤2

echo -n 不換行輸出

#!/bin/bash

#

declare -i MAX=0

declare -i MIN=0

for I in {1..10};do

MYRAND=$RAMDOM

[ $I -eq 1 ] && MIN=$MYRAND

if [ $I -le 9 ];then

echo -n "$MYRAND,"

else

echo "$MYRAND"

fi

[ $MYRAND -gt $MAX ] && MAX=$MYRAND

[ $MYRAND -lt $MIN ] && MIN=$MYRAND

done


echo $MAX

echo $MAX


面向過程:

控制結構

順序結構

選擇結構

循環結構


選擇結構:

if:單分支 雙分支 多分支

if CONDITION;then

statement

...

fi


if CONDITION;then

statement

...

else

statement

...

fi


if CONDITION;then

statement

elif CONDITION;then

statement

else CONDITION;then

statement

fi


case 語句;選擇結構

變量的值

case SWITCH in

value1)

statement

...

;;

value2)

statement

...

;;

*)

statement

...

;;

esac


#!/bin/bash

#

case $1 in

[0-9])

echo "A digit" ;;

[a-z])

echo "Lower" ;;

[A-z])

echo "Upper" ;;

*)

echo "Spacial character." ;;

esac


接受參數,start stop restart status其中之一

#!/bin/bash

#

case $1 in

‘start‘)

echo "start server ..." ;;

‘stop‘)

echo "stop server ..." ;;

‘restart‘)

echo "Restart server ..." ;;

‘status‘)

echo "running ..." ;;

*)

echo "basename $0 {start|restart|status|stop}" ;;

esac


寫一個腳本,可以接收選項及參數,而後獲取每一個選項,及選項的參數;

並能根據選項及參數做出特點的操作,比如

adminusers.sh --add tom,jerry

--del tom,blair

-v|--verbose

-h|--help

vi debug.sh

#!/bin/bash

#

DEBUG=0

case $1 in

-v|--verbose)

DEBUG=1 ;;

*)

echo "Unkown options"

exit 7

;;

esac


[ $DEBUG -eq 1 ] && echo hello


vi adduser.sh

#!/bin/bash

#

DEBUG=0

ADD=0

DEL=0

case $1 in

-v|--verbose)

DEBUG=1 ;;

esac


useradd tom &> /dev/null

[ $DEBUG -eq 1 ] && echo "Add user finished"


vim adminusers.sh

#!/bin/bash

#

DEBUG=0

ADD=0

DEL=0


for I in `seq 1 $#`;do

case $1 in

-v|--verbose)

DEBUG=1

shift $1 ;;

-h|--help)

echo "Usage:`bashname $0` --add USER_LIST --del USER_LIST -v| verbose -h|--help "

exit 0 ;;

--add)

ADD=1

ADDUSERS=$2

shift 2 ;;

--del)

DEL=1

DELUSERS=$2

shift 2 ;;

#*)

# echo "Usage:`bashname $0` --add USER_LIST --del USER_LIST -v| # verbose -h|--help "

# exit 7 ;;

esac

done


if [ $ADD -eq 1 ];then

for USERS in `echo $ADDUSERS | sed ‘s@,@ @g‘` ;do

if id $USER &> /dev/null;then

[ $DEBUG -eq 1 ] && echo "$USER exist."

else

useradd $USER

[ $USER -eq 1 ] && echo "$USER finished"

fi

done

fi


if [ $DEL -eq 1 ];then

for USERS in `echo $DELUSERS | sed ‘s@,@ @g‘` ;do

if id $USER &> /dev/null;then

usedell -r $USER;

[ $DEBUG -eq 1 ] && echo "Delete $USER finished."

else

[ $USER -eq 1 ] && echo "$USER not exist"

fi

done

fi



























































































8.2磁盤2