码迷,mamicode.com
首页 > 其他好文 > 详细

bash编程练习脚本

时间:2017-05-06 17:48:54      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:bash编程练习脚本

1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);

分别这两类用户的个数;通过字符串比较来实现;

通过while循环遍历来实现。

while循环的特殊用法(遍历文件的行):

         while  read VARIABLE; do

                   循环体;

         done  < /PATH/FROM/SOMEFILE

                   依次读取/PATH/FROM/SOMEFILE文件中的每一行,且将基赋值给VARIABLE变量;

[root@localhosthome]# vim  login.sh

#!/bin/bash

#

#Author=dylan

login_user=0

nologin_user=0

while read line; do

         usershell=$(echo$line | cut -d: -f7)

         if[ "$usershell" == "/sbin/nologin" ];then

                   letnologin_user++

         else

                   letlogin_user++

         fi

done < /etc/passwd

echo "Number of userslogin:$login_user"

echo "Number of usersnologin:$nologin_user"

代码如下:

技术分享

执行如下:

技术分享

2、写一个脚本

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

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

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

vim hostnametest.sh

#!/bin/bash

#

#Author=dylan

hostname=$(uname -n)

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

         hostnamewww.magedu.com

else

         echo$hostname

fi

代码如下:

技术分享

执行如下:

技术分享

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

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

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

vim devtest.sh

#!/bin/bash

#

#Author=dylan

if [ $# -lt 1 ];then

         echo"Plesase supply a disk path:Usage /dev/[s|h]d[a-z]"

         exit2

fi

if [ -b $1 ]; then

         fdisk-l $1

else

         echo"Invalid file"

fi

代码如下:

技术分享

执行如下:

技术分享

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

脚本能够接受一个参数;

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

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

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

[root@localhost home]# vim  arg.sh

#!/bin/bash

#

#Author=dylan

if [ $# -ne 1 ];then

         echo"enter only one agrument"

         exit1

else

         case$1 in

         "quit")

                   echo"quitting..."

                   exit0

                   ;;

         "yes")

                   echo"continuing..."

                   exit0

                   ;;

         *)

                   echo"interrupt..."

                   exit2

         esac

fi

代码如下:

技术分享

执行如下:

技术分享

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

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

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

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

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

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

#!/bin/bash

#

#Author=dylan

if [ $# -ne 1 ];then

         echo"enter only one argument"

         exit1

else

         case$1 in

         "gzip")

                   tar-zcvf /backups/etc-`date +"%Y%m%d"`.tar.gz /etc &> /dev/null

                   echo"use gzip "

                   exit0

         ;;

         "bzip2")

                   tar-jcvf /backups/etc-`date +"%Y%m%d"`.tar.bz /etc &> /dev/null

                   echo"use bzip2 "

                   exit0

         ;;

         "xz")

                   tar-Jcvf /backups/etc-`date +"%Y%m%d"`.tar.xz /etc &> /dev/null

                   echo"use xz "

                   exit0

         ;;

         *)

                   echo"invalid compress tools"

                   exit2

         esac

fi

代码如下:

技术分享

执行如下

技术分享

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

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

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

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

(4)其它为无法判断;

[root@localhost home]# vim filetest.sh

#!/bin/bash

#

#Author=dylan

if [ $# -ne 1 ];then

         echo"enter only one agrument"

         exit1

else

         if[ -f $1 ];then

                   echo"It is a normal file that can be accessed normally"

         elif[ -d $1 ];then

                 echo"It is a directory file,then you can use the cd command"

         elif[ -L $1 ];then

                   echo"It is a aymbolic link file,the description ia an access path"

         else

                   echo"It can not be judged"

         fi

fi

 

代码如下:

技术分享

执行如下:

技术分享

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

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

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

[root@localhost home]# vim nametest.sh

#!/bin/bash

#

#Author=dylan

name=$(uname -n)

if [ -z "$name" -o  "$name" == "localhost" -o"$name" == "none" ];then

         hostnamemail.magedu.com

         echo"hostname is mail.magedu.com"

else

         echo$name

fi

代码如下:

技术分享

 

执行如下:

技术分享

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

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

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

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

 

[root@localhost home]# cat idtest.sh

#!/bin/bash

#

#Author=dylan

if [ $# -ne 1 ];then

         echo"enter only one agrument"

         exit1

else

         id$1 &>/dev/null

         if[ $? -ne 0 ];then

                   echo"user does not exist"

                   exit2

         else

                   userid=$(id-u $1)

                   if[ $userid -eq 0 ];then

                            echo"sys admin"

                   elif[ $userid -gt 0 -a $userid -lt 500 ];then

                            echo"sys user"

                   else

                            echo"general user"

                   fi

         fi

fi

 

代码如下:

技术分享

执行如下:

技术分享


本文出自 “11290766” 博客,请务必保留此出处http://rylan.blog.51cto.com/11290766/1922679

bash编程练习脚本

标签:bash编程练习脚本

原文地址:http://rylan.blog.51cto.com/11290766/1922679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!