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

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

被折叠的 条评论
为什么被折叠?



