要求:
1、取出/etc/inittab文件的第6行;
2、取出当前系统上所有用户的shell,要求,每种shell只显示一次,并且按顺序进行显示;
使用cut、sort结合管道实现
3、如果/var/log/messages文件的行数大于100,就显示好大的文件
4、显示/etc目录下所有以pa开头的文件,并统计其个数
5、如果用户hadoop不存在就添加,否则显示用户已存在
6、编写一个 Shell 程序 test1,程序执行时从键盘读入一个目录名,然后 显示这个目录下所有文件的信息
7、编写一个 Shell 程序 test2,从键盘读入 x、y 的值,然后做加法运算,最后输出结果
1、取出/etc/inittab文件的第6行;
1、取出/etc/inittab文件的第6行;(三种方法)
[root@centos7 ~]# head -6 /etc/inittab | tail -1
#
[root@centos7 ~]# sed -n '6p' /etc/inittab
#
[root@centos7 ~]# awk 'NR==6' /etc/inittab
2、取出当前系统上所有用户的shell,要求,每种shell只显示一次,并且按顺序进行显示;
使用cut、sort结合管道实现
[root@centos7 ~]# cut -d : -f 7 /etc/passwd | sort -u
/bin/bash
/bin/sync
/sbin/halt
/sbin/nologin
/sbin/shutdown
3、如果/var/log/messages文件的行数大于100,就显示好大的文件
[root@centos7 ~] # [ `wc -l < /var/log/messages` -gt 100 ] && echo "好大的文件"
4、显示/etc目录下所有以pa开头的文件,并统计其个数
[root@centos7 ~]# ls -d /etc/pa* | wc -l
5、如果用户hadoop不存在就添加,否则显示用户已存在
[root@centos7 ~]# id hadoop &> /dev/null && echo "用户已存在" || useradd hadoop
6、编写一个 Shell 程序 test1,程序执行时从键盘读入一个目录名,然后 显示这个目录下所有文件的信息
[root@centos7 ~]# vim test1.sh
#! /bin/sh
read -p "请输入目录名:" DIY
[ -d $DIY ] && ls $DIY || echo "$DIY is not exists..."[root@centos7 ~]#sh test1.sh
7、编写一个 Shell 程序 test2,从键盘读入 x、y 的值,然后做加法运算,最后输出结果
[root@centos7 ~]# vim test2.sh
#!/bin/sh
read -p "请输入两个整数:" x y
[[ "$x" =~ ^[1-9]+$ ]] && [[ "$y" =~ ^[1-9]+$ ]]
if [ $? -ne 0 ]
then
echo "请输入数字"
exit 3
else
echo ` expr $x + $y`
fi[root@centos7 ~]# bash test2.sh
请输入两个整数: