shell脚本练习2

本文介绍四个实用的Shell脚本案例,包括验证用户输入目录、判断用户权限、比对UID与GID以及检查文件中的空白行。

1、编写一个 Shell脚本,程序执行时从键盘读入一个目录名,如果用户输入的目录不存在,则提示file does not exist;如果用户输入的不是目录则提示用户必须输入目录名;如果用户输入的是目录则显示这个目录下所有文件的信息。

#!/bin/bash
unset file
read -p "Please input your directoryname: " file
test -z "$file" && echo "You must input a directoryname " && exit 0
! test  -a "$file" && echo "The file does not exist" && exit 0
ll  $file

 

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

#!/bin/bash
read -p "Please input you username:" username
uid=`id -u  $username`
if [ $uid -eq 0 ]
then
        echo "该$username是管理员"
else
        echo "该$username是普通用户"
fi

 

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

#!/bin/bash
read -p "Please input you username:" username
uid=`id -u  $username`
gid=`id -g  $username`
if [ $uid -eq $gid ]
then
        echo "The $username is good guy"
else
        echo "The $username is bad guy"
fi

查看用户的uid和gid

 

 4、写一个脚本,给定一个文件,比如/etc/inittab,判断这个文件中是否有空白行;如果有,则显示其空白行数;否则,显示没有空白。

#!/bin/bash
null=`grep '^#$' /etc/inittab | wc -l  `
if [ $null -eq 0 ]
then
        echo  "does not null"
else
        echo $null
fi

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值