Linux练习(Part6.4:shell脚本编程)


练习

1、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之。并设置初始密码为123456,显示添加的用户的id号等信息,在此新用户第一次登录时,会提示用户立即改密码,如果没有参数,就提示:请输入用户名

[14:07:18 root@Centos8 /data/Mage]#cat createuser.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-24
#FileName:                 createuser.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#功能实现:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之。并设置   初始密码为123456,显示添加的用户的id号等信息,在此新用户第一次登录时,会提示用户立即改密码,   如果没有参数,就提示:请输入用户名
read -p "Enter Username: " USER
if
[ -z $USER ]
then
echo "请输入用户名"
else
id $USER &> /dev/null
if
[ $? -eq 0 ]
then
echo "用户已存在"
else
useradd $USER;echo "123456"|passwd --stdin $USER &> /dev/null
paaswd -e $USER &> /dev/null
echo "Create succeed! id and username is: `id $USER`"
fi
fi
[14:04:39 root@Centos8 /data/Mage]#bash createuser.sh
Enter Username: qing
Create succeed! id and username is: uid=1012(qing) gid=1015(qing) groups=1015(qing)
[14:06:59 root@Centos8 /data/Mage]#bash createuser.sh
Enter Username:
请输入用户名
[14:07:11 root@Centos8 /data/Mage]#bash createuser.sh
Enter Username: huawei
用户已存在
[14:14:02 root@Centos8 ~]#ssh qing@100.0.0.120
qing@100.0.0.120's password:
You are required to change your password immediately (administrator enforced)
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Sun Jan 24 14:13:51 2021 from 100.0.0.120
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user qing.
Current password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

2、编写脚本 yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息

[15:16:43 root@Centos8 /data/Mage]#cat  yesORno.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-24
#FileName:                 yesORno.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
read -p "请输入yes or no: " INPUT
case $INPUT in
[yY]|[Yy][Ee][Ss])
echo "You input is YES"
;;
[nN]|[Nn][Oo])
echo "You input is No"
;;
*)
echo "Input false,please input yes or no!"
esac

[15:17:17 root@Centos8 /data/Mage]#bash yesORno.sh
请输入yes or no: yEs
You input is YES
[15:17:38 root@Centos8 /data/Mage]#bash yesORno.sh
请输入yes or no: No
You input is No
[15:17:44 root@Centos8 /data/Mage]#bash yesORno.sh
请输入yes or no: y
You input is YES
[15:17:50 root@Centos8 /data/Mage]#bash yesORno.sh
请输入yes or no: N
You input is No
[15:17:53 root@Centos8 /data/Mage]#bash yesORno.sh
请输入yes or no: e
Input false,please input yes or no!

3、编写脚本 filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

[19:10:25 root@Centos7 /data/Mage]#cat filetype.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-18
#FileName:                 filetype.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):        2021 All rights reserved
#********************************************************************
#判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
read -p "请输入文件: " FILE
if [ -L $FILE ] ;then
echo "这是一个链接文件"
elif [ -d $FILE ] ;then
echo "这是一个目录"
elif [ -f $FILE ] ;then
echo "这是一个普通文件"
else
echo "这是一个其他文件"
fi

4、编写脚本 checkint.sh,判断用户输入的参数是否为正整数

5、编写脚本 reset.sh,实现系统安装后的初始化环境,包括:1、别名 2、环境变量,如PS1等 3、安装常用软件包,如:tree 5、实现固定的IP的设置,6、vim的设置等


posted @ 2021-01-24 13:51  加油啊坚持啊搞钱啊  阅读(252)  评论(0)    收藏  举报