Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

N22-第七周作业

$
0
0

第九周

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

#!/bin/bash

#

declare -i a=0

declare -i j=0

while read line;do

if [ `echo $line|awk -F':' '{print $7}'` == "/sbin/nologin" ];then

let a++

else

let j++

fi

done </etc/passwd

echo "The user of shell is /sbin/nologin is $a"

echo "The user of shell is not /sbin/nologin is $j"

2、写一个脚本

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

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

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

#!/bin/bash

#

hostname=${hostname}

if [ $hostname=="localhost" ];then

/bin/hostname www.magedu.com

[ $? -eq 0 ] && echo "already"

else

hostname

fi

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

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

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

#!/bin/bash

#

if [ -b $1 ];then

if fdisk -l $1;then

fdisk -l $1

fi

else

echo "no such device"

fi

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

脚本能够接受一个参数;

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

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

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

#!/bin/bash

#

case $1 in

"quit")

echo "quiting…"

;;

"yes")

echo "continue…"

;;

*)

echo "innormol quit"

exit 1

;;

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

#

if [ $# -ne 1 ];then

echo "para error please exec again"

#!/bin/bash

#

if [ $# -ne 1 ];then

echo "para error please exec again"

exit 2

fi

! [ -e /backups ] && mkdir /backups

case $1 in

"gzip")

tar -Pzcf /backups/etc-20160613.tar.gz /etc && echo "finished"

;;

"bzip2")

tar -Pjcf /backups/etc-20160613.tar.bz2 /etc && echo "finished"

;;

"xz")

tar -PJcf /backups/etc-20160613.tar.xz /etc && echo "finished"

;;

*)

echo "No such tools,quiting…"

exit 1

;;

esac

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

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

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

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

(4) 其它为无法判断;

#!/bin/bash

#

if [ $# -ne 1 ];then

echo "The para error"

fi

if [ -f $1 ];then

echo "The $1 could be accessed"

elif [ -d $1 ];then

echo "The $1 could be 'cd'"

elif [ -L $1 ];then

echo "The $1 is a access path"

else

echo "we can not judge"

fi

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

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

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

#!/bin/bash

#

if [ `hostname` == "localhost" ];then

hostname mail.magedu.com1

elif [ -z `hostname` ];then

hostname mail.magedu.com2

elif [ `hostname` == "'(none)'" ];then

hostname mail.magedu.com3

else

echo "`hostname`"

fi

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

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

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

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

#!/bin/bash

#

if [ $# -ne 1 ];then

echo "error para"

exit 1

fi

if id $1&>/dev/null;then

if [ `id -u $1` -eq 0 ];then

echo "administrator"

elif [ `id -u $1` -gt 0 -a `id -u $1` -lt 500 ];then

echo "system user"

else

echo "common user"

fi

else

echo "no such user"

fi

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

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

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

#!/bin/bash

#

if [ $# -ne 1 ];then

echo "The para error"

exit 1

else

if id $1 &>/dev/null;then

if [ `id -u $1` -ge 500 -a `cat /etc/passwd|grep ^$1|grep -o "sh"$` == "sh" ];then

echo "a user can log system"

else

echo "$1 can not log in system"

fi

else

echo "no such this user"

fi

fi

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

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

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

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

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

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

#!/bin/bash

#

FILE=/var/log

DES=/tmp/test1-testn

if [ -e $DES ];then

echo "$DES exit"

else

mkdir $DES &>/dev/null && echo "make directory success"

fi

for i in `ls $FILE`;do

if [ -d $FILE/$i ];then

cp -r $FILE/$i $DES && echo "d done"

elif [ -f $FILE/$i ];then

cp $FILE/$i $DES && echo "f done"

elif [ -L $FILE/$i ];then

cp -d $FILE/$i $DES && echo "L done"

else

cp -a $FILE/$i $DES && echo "a done"

fi

done


Viewing all articles
Browse latest Browse all 11063

Trending Articles