马哥教育网络班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

相关推荐

  • Linux文件与目录管理之权限与命令之间的关系

    Linux文件与目录管理之权限与命令之间的关系 我们知道权限对于用户账户来说是非常重要的,因为它可以限制用户能不能读取/新建/删除/修改文件或目录。在这我们就来说明下什么命令在什么样的权限下才能够运行。         让用户能进入某目录成为“可工作目录”的基本权限是什么     &nbs…

    Linux干货 2017-04-24
  • 对软连接进行cp,rm

    软连接的原理 图1 由 1 号inode 读取到连结档的内容仅有档名,根据档名链接到正确的目录去取得目标文件的inode , 最终就能够读取到正确的数据了。 软连接的使用 创建软连接,从下图可以看出软连接的大小是11个字节,对比/etc/passwd和passwdlns可知他们不是同一个文件。 在复制cp的时候对软连接的处理 对指向文件的的软连接:使用cp …

    Linux干货 2017-02-25
  • shell进阶之循环

    循环执行,将某代码段重复运行多次

    重复运行多少次:

    循环次数事先已知

    循环次数事先未知

    有进入条件和退出条件

    for, while, until

    Linux干货 2017-12-24
  • N21沉舟15周作业

    1、总结sed和awk的详细用法; 2、删除/boot/grub/grub.conf文件中所有行的行首的空白字符; #  sed 's@^[[:space:]]@@' /boot/grub/grub.conf 3、删除/etc/fstab文件中所有以#开头,后跟至少一个空白字符的行的行首的#和空白字…

    Linux干货 2016-11-14
  • M20 – 1- 第二周(2):硬链接与软链接的区别

    在讲硬链接与软链接的区别之前,我们首先了解inode,了解inode让我们更容易理解何为硬链接和软链接。 inode概念        何为inode,inode就是索引节点,而inode表中存放着文件的元数据,何为元数据,元数据就是文件名称、大小、时间戳、所有者、权限、inode等信息,而文件中的内容就是文件的数据,…

    Linux干货 2016-08-02
  • 马哥教育网络班22期+第2周课程练习

    一、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。     Linux上文件管理类命令常用的有:pwd、ls、cd、cp、touch、mv、rm、rmdir (1)pwd:显示当前路径,打印工作目录(Print Working Directory) (2)ls:查看指定目录下的内容 参数 说明 -a 列举目录中的…

    Linux干货 2016-08-29

评论列表(1条)

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

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