练习1 写一个脚本,利用RANDOM生成10个随机数,并找出其中的最大值最小值
[root@localhost mscripts]# cat lx20.sh
#!/bin/bash
declare -i MIN=0
declare -i MAX=0
for I in {1..10}; do
declare -i N=echo $RANDOM
[ $I -eq 1 ] && MIN=$RANDOM
if [ $I -le 9 ]; then
echo -n "$N,"
else
echo "$N"
fi
if [ $N -gt $MAX ]; then
MAX=$N
fi
if [ $N -lt $MIN ]; then
MIN=$N
fi
done
echo -e "MAX is $MAX.\nMIN is $MIN"
练习2 写一个脚本 ,跟三个整数; 并比较三个整数的大小,找出最大值与最小值;
[root@localhost mscripts]# cat lx21.sh
#!/bin/bash
declare -i MAX=0
declare -i MIN=$1
for I in {$1,$2,$3}; do
if [ $I -gt $MAX ]; then
MAX=$I
fi
if [ $I -lt $MIN ]; then
MIN=$I
fi
done
echo -e "MAX is $MAX. \nMIN is $MIN."
练习3 写一个脚本
接受一个选项--Add,--Del,后面跟参数:user1,user2,user10
--Add:添加用户user1,user2,user10
--Del: 删除用户user1,user2,user10
其他:退出
[root@localhost mscripts]# cat lx22.sh
#!/bin/bash
if [ $# -lt 2 ]; then
echo "please input two arguments.."
exit 0
fi
if [ $1 = '--add' ]; then
for I in echo $2 | sed 's@,@ @g'
; do
if id $I &> /dev/null
; then
echo "user $I exist"
else
useradd $I &> /dev/null
echo "$I" | passwd --stdin $I &> /dev/null
echo "user $I have been added."
fi
done
elif [ $1 = '--del' ]; then
for I in echo $2 | sed 's@,@ @g'
; do
if ! id $I &> /dev/null
; then
echo "user $I not exist."
else
userdel -r $I &> /dev/null
echo "user $I have been deleted."
fi
done
else
echo "quit..."
exit 9
fi
转载于:https://blog.51cto.com/ahtao/2048411