马哥教育网络班21期+第9周课程练习

1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;

#!/bin/bash
while read line; do
    if [[ $line =~ .*/sbin/nologin$ ]]; then
        let i++
    else
        let j++
    fi
done < /etc/passwd
echo "Number of log shell:$j."
echo "Number of nolog shell:$i."

2、写一个脚本

    (1) 获取当前主机的主机名,保存于hostname变量中;

    (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;

    (3) 否则,则显示当前主机名;

#!/bin/bash
hostname=`hostname`
if [[ $hostname == "localhost" ]]; then
    hostname www.mageud.com && echo "The hostname changed for www.magedu.com"
else
    echo "The hostname is $hostname."
fi

3、写一个脚本,完成如下功能

    (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;

    (2) 如果存在,则显示此设备上的所有分区信息;

#!/bin/bash
read -p "Please enter a path of device:" path
df $path &> /dev/null
if [ $? -eq 0 ]; then
    parted $path print
else
    echo "$path not exists."
fi

4、写一个脚本,完成如下功能

   脚本能够接受一个参数;

   (1) 如果参数1为quit,则显示退出脚本,并执行正常退出;

   (2) 如果参数1为yes,则显示继续执行脚本;

   (3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash
[ $# -eq 0 ] && echo "Enter something!" && exit 1
case $1 in
quit)
    echo "Quit $0!" && exit 0
    ;;
yes)
    echo "Stay $0! " 
    while true; do
        read -p "Please enter again:" again
        [[ $pathagain == 'quit' ]] && echo "ByeBye!" && exit 0
        [[ $pathagain != 'quit''yes' ]] && echo "Goodbye!" && exit 2
        bash 5.sh $pathagain
    done
    ;;
*)
    echo "Quit!" && exit 2
esac

5、写一个脚本,完成如下功能

   传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;

   (1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.gz;

   (2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.bz2;

   (3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.xz;

   (4) 其它任意值,则显示错误压缩工具,并执行非正常退出;

#!/bin/bash
[ $# -eq 0 ] && echo "Enter something!" && exit 1
case $1 in
gzip)
    tar -czf /backups/etc-`date +%Y%m%d`.tar.gz /etc/ &> /dev/null
    echo "tar and gzip finished!"
    ;;
bzip2)
    tar -cjf /backups/etc-`date +%Y%m%d`.tar.bz2 /etc/ &> /dev/null
    echo "tar and bzip2 finished!"
    ;;
xz)
    tar -cJf /backups/etc-`date +%Y%m%d`.tar.xz &> /etc/ /dev/null
    echo "tar and xz finished!"
    ;;
*)
    echo "Wrong tools!"
    exit 3
    ;;
esac

6、写一个脚本,接受一个路径参数:

   (1) 如果为普通文件,则说明其可被正常访问;

   (2) 如果是目录文件,则说明可对其使用cd命令;

   (3) 如果为符号链接文件,则说明是个访问路径;

   (4) 其它为无法判断;

#!/bin/bash
read -p "Please enter a path of file:" path
if [ -f $path ]; then
    echo "$path is common file.Can access!"
elif [ -d $path ]; then
    echo " $path is directory.Can use cd command!"
elif [ -s $path ]; then
    echo " $path is symbolic."
else 
    echo "Could't know about $path!"
fi

7、写一个脚本,取得当前主机的主机名,判断

   (1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;

   (2) 否则,显示现有的主机名即可;

#!/bin/bash
if [ -z $HOSTNAME -o "$HOSTNAME" == "localhost" -o "$HOSTNAME" == "none" ];then
 hostname mail.magedu.com && sed -i '/^HOSTNAME/d' /etc/sysconfig/network &&
 echo "HOSTNAME=mail.magedu.com" >> /etc/sysconfig/network
else
hostname
fi

8、写一脚本,接受一个用户名为参数;

   (1) 如果用户的id号为0,则显示其为管理员;

   (2) 如果用户的id号大于0且小于500, 则显示其为系统用户;

   (3) 否则,则显示其为普通用户;

#!/bin/bash
[ $# -eq 0 -o $# -gt 1 ]&& echo "only for one user account or too many user account!"&& exit 12
userid=$(id $1 | cut -d " " -f1 | tr "()" " " | cut -d "=" -f2 | cut -d " " -f1)
echo "$userid $1" >/tmp/id.txt
id $1 &>/dev/null && awk '{if($1==0){print $2 " is super user";}else if($1>0 && $1<500)
{print $2 " is System user";}else{print $2 " is Common user";}}' /tmp/id.txt

9、写一个脚本,传递一个用户名参数给脚本;

   (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;

   (2) 否则,则显示无法登录系统;

#!/bin/bash
[ $# -eq 0 -o $# -gt 1 ]&& echo "only for one user account or too many user account!"&& exit 12
if id $1 &>/dev/null;then
userid=$(id $1 | cut -d " " -f1 | tr "()" " " | cut -d "=" -f2 | cut -d " " -f1)
usershell=$(finger $1 | grep "Shell" | cut -d ":" -f3 | cut -d " " -f2)
userend=${usershell:0-2}
echo "$userid $1 $usershell $userend" >/tmp/idtmp.txt
else
echo "user doesn't exisits!"
exit 13
fi
awk '{if($1>=500 && $4=="sh"){print "a user can login!";}else{print "you cannot login!";}}' /tmp/idtmp.txt

10、写一个脚本,完成如下任务 :

   (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;

   (2) 复制目录时,才使用cp -r命令;

   (3) 复制文件时使用cp命令;

   (4) 复制链接文件时使用cp -d命令;

   (5) 余下的所有类型,使用cp -a命令;

#!/bin/bash
sdir="/var/log"
ddir="/tmp/tes1-testn/"
[ ! -e $ddir ] && mkdir $ddir
for i in $(ls -l $sdir | grep "^-" | awk '{print $NF}');do
cp $sdir/$i  $ddir
echo "$sdir/$i copy to $ddir finished!"
done
for m in $(ls -l $sdir | grep "^d" | awk '{print $NF}');do
cp -r $sdir/$m  $ddir
echo  "$sdir/$m copy to $ddir finished!"
done
for n  in $(ls -l $sdir | grep "^l" | awk '{print $NF}');do
cp -d  $sdir/$i  $ddir
echo "$sdir/$n copy to $ddir finished!"
done
for k in $(ls -l $sdir | grep -v "^[-dl]" | awk '{print $NF}');do
cp -a $sdir/$i  $ddir
echo "$sdir/$k copy to $ddir finished!"
done

原创文章,作者:Bazinga,如若转载,请注明出处:http://www.178linux.com/43104

(0)
BazingaBazinga
上一篇 2016-09-06 08:50
下一篇 2016-09-06 08:50

相关推荐

  • 对文件数据操作命令

    对文件数据操作命令   很多时候,我们都会去计算一次数据里头的的相同类型的数据总数或者查看特性的一些信息,在这个时候我们就需要排序与计算之类的命令来辅助,下面就是几个好用的操作文件数据命令。 文件查看命令:cat, tac cat [OPTION]… [FILE]…     -E: 显…

    Linux干货 2016-08-08
  • The first work’s homework

    一、描述计算机的组成及其功能     计算机是由硬件系统(hardware system)和软件系统(software system)两部分组成的。     1.硬件系统:       根据冯诺依曼计算机体系结构模型,中…

    Linux干货 2016-12-05
  • 第二次作业

    一、只显示某目录下隐藏文件    答案1:#ls -d /tmp/*/    但是此答案是我在同学知道下完成,但是原理没明白 如果单独执行        #ls -d  &nbsp…

    Linux干货 2016-08-03
  • 马哥教育网络班25期-第4周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 ~]# cp -r /etc/skel/ /home/tuser1 ~]# cd /home/tuser1 tuser1]# ll -a 总用量…

    Linux干货 2016-12-26
  • 编程真难啊

    上周,在Sun的Java论坛上出现了一个这样的帖子,这个贴子的链接如下:http://forums.sun.com/thread.jspa?threadID=5404590&start=0&tstart=0 LZ的贴子翻译如下: 大家好,我是一个Java的新手,我有一个简单的问题:请问我怎么才能反转一个整数的符号啊。比如把-12转成+12。是…

    Linux干货 2015-04-03
  • GRUB——防止root密码被破解

    GRUB(Boot Loader): ·grub:GRand Unified Bootloader          grub 0.x:grub leagacy          grub 1.x:g…

    Linux干货 2016-09-11

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-07 16:47

    脚本写得非常的漂亮,思路清晰,5题的case..xz)选项还有一点小的瑕疵