练习1 通过read传参添加用户和密码
#!/bin/bash
#输入想要添加的用户名
read -p "Enter a username:" name
[ -z "$name" ] && echo "a username is needed." && exit 1
#输入用户的密码
read -p "Enter a passwd for user $name:" passwd
[ -z "passwd" ] && passwd="passwd"
#系统判断用户是否存在,不存在则添加用户和密码
if id $name &> /dev/null;then
echo "$name is exist."
else
useradd $name
echo $passwd | passwd --stdin $name &> /dev/null
fi
解释:
[ -z "passwd" ]#
表示如果passwd为空,不加"$"
运行:
[root@bogon ~]# bash useradd2.sh
Enter a username:kxq
Enter a passwd for user kxq:123456
正在创建信箱文件: 文件已存在
bash检查命令
bash -n 检查语法错误
代码
注意结尾没有fi
1 #!/bin/bash
2
3 #输入想要添加的用户名
4 read -p "Enter a username:" name
5 [ -z "$name" ] && echo "a username is needed." && exit 1
6
7 #输入用户的密码
8 read -p "Enter a passwd for user $name:" passwd
9 [ -z "passwd" ] && passwd="passwd"
10
11 #系统判断用户是否存在,不存在则添加用户和密码
12 if id $name &> /dev/null;then
13 echo "$name is exist."
14 else
15 useradd $name
16 echo $passwd | passwd --stdin $name &> /dev/null
17
~
[root@bogon ~]# bash -n useradd2.sh
useradd2.sh:行18: 语法错误: 未预期的文件结尾
bash -x 调试执行,一步一步的
代码:
#!/bin/bash
#输入想要添加的用户名
read -p "Enter a username:" name
[ -z "$name" ] && echo "a username is needed." && exit 1
#输入用户的密码
read -p "Enter a passwd for user $name:" passwd
[ -z "passwd" ] && passwd="passwd"
#系统判断用户是否存在,不存在则添加用户和密码
if id $name &> /dev/null;then
echo "$name is exist."
else
useradd $name
echo $passwd | passwd --stdin $name &> /dev/null
fi
运行:
[root@bogon ~]# bash -x useradd2.sh
+ read -p 'Enter a username:' name
Enter a username:aaa
+ '[' -z aaa ']'
+ read -p 'Enter a passwd for user aaa:' passwd
Enter a passwd for user aaa:123456
+ '[' -z passwd ']'
+ id aaa
+ useradd aaa
+ echo 123456
+ passwd --stdin aaa
bash脚本编程
过程式编程语言的执行流程:
顺序执行
选择执行
循环执行
选择执行:
(1)&&||
(2)if语句
(3)case语句
if语句:三种格式
单分支的if语句
if condtition ;then
if-true-分支
fi
双分支的if语句
if condition; then
if-true-分支
else
if-false-分支
fi
多分支的if语句
if condition1 ;then
条件1为真分支
elif condition2;then
条件2为真分支
elif condition3 ;then
条件3为真分支
.....
else
所有条件均不满足时的分支
fi
注意:即便多个条件能同时满足,分支只会执行其中一个,首先测试为“真”;
练习2:脚本参数传递一个路径给脚本,判断此文件的类型
#!/bin/bash
#
if [ $# -lt 1 ];then
echo "at leaset one path"
exit 1
fi
if ! [ -a $1 ];then
echo "no such file"
exit 2
fi
if [ -f $1 ];then
echo "Common file!!! "
elif [ -d $1 ];then
echo "Directory!!!"
elif [ -b $1 ];then
echo "block special file!!!"
elif [ -L $1 ];then
echo "Symbolic link!!!"
elif [ -s $1 ];then
echo "Socket file!!!"
else
echo "other file!!!"
fi
[root@bogon ~]# bash -x testfile.sh /var/log/yum.log
+ '[' 1 -lt 1 ']'
+ '[' -a /var/log/yum.log ']'
+ '[' -f /var/log/yum.log ']'
+ echo 'Common file!!! '
Common file!!!
[root@bogon ~]# bash -x testfile.sh /etc/
+ '[' 1 -lt 1 ']'
+ '[' -a /etc/ ']'
+ '[' -f /etc/ ']'
+ '[' -d /etc/ ']'
+ echo 'Directory!!!'
Directory!!!
注意:link文件也是file文件,所以当检测file文件在前会先判断为file文件。
练习3 写一个脚本
(1)传递一个参数给脚本,此参数为用户名;
(2)根据id号来此判断用户类型
1)0:管理员
2)1-999:系统用户
3)>1000:登录用户
#!/bin/bash
#
[ $# -lt 1 ] && echo "at least one user " && exit 1
! id $1 &> /dev/null && "the user is not exist" && exit 2
iduser=$(id -u $1)
#userid=$(id -u $1)
if [ $iduser -eq 0 ];then
echo "the user is root"
elif [ $iduser -ge 1000 ];then
echo "the user is login user"
else
echo "the user is system user"
fi
获取id的两种格式:
iduser=`id -u $1`
userid=$(id -u $1)
练习4 写一个脚本
(1)列出如菜单给用户
- show disk info
- show mem info
- show cpu info
用户选择并输出相应信息
#!/bin/bash
#
cat << EOF
1) show disk info
2) show mem info
3) show cpu info
*) QUIT
EOF
read -p "Your choice:" option
if [ "$option" == "1" ];then
fdisk -l /dev/[sh]d[a-z]
elif [ "$option" == "2" ];then
free -h
elif [ "$option" == "3" ];then
lscpu
else
exit 1
fi
注意 等号两边有空格
for循环
#!/bin/bash
sum=0
for (( i=0; i<=100; i++ ));do
sum=$[ $sum + $i ]
done
echo "1-100的和为$sum"
s=0
for i in {1..100};do
s=$[ $s + $i ]
done
echo "1-100的和为$s"
su=0
for i in `seq 1 100`;do
su=$(( $su + $i ))
done
echo "1-100的和为$su"
ss=0
for i in `seq 1 2 100`;do
ss=$(( $ss + $i ))
done
echo "1-100的奇数之和为$ss"
sq=0
for i in `seq 0 2 100`;do
sq=$(( $sq + $i ))
done
echo "1-100的偶数之和为$sq"