三、通过参数传递一个用户名给脚本,此用户不存在时,添加之: vim /useradd.sh #!/bin/bash if ! grep "^$1\>" /etc/passwd &> dev/null; then useradd $1 echo $1 | passwd --stdin $1 &> /dev/null echo "User Add $1 yes" fi
vim /useradd.sh #!/bin/bash if [ $# -lt 1 ];then echo "AT least username" exit 1 fi if ! grep "^$1\>" /etc/passwd &> dev/null; then useradd $1 echo $1 | passwd --stdin $1 &> /dev/null echo "User Add $1 yes" fi
vim /useradd.sh #!/bin/bash if [ $# -lt 1 ];then echo "AT least username" exit 1 fi if grep "^$1\>" /etc/passwd &> dev/null; then echo "User $1 exists" exit 1 else useradd $1 echo $1 | passwd --stdin $1 &> /dev/null echo "User Add $1 yes" fi
四、通过命令行参数给定两个数字,输出其中较大的数值; vim /xxx.sh #!/bin/bash # if [ $# -lt 2 ];then echo "Tow integers." exit 1 fi
if [ $1 -ge $2 ]; then echo "Max number:$1"
else echo "Max number: $2" fi vim /xxx.sh #!/bin/bash # if [ $# -lt 2 ];then echo "Tow integers." exit 1 fi declare -i max if [ $1 -ge $2 ]; then max=$1 else max=$2 fi echo "Max number: $max vim /xxx.sh
#!/bin/bash # if [ $# -lt 2 ];then echo "Tow integers." exit 1 fi declare -i max=$1 if [ $1 -ge $2 ]; then max=$2 fi 五、通过命令行参数给定一个用户名,判断其ID号时偶数还是奇数; vim /xxx.sh #!/bin/bash if [ $# -lt 1 ]; then echo "userid X >1" exit 1 fi
uid1=$(id -u $1) let s=$[$uid1%2]
if [ $s -eq 0 ]; then echo "user: $1 uid: $uid1 and OU" else echo "user: $1 uid: $uid1 and ji" fi vim /xxx.sh #!/bin/bash # if [ $# -lt 1 ]; then echo "userid Y > 1" exit 1 fi
if id -u $1 &> /dev/null; then uid=$(id -u $1) let S=$[$uid%2] if [ "$S" -eq 0 ]
then echo "user: $1 uid: $uid an ou" else echo "user: $1 uid: $uid an ji" fi else echo "wu yong hu" fi 六、通过命令行参数给定两个文本文件名,如果某文件不存在,则结束脚本执行;都存在时返回每个文件的行数,并说明其中行数较多的文件’ #!/bin/bash if [ $# -lt 2 ];then echo "Wen Jian Shu > 2" exit 1 fi
if [ -e $1 -a -e $2 ];then w1=$(grep ^.*$ $1 | wc -l) echo "Wenj: $1 HangShu: $w1" w2=$(grep ^.*$ $2 | wc -l) echo "Wenj: $2 HangShu: $w2" if [ "$w1" -gt "$w2" ];then echo "$1 Hang Shu Jiao Duo" else echo "$2 Hang Shu Jiao Duo" fi else echo "WENJIAN: $1 or WenJian: $2 bu cun zai" fi 七、 (1)传递一个参数给脚本,此参数为用户名; (2)根据其ID好判断其用户类型: 0:管理员 1-999:系统用户 1000+:登录用户 #!/bin/bash # if [ $# -lt 1 ];then echo "can chu xu lt 1" exit 1 fi
if ! grep "^$1" /etc/passwd &> /home/cs;then echo "wu cu y h" exit 2 fi
#!/bin/bash # cat << EOF disk) show disks info mem) show memory info cpu) show cpu info *) QUIT EOF
read -p "Your choice: " options if [[ "$options" == "disk" ]]; then fdisk -l /dev/[sh]d[a-z] elif [[ "$options" == "mem" ]]; then free -m elif [[ "$options" == "cpu" ]]; then lscpu else echo "Unkown option." exit 1
fi
九、循环遍历三个用户有则显示$username exits,没有则添加; #/bin/bash # for username in user21 user22 user23; do if id $username &>/etc/null; then echo "$username exits" else useradd $username && echo "Add user $username dinlshed."
fi done
十、创建1-10个空文件 #!/bin/bash # for filename in 1 2 3 4 5 6 7 8 9 10; do if touch /tmp/f$filename done
十一、求100以内所有正整数之和; #!/bin/bash # declare -i sum=0 for i in {1..100}; do sum=$[$sum + $i] done echo $sum
十二、判断/var/log目录下的每一个文件的内容类型 #!/bin/bash # for filename in /var/log/*; do
十三、分别求100以内分别求所有偶数之和,以及所有奇数之和; #!/bin/bash # declare -i sunj=0 suno=0 for i in $( seq 1 2 100) ; do sunj=$[$sunj + $i] done echo $sunj for x in $(seq 0 2 100); do suno=$[$suno + $x ] done echo $suno 十四、计算当前系统所有用户的id之和; #!/bin/bash for i in `cut -d: -f3 /etc/passwd`;do sum=$[$sum+$i] done echo $sum
十五、通一过脚本参数传递一个目录给脚本,而后计算此目录下所有文本文件的行数之和;并说明此类文件的总数; #!/bin/bash read -p "Please enter your directory:" file [ -z $file ] && echo “error" && exit 3 for i in `find $file -type f`;do wc=`cat $i | wc -l` for i in $wc;do sum=$[$sum+$i] done done echo $sum