您的位置:首页 > 运维架构 > Shell

bash脚本简单脚本集合(1)

2016-09-08 10:11 316 查看
1、使用一个用户名作为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
#!/bin/bash
#
read -p "please input one UserName: " USERNAME
id $USERNAME &>/dev/null
if [ $? -eq 0 ];then
echo "The User is exist"
exit
else
useradd $USERNAME
echo "$USERNAME already add"
echo "`grep "\<$USERNAME\>" /etc/passwd`"
fi


2、判断用户输入文件路径,显示其文件类型(普通、目录、链接,其他文件类型)
#!/bin/bash
#
read -p "please input valid path: " PATH1
[ ! -e $PATH1 ] && echo "This path is non-exsit" && exit
type1=`ls -ld $PATH1 | grep -o "^."`
case $type1 in
-)
echo "This is file"
;;
d)
echo "This is directory"
;;
l)
echo "This is links"
;;
p)
echo "This is pipe"
;;
s)
echo "This is sokect"
;;
c)
echo "This is charater"
;;
b)
echo "This is block"
;;
esac


3、添加10个用户user1-user10,密码同用户名
#!/bin/bash
#
for i in {1..10}
do
id user$i &> /dev/null
if [ $? -eq 1 ];then
useradd user$i
echo "user$i" | passwd --stdin user$i &> /dev/null
echo "The User$i success add!"
else
echo "The User$i is exist"
fi
done


4、使用for循环打印九九乘法表
#!/bin/bash
#
for h in {1..9}
do
for l in `seq 1 $h`
do
squ=$(($h*$l))
echo -ne "${h}x${l}=$squ\t"
done
echo
done


5、使用for循环打印国际象棋棋盘
#!/bin/bash
#
for j in {1..8}
do
for i in {1..8}
do
if [ $[($i+$j)%2] -eq 1 ];then
echo -ne "\e[41m  \e[0m"
else
echo -ne "\e[47m  \e[0m"
fi
done
echo ""
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  for bash 国际棋盘