当前位置:   article > 正文

脚本练习题_脚本题库链接

脚本题库链接

1.写一个脚本,要求如下:

  • 设定变量Fa的值为/etc/passwd
  • 依次向/etc/passwd中的每个用户问好,并且说出对方的ID是什么。结果输出如下:
    • Hello,root,your UID is 0.
  • 统计当前系统一个有多少个用户并输出
[root@master ~]# ./1.test.sh 
hello,root,your UID is 0
hello,bin,your UID is 1
hello,daemon,your UID is 2
hello,adm,your UID is 3
hello,lp,your UID is 4
hello,sync,your UID is 5
hello,shutdown,your UID is 6
hello,halt,your UID is 7
hello,mail,your UID is 8
hello,operator,your UID is 11
hello,games,your UID is 12
hello,ftp,your UID is 14
hello,nobody,your UID is 65534
hello,dbus,your UID is 81
hello,systemd-coredump,your UID is 999
hello,systemd-resolve,your UID is 193
hello,tss,your UID is 59
hello,polkitd,your UID is 998
hello,unbound,your UID is 997
hello,sssd,your UID is 996
hello,sshd,your UID is 74
hello,mysql,your UID is 995
hello,apache,your UID is 994
hello,saslauth,your UID is 993
hello,zabbix,your UID is 992
当前有25用户

[root@master ~]# vi ./1.test.sh 
#!/bin/bash

Fa=/etc/passwd
sum=$(cat $Fa|wc -l)

while read A;do
       username=$(echo $A|awk -F: '{print$1}')
       user_uid=$(echo $A|awk -F: '{print$3}')
    echo   hello,$username,your UID is $user_uid

done < $Fa

echo "当前有$sum用户"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

2.写一个脚本,传递两个整数给此脚本,让脚本分别计算并显示这两个整数的和,差,积,商

[root@master ~]# vi 2.test.sh

#!/bin/bash
#!/bin/bash
echo "输入的第一个数为$1"
echo "输入的第二个数为$2"
echo "二数之和为$[$1+$2]"
echo "二数之差为$[$1-$2]"
echo "二数之积为$[$1*$2]"
echo "二数之商为$[$1/$2]"
[root@master ~]# chmod +x 2.test.sh 
[root@master ~]# ./2.test.sh 8 4
输入的第一个数为8
输入的第二个数为4
二数之和为12
二数之差为4
二数之积为32
二数之商为2

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3.写一个脚本,要求如下:

  • 创建目录/tmp/scripts
  • 切换至此目录中
  • 复制/etc/pam.d目录至当前目录,并重命名为test
  • 将当前目录的test及其里面的文件和子目录的属主改为redhat
  • 将test及其子目录中的文件的其它用户的权限改为没有任何权限
[root@master ~]# vi 3.test.sh

#!/bin/bash

mkdir /tmp/scripts
cd /tmp/scripts
cp -rf /etc/pam.d ./test
useradd redhat
chown -R redhat ./test
chmod -R o-rwx ./test
[root@master ~]# chmod +x 3.test.sh 
[root@master ~]# ./3.test.sh 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

4.写一个脚本,要求如下:

  • 显示当前系统日期和时间,而后创建目录/tmp/lstest
  • 切换工作目录至/tmp/lstest
  • 创建目录a1d,b56e,6test
  • 创建空文件xy,x2y,732
  • 列出当前目录下以a,x或者6开头的文件或目录
  • 列出当前目录下以字母开头,后跟一个任意数字,而后跟任意长度字符的文件或目录
[root@master ~]# vi 4.test.sh
#!/bin/bash

date
mkdir /tmp/lstest
cd /tmp/lstest
mkdir a1d,b56e,6test
touch xy,x2y,732
ls [a,x,6]*
ls | egrep -i "^[a-z] [0-9]"
[root@master ~]# ./4.test.sh 
Thu Sep  8 19:44:57 CST 2022
xy,x2y,732
a1d,b56e,6test:

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/819153
推荐阅读
相关标签
  

闽ICP备14008679号