shell练习题(1)
练习:
1.建立脚本service.sh,当执行的时候要求输入(1、2、3、4、5)时安装对应的httpd、vim、wget、更换aliyum等功能,当输入错误 时会提示你,应该输入正确的值
[root@bogon yy]# cat service.sh
#!/bin/bash
cat <<-EOF
+-------------------------------------------------------------------------+
| System_tools V1.0 |
+-------------------------------------------------------------------------+
| 1. 更换阿里源. |
| 2. 安装http. |
| 3. 安装vim. |
| 4. 安装wget. |
| 5. exit. |
+-------------------------------------------------------------------------+
EOF
read -p "请输入你的选项:" cce
case $cce in1)rm -rf /etc/yum.repos.d/* &> /dev/nullcurl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/nullif [ $? -eq 0 ];thenecho "yum源更换成功"elseecho "yum源更换失败,请手动安装"fi;;2)yum -y remove httpd &> /dev/nullyum -y install httpd &> /dev/null systemctl restart httpd &> /dev/nullif [ $? -eq 0 ];thenecho "http ok!"elseecho "http fialed,请手动安装"fi;;3)yum -y install vim &> /dev/nullif [ $? -eq 0 ];thenecho "vim ok!"elseecho "vim nooo!!!"fi;;4)yum -y install wget &> /dev/nullif [ $? -eq 0 ];thenecho "wget ok"elseecho "wget nooo!!!"fi;;5)exit 0;;
esac
2.随机数生成8位随机密码
#!/bin/bash
string="AFJALKGJglekrjgkASKJFEKFELjjglkerjgwej235834095ejglkdjg"
sum=''
for ((i=1; i<=8; i++)) #指定循环8次
dolen=${#string} #获取变量的长度num=$[RANDOM % $len] #使用变量长度中生成随机数(作为取字符的索引)value=${string:$num:1} #截取指定索引的字符 sum+=$value #字符追加到sum变量中
done
echo $sum #输出变量$sum(8位随机密码)
3.测试生产环境的主机存活性,将up的ip保存在一个文件中,将down的ip保存在一个文件中
参数详解:
wait:等待上面命令后台执行结束后(即上一个的进程终止),在执行下面的echo命令
[root@bogon yy]# cat ip.sh
#!/bin/bash
ip=192.168.127.
for i in {2..10}
do
{ping -c1 $ip$i &> /dev/nullif [ $? -eq 0 ];thenecho "$ip$i is ok" >> /tmp/ip_up.txtelseecho "$ip$i down" >> /tmp/ip_down.txtfi
} &
done
wait
echo "ok!!
4.for循环批量创建用户
[root@bogon yy]# cat user.sh
#!/bin/bash
read -p "请设置用户名/数量/密码:" prefix num pass
cat <<EOF
用户前缀:$prefix
用户数量:$num
用户密码:$pass
EOF
for (( i=1;i<=$num;i++ ))
#for i in $(seq 1 $num)
do
user=$prefix$i
id $user &> /dev/null
if [ $? -eq 0 ];thenecho "$user is already exist!"exit 0
elseuseradd $user && echo $pass | passwd --stdin $user &> /dev/null
fi
done
echo "starting create users..."[root@bogon yy]# ./user.sh
请设置用户名/数量/密码:liu 2 1
用户前缀:liu
用户数量:2
用户密码:1
starting create users...
[root@bogon yy]# cat /etc/passwd | tail -2
liu1:x:1000:1000::/home/liu1:/bin/bash
liu2:x:1001:1001::/home/liu2:/bin/bash
5.通过一个文件批量创建用户:(根据文件内容实现批量创建用户,第一列为用户名,第二列为密码)
[root@bogon tmp]# cat /etc/passwd | tail -5
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
user4:x:1004:1004::/home/user4:/bin/bash
user5:x:1005:1005::/home/user5:/bin/bash
[root@bogon tmp]# cat user_pass.txt
user1 1234
user2 1234
user3 1234
user4 1234
user5 1234
[root@bogon tmp]# cat create_user.sh
#!/bin/bash
[ $UID -ne 0 ] && exit 1
while read ug
douser=`echo $ug | awk '{print $1}'`pass=`echo $ug | awk '{print $2}'`id $user &> /dev/null || useradd $user &> /dev/null && echo $pass | passwd $user --stdin &> /dev/null
done < /tmp/user_pass.txt