写一个脚本,用RANDOM生成10个随机数,并找出其中的最大值和最小值
#!/bin/bash
ran=$RANDOM
max=$ran
min=$ran
for i in {1..10}
do
echo $ran
ran=$RANDOM
if [ $ran -gt $max ];then
max=$ran
fi
if [ $ran -lt $min ];then
min=$ran
fi
done
echo "MAX: $max MIN:$min"
面向过程语言三种控制结构:
顺序结构:
选择结构:if、case
循环结构:for、while、until
选择结构:
if:单分支,双分支,多分支
单分支:
if condition ;then
statement;
...
fi
双分支:
if condition ;then
statement
...
else
statement
...
fi
多分支
if condition ;then
statement
...
elif condition2 ;then
statement
...
else
statement
...
fi
case:
case SWITCH in
value1)
statement
...
;;
value2)
statement
....
;;
*)
statement
...
;;
esac
写一个脚本,可以接受选项以及参数,而后能获取每一个选项及选项的参数;并能根据选项和参数做出特定的操作.比如
adminusers.sh --add tom,jerry --del tom,blair -v|--verbose -h|--help
#!/bin/bash
if [ $# -eq 0 ];then
echo "Usage: `basename $0` {--add|--del} user1,user2 ... [-v|--verbose] or {-h|--help}"
exit 1
fi
declare -i debug=0
declare -i adduser=0
declare -i deluser=0
for i in `seq 0 $#`
do
if [ $# -gt 0 ]; then
case $1 in
--add)
adduser=1
user1=$2
shift 2
;;
--del)
deluser=1
user2=$2
shift 2
;;
-v|--verbose)
debug=1
shift
;;
-h|--help)
echo "Usage: `basename $0` {--add|--del} user1,user2 ... [-v|--verbose] or {-h|--help}"
exit 0
;;
*)
echo "Usage: `basename $0` {--add|--del} user1,user2 ... [-v|--verbose] or {-h|--help}"
exit 7
;;
esac
fi
done
if [ $adduser -eq 1 ]; then
for i in `echo $user1|sed 's/,/ /g'`
do
if id $i &>/dev/null;then
[ $debug -eq 1 ]&& echo "user $i exists!"
else
useradd $i ;
[ $debug -eq 1 ]&& echo "user $i add finished!"
fi
done
fi
if [ $deluser -eq 1 ]; then
for i in `echo $user2|sed 's/,/ /g'`
do
if id $i &>/dev/null;then
userdel -r $i
[ $debug -eq 1 ]&& echo "user $i del finished!"
else
[ $debug -eq 1 ]&& echo "user $i not exists!"
fi
done
fi
本文转自biao007h51CTO博客,原文链接: http://blog.51cto.com/linzb/1736853,如需转载请自行联系原作者