1、测试用户uid和gid是否一样
#/bin/bash
#
if [ $# -lt 1 ];then
echo "Usage:./1.sh ARG1 [ARG2...]"
exit 7
fi
if ! id $1 &>/dev/null;then
echo "No such user."
exit 10
fi
if [ `id -n -u $1` == `id -n -g $1` ];then
echo "yiyang"
else
echo "buyiyang"
fi
2、传递一个参数给脚本,执行判断
#/bin/bash
#
if [ $# -lt 1 ];then
echo "Usage:./1.sh ARG1 [ARG2...]"
exit 7
fi
if [ $1 = 'q' ];then
echo "Quiting..."
exit 1
elif [ $1 = 'Q' ];then
echo "Quiting..."
exit 2
elif [ $1 = 'q' ];then
echo "Quiting..."
exit 3
elif [ $1 = 'quit' ];then
echo "Quiting..."
exit 4
elif [ $1 = 'Quit' ];then
echo "Quiting..."
exit 5
else
echo $1
fi
3、计算1到100的和
#/bin/bash
#
SUM=0
declare -i SUM=0
for I in {1..100};do
let SUM=$[SUM+$I]
done
echo "The sum is :$SUM"
4、利用for循环添加user1到user10
#/bin/bash
#
I=0
declare -i I=0
for I in {1..10};do
if id user$I &> /dev/null;then
echo "user$I exists."
else
useradd user$I
echo "user$I" |passwd --stdin &> /dev/null
echo "Add user$I finished."
fi
done
5、利用for循环语句实现添加和删除10个用户
#/bin/bash
#
I=0
declare -i I=0
for I in {1..10};do
if [ $# -lt 1 ];then
echo "Usage:./7.sh 'add' 'del'"
exit 7
elif [ "$1" == "add" ];then
if id user$I &> /dev/null;then
echo "user$I exists."
else
useradd user$I
echo "user$I" |passwd --stdin &> /dev/null
echo "Add user$I finished."
fi
elif [ "$1" == "del" ];then
if ! id user$I &> /dev/null;then
echo "No user$I"
else
userdel -r user$I
echo "user$I deleted."
fi
fi
done