1、取出用户配置文件的第6行;
[root@localhost ~]# vim 作业1.sh
#!/bin/bash
head -6 /etc/passwd | tail -1
2、取出当前系统上所有用户的shell,要求,每种shell只显示一次,并且按顺序进行显示;
[root@localhost ~]# vim 作业2.sh
#!/bin/bash
cut -d ":" -f 7 /etc/passwd | sort -u
3、如果/var/log/messages文件的行数大于100,就显示好大的文件;
[root@localhost ~]# vim 作业3.sh
#!/bin/bash
if [[ $(wc -l /var/log/messages | cut -d " " -f 1) > 100 ]]
then
echo "好大的文件"
fi
4、显示/etc目录下所有以pa开头的文件,并统计其个数;
[root@localhost ~]# vim 作业4.sh
#!/bin/bash
find /etc/ -name pa* | wc -l
5、如果用户hadoop不存在就添加,否则显示用户已存在;
[root@localhost ~]# vim 作业5.sh
#!/bin/bash
! id hadoop &> /dev/null
if [ $? -eq 0 ];then
useradd hadoop &> /dev/null
else
echo "用户已存在"
fi
6、编写一个 Shell 程序 mkf,此程序的功能是:显示 root 下的文件信息,然后建立一个 kk 的文件夹,在此文件夹下建立一个文件 aa,修改此文件的权限为可执行;
[root@localhost ~]# vim mkf.sh
#!/bin/bash
ls -l /root && mkdir /root/kk && touch /root/kk/aa && chmod +x /root/kk/aa
7、编写一个 Shell 程序 test3,程序执行时从键盘读入一个目录名,然后 显示这个目录下所有文件的信息;
[root@localhost ~]# vim test3.sh
#!/bin/bash
read -p "输入一个目录名:" ml
ls -l $ml
8、编写一个 Shell 程序 test4,从键盘读入 x、y 的值,然后做加法运算,最后输出结果;
[root@localhost ~]# vim test4.sh
#!/bin/bash
read -p "输入第一个数:" num1
read -p "输入第二个数:" num2
let sum=$num1+$num2
echo "相加后结果为:$sum"
9、写一个脚本,给定一个用户,如果其UID为0,就显示此为管理员;否则,就显示其为普通用户;
[root@localhost ~]# vim 判断用户.sh
#!/bin/bash
read -p "请输入用户名:" user
id $user &> /dev/null
if [ $? -eq 0 ]
then
uid=$(id $user | cut -d " " -f 1 | cut -d "=" -f 2 | cut -d "(" -f 1)
if [ $uid -eq 0 ]
then
echo "$user是管理员"
else
echo "$user是普通用户"
fi
else
echo "你输入的用户不存在"
fi
10、写一个脚本,判断当前系统上是否有用户的默认shell为bash;如果有,就显示有多少个这类用户;否则,就显示没有这类用户;
[root@localhost ~]# vim 判断bash用户.sh
#!/bin/bash
cat /etc/passwd | grep "bash" &> /dev/null
if [ $? -eq 0 ];then
user=$(cat /etc/passwd | grep "bash" | cut -d ":" -f 1)
sum=$(cat /etc/passwd | grep "bash" | cut -d ":" -f 1 | wc -l)
echo -e "bash用户有以下这些:\n$user"
echo ""
echo "bash用户共有$sum个"
else
echo "没有bash用户"
fi
11、写一个脚本,给定一个文件,比如/etc/inittab,判断这个文件中是否有空白行;如果有,则显示其空白行数;否则,显示没有空白行。
[root@localhost ~]# vim 判断空行.sh
#!/bin/bash
count=$(cat /etc/inittab | cut -d "#" -f 2 | grep "^$" | wc -l)
if [ $count -eq 0 ];then
echo "该文件没有空白行"
else
echo "该文件空白行数为:$count"
fi
12、写一个脚本,给定一个用户,判断其UID与GID是否一样,如果一样,就显示此用户为“good guy”;否则,就显示此用户为“bad guy”。
[root@localhost ~]# vim uid和gid.sh
#!/bin/bash
read -p "输入一个用户名:" user
id $user &> /dev/null
if [ $? -eq 0 ];then
uid=$(id $user | cut -d " " -f 1 | cut -d "=" -f 2 | cut -d "(" -f 1)
gid=$(id $user | cut -d " " -f 2 | cut -d "=" -f 2 | cut -d "(" -f 1)
if [ $uid -eq $gid ];then
echo "good guy"
else
echo "bad guy"
fi
else
echo "你输入的用户不存在"
fi