bash脚本示例2
1、编写脚本计算/etc/passwd文件中第10个用户和第20个用户id号之和。
#!/bin/bash
#
a=$(head /etc/passwd | tail -1 | cut -d : -f 3)
b=$(head -20 /etc/passwd | tail -1 | cut -d : -f 3)
echo $[a+b]
2、将当前主机名保存至hostName变量中,主机名如果为空,或者为localhost.localdomain,则将其设置为www.magedu.com
#!/bin/bash
#
hostName=$(hostname)
[ -z $hostName -o $hostName == localhost.localdomain -o $hostName == localhost ] && hostname www.magedu.com
3、编写脚本,通过命令行参数传入一个用户名,判断uid是偶数还是奇数。
#!/bin/bash
#
uid=$(grep “^$1\>” /etc/passwd | cut -d : -f 3)
remainder=$[$uid % 2]
if [ $remainder == 0 ]; then
echo "The uid of user $1 is even."
else
echo "The uid of user $1 is odd."
fi
转载于//blog.51cto.com/3653220/2356406
还没有评论,来说两句吧...