编写脚本/opt/test.sh,作用是打印输出用户输入的内容。要求输入用户名、年龄和职业,使用echo命令将这些信息输出到显示屏上
#!/bin/bash
read -p "用户名:" Username
read -p "年龄:" Userage
read -p "职业:" Userjob
echo $Username $Userage $Userjob
编写脚本/opt/PS1-test.sh,作用是读取环境变量。执行脚本之后,PS1环境变量的值备份为/opt/PS1.txt文件,且当前的命令提示符变为"[ <用户名名>@<主机名><当前工作目录> ]# (shell)" ---> 比如[root@localhost ~]# (shell)
echo $PS1 #先读取一下PS1的环境变量
vim /opt/PS1-test
#!/bin/bash
echo $PS1 > /opt/PS1.txt #进行备份
export PS1="[\u@\h \W]\$ (shell)" #配置环境变量
:wq #保存并退出
source PS1-test #在当前shell环境中执行此脚本
编写脚本/opt/user-add.sh,作用是批量创建用户。执行脚本会创建zhangsan110,lisi,zhangsan111,zhangsan112用户,如果用户已创建则输出"<username>exist",如果用户未创建则创建用户,且设置所有用户的密码为Huawei12#$,如果要创建的用户名是lisi,则跳过此用户的创建
#!/bin/bash
for users in zhangsan110 lisi zhangsan111 zhangsan112;do
id $users &> /dev/null
if [ $? -eq 0 ]; then
echo $users exist
else
if [ "$users"="lisi" ]; then
continue
fi
useradd $users
echo "Huawei12#$" | passwd --stdin $users
fi
done
编写脚本/opt/file-backup.sh,作用是备份配置文件。执行脚本的时候将会把/etc第一个层级目录下的所有以.conf结尾的普通文件进行备份至/tmp目录下,并且备份后的文件后缀为.bak(比如/etc/chrony.conf备份后的名字为/etc/chrony.conf.bak)
#!/bin/bash
mkdir /tmp/etc
find /etc -maxdepth 1 -name "*.conf" -type f -exec cp -a {} /tmp{}.bak \;