Shell2

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

(1)编写脚本  vim shell_cd.sh


#!/bin/bash
# 
#######################################
#Date:          2022/12/27
#FileName:      shell_cd.sh
#Description:
#               directory exists or not.
#######################################

read -p "Please input a directory name to show:" dir
[ ! -e "$dir" ] && echo "directory does not exist." && exit 1
[ ! -d "$dir" ] && echo "you must need to input a directory name." && exit 2
[-d "$dir" ] && ls -l $dir

(2)增加权限并运行

[root@rhcsa ~]# chmod a+rx shell_cd.sh
[root@rhcsa ~]# ./shell_cd.sh
Please input a directory name to show:ccd
directory does not exist.
[root@rhcsa ~]# ./shell_cd.sh
Please input a directory name to show:shell_cd.sh
you must need to input a directory name.
[root@rhcsa ~]# ./shell_cd.sh
Please input a directory name to show: /root
total 28
-rw-------. 1 root root 1550 Jul 19 13:39 anaconda-ks.cfg
drwxr-xr-x  2 root root   69 Nov  4 20:28 ansible
-rw-r--r--. 1 root root  127 Sep 18 01:06 base.repo
drwxr-xr-x. 2 root root    6 Jul 19 14:03 Desktop
drwxr-xr-x. 2 root root    6 Jul 19 14:03 Documents
drwxr-xr-x. 2 root root    6 Jul 19 14:03 Downloads
-rwxr-xr-x  1 root root  104 Dec 27 20:30 history_max.sh
-rw-r--r--. 1 root root 1871 Jul 19 13:45 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Jul 19 14:03 Music
drwxr-xr-x. 2 root root    6 Jul 19 14:03 Pictures
drwxr-xr-x. 2 root root    6 Jul 19 14:03 Public
-rwxr-xr-x  1 root root  397 Dec 27 22:23 shell_cd.sh
-rwxr-xr-x  1 root root  496 Dec 27 22:17 shell_dir.sh
drwxr-xr-x. 2 root root    6 Jul 19 14:03 Templates
-rwxr-xr-x  1 root root  145 Dec 27 21:17 user_rhel.sh
drwxr-xr-x. 2 root root    6 Jul 19 14:03 Videos

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

(1)编写脚本 vim user01.sh

#!/bin/bash
############################
#Date:          2022/12/27
#FileName:      user01.sh
#Description:
#               UID=0
############################
uid="`id -u $1`"
[ "$uid" -eq 0 ] && echo "user $1 is administrator." && exit 0
[[ "$uid" != 0 ]] && echo "user $1 is common user."

(2)增加权限并运行

[root@rhcsa ~]# chmod a+rx user01.sh
[root@rhcsa ~]# ./user01.sh
[root@rhcsa ~]# ./user01.sh root
user root is administrator.
[root@rhcsa ~]# ./user01.sh xxx
user xxx is common user.

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

(1)编写脚本 vim uidgid.sh


#!/bin/bash
###########################
#Date:          2022/12/27
#FileName:      uidgid.sh
#Description:
#               UID=GID
############################
uid=`id -u $1`
gid=`id -g $1`
[ "$uid" -eq "$gid" ] && echo "user $1 is a good guy." && exit 0
echo "user $1 is a bad guy."

(2)增加权限并运行

[root@rhcsa ~]# chmod a+rx uidgid.sh
[root@rhcsa ~]# id xxx
uid=1000(xxx) gid=1000(xxx) groups=1000(xxx)
[root@rhcsa ~]# ./uidgid.sh xxx
user xxx is a good guy.

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

(1)编写脚本 vim null_str.sh

#!/bin/bash
# 
############################
#Date:          2022/12/27
#FileName:      null_str.sh
#Description:
#               null string
############################
read -p "please input a file:" file
[ ! -e "$file" ] && echo "$file is not exit." && exit 1
[ ! -f "$file" ] && echo "the input must be a file name." && exit 2
num=`grep ^$ $file |wc -l`
[[ $num = 0 ]] && echo "the file has no null string." && exit
echo "the file has $num null string."

(2)增加权限并运行

[root@rhcsa ~]# chmod a+rx null_str.sh
[root@rhcsa ~]# ./null_str.sh
please input a file:/etc/inittab
the file has no null string.
[root@rhcsa ~]# ./null_str.sh
please input a file:test.txt
the file has 3 null string.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值