Shell条件测试练习

1、取出/etc/passwd文件的第6行;

[root@shell ~]# head -6 /etc/passwd | tail -1
sync:x:5:0:sync:/sbin:/bin/sync
[root@shell ~]# sed -n '6p' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
[root@shell ~]# awk 'NR==6' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync

2、取出当前系统上所有用户的shell,要求,每种shell只显示一次,并且按顺序进行显示;使用cut、sort结合管道实现

[root@shell ~]# cut -d ":" -f7 /etc/passwd | sort -u    #sort -u为排序并去重
/bin/bash
/bin/sync
/sbin/halt
/sbin/nologin
/sbin/shutdown
/usr/sbin/nologin

3、如果/var/log/messages文件的行数大于100,就显示好大的文件

[root@shell ~]# [[ `wc -l < /var/log/messages` >100 ]] && echo 好大的文件
好大的文件

wc -l /var/log/messages 会把文件名 /var/log/messages 一起进行输出;而 wc -l < /var/log/messages 则只会输出统计结果。wc -l < /var/log/messages 的输入来自文件 /var/log/messages,而不是来自标准输入,也就是键盘输入。

4、显示/etc目录下所有以pa开头的文件,并统计其个数;

[root@shell ~]# find /etc -type f -name "pa*" | wc -l
11

5、如果用户fox不存在就添加,否则显示用户已存在

[root@shell ~]# id fox &>/dev/null && echo 用户已存在 || useradd fox
[root@shell ~]# id fox &>/dev/null && echo 用户已存在 || useradd fox
用户已存在

6、编写一个 Shell 程序 mkf,此程序的功能是:显示 root 下的文件信息,然后建立一个 kk 的文件夹,在此文件夹下建立一个文件 aa,修改此文件的权限为可执行

[root@shell ~]# vim mkf.sh +

#!/bin/bash

ls /root
mkdir kk
cd kk
touch aa
chmod +x aa

[root@shell ~]# bash mkf.sh 
2  aa  anaconda-ks.cfg    date.txt  mkf.sh
[root@shell ~]# cd kk
[root@shell kk]# ll
总用量 0
-rwxr-xr-x. 1 root root 0 11月 19 20:30 aa

7、编写一个 Shell 程序 test3,程序执行时从键盘读入一个目录名,然后 显示这个目录下所有文件的信息

root@shell ~]# vim test3.sh +

read -p "请输入一个目录名:" dir_name
ls -l ${dir_name}

[root@shell ~]# bash test3.sh 
请输入一个目录名:/etc/hosts
-rw-r--r--. 1 root root 158  6月 23  2020 /etc/hosts
[root@shell ~]# bash test3.sh 
请输入一个目录名:/etc/passwd
-rw-r--r--. 1 root root 2176 11月 19 20:20 /etc/passwd
 

8、编写一个 Shell 程序 test4,从键盘读入 x、y 的值,然后做加法运算,最后输出结果

[root@shell ~]# vim test4.sh +

read -p "请输入两个数:" a b
echo $[a+b]

[root@shell ~]# bash test4.sh 
请输入两个数:1 2  
3

写一个脚本,完成以下要求:
给定一个用户:
    1、如果其UID为0,就显示此为管理员;
    2、否则,就显示其为普通用户;

[root@shell scripts]# vim user.sh +

read -p "请输入一个用户的UID:" uid 
if [[ $uid = 0 ]]; then
        echo "此用户为管理员"
else
        echo "此用户为普通用户"
fi


[root@shell scripts]# bash user.sh 
请输入一个用户的UID:1
此用户为普通用户
[root@shell scripts]# bash user.sh 
请输入一个用户的UID:0
此用户为管理员

写一个脚本
给定一个用户,判断其UID与GID是否一样 
如果一样,就显示此用户为“good guy”;否则,就显示此用户为“bad guy”

[root@shell scripts]# vim user.sh +

read -p "请输入一个用户名:" name
        uid=$(id -u "$name")
        gid=$(id -g "$name")
 
if [[ "$uid" = "$gid" ]]; then
        echo "good guy"
else
        echo "bad guy"
fi

[root@shell scripts]# bash user.sh

[root@shell scripts]# id root
用户id=0(root) 组id=0(root) 组=0(root)
[root@shell scripts]# id pzz
用户id=1005(pzz) 组id=1003(pzz) 组=1003(pzz)

[root@shell scripts]# bash user.sh 
请输入一个用户名:root
good guy
[root@shell scripts]# bash user.sh 
请输入一个用户名:pzz
bad guy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值