Linux基础知识——SHELL之循环

1、写一个脚本,判断当前系统用户shell是否都为可登陆shell(即非/sbin/nologin),分别计算两类用户的个数(通过比较字符串实现)

#!/bin/bash
#       check the user could login by default or not,and how many it is 
#       20161211
declare logini
declare nologinj
for ((i=1;i<=$(cat /etc/passwd|wc -l);i++))
do
defaultshell=$(head -$i /etc/passwd|sed '$!d'|cut -d':' -f7)
        if [ $defaultshell == /sbin/nologin ];then
        logini=$(($logini+1))
        else
        nologinj=$(($nologinj+1))
        fi
done
        echo "the default login_user has $logini,the others has $nologinj"

或者

#!/bin/bash
#
#

declare nologini
declare othersj

for ((i=1;i<=$(cat /etc/passwd|wc -l);i++))
do

if [ $(head -$i /etc/passwd|sed '$!d'|sed 's/^.*://') == /sbin/nologin ]
   then
    nologini=$(($nologini+1))
else
    othersj=$(($othersj+1))
fi

done
echo "the default login users is $nologini,the others is $othersj"

2、写一个脚本:1)获取当前主机的主机名,保存在变量hostname中;2)判断此变量的值是否是localhost,如果是,将当前主机名更改为www.mageedu.com;3)如果不是,则显示主机名

#!/bin/bash
#   check the $HOSTNAME if it's localhost,then change it to www.mageedu.com ;else display it
#   20161212

hostname=$(hostname)
if [ $hostname == localhost ] ||[ $hostname == localhost.localdomain ]
then 
    hostname "www.mageedu.com"
    echo -e 'I change HOSTNAME is $hostname\n'
else
    echo -e $hostname
fi

3、写一个脚本实现如下功能:1)传递一个磁盘设备文件路径给脚本,判断此设备是否存在;2)如果存在,则显示此设备上所有分区信息

#!/bin/bash
#       please input a device ,check if it's a block device ,so display the partition
#

read -p "Please input a block device,like:sda、sdb:" device

if [ -b /dev/$device ]
then
        echo "the $device is a block device:/dev/$device;and its partition like:"
        lsblk /dev/$device
else
        echo "$device is not exist!"
fi

4、写一个脚本实现以下功能:脚本能接受一个参数1)如果参数1为quit,则显示退出脚本,并执行正常退出;2)如果参数1为yes,则显示继续执行脚本;3)否则,参数1为其他任意值,均执行非正常退出

#!/bin/bash
#       while you input 'quit',the shell exit 0;input 'yes' the shell continue;input other command the shell exit 1
#

read -p "please input yes,quit,*:" l 
case $l in   
        yes)
                echo "the shell end with no error"
                exit 0
        ;;
        quit)
                echo "the shell continue"
                continue
        ;;
        *)
                echo "the shell out within error"
                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
#       tar /etc/* with bzip2,gzip,xz
#


if [ $# == 0 ]
        then
        echo " you must choose a option(like bzip2,gzip,xz)!"
        else
        case $1 in

        gzip)
                tar -czvf /backups/etc-20161212.tar.gz /etc/*
        ;;
        bzip2)
                tar -P -cjvf /backups/etc-20161212.tar.bz2 /etc/*     
        ;;
        xz)
                tar -Jcvf /backups/etc-20161212.tar.xz /etc/*
        ;;
        esac
        ls -al /backups
fi

6、写一个脚本接受一个路径参数:1)如果是普通文件,则说明其可被正常访问;2)如果是目录文件,则说明可对其使用cd命令;3)如果是符号链接文件,则说明是个访问路径;4)其他为无法判断

#!/bin/bash
#   check the file or directory 's type
#   20161213

if [ $# -eq 0 ];then
   echo "you must insert a file or directory!"
elif [ -f $1 ];then
    echo "$1 is a file and you can read/write it"
elif [ -d $1 ];then
    echo "$1 is a directory,you can access it with COMMAND 'cd'"
elif [ -l $1 ];then
    echo "$1 is a link file"
else 
    echo "i can't fond what type it is"
fi

7、写一个脚本,取得当前主机的主机名,判断1)如果主机名为空或者为localhost,或者为“(none)”,则将其命名为mailmagedu.com;2)负责,显示现有的主机名即可

同题2

8、写一个脚本,接受一个用户名为参数:1)如果用户的id号为0,则显示为管理员;2)如果用户的id号大于0且小于500,则显示其为系统用户;3)否则,显示其为普通用户

#!/bin/bash
#   check the user's type,it's operater or ordinery user
#   20161213

if [ $# -eq 0 ];then
    echo "Please input a username!just one"
elif `id -u $1>>/dev/null 2>&1`;[ $? != 0 ];then
    echo "the user $1 isn't fond" && exit 1
   elif [ `id -u $1` -eq 0 ];then
    echo "$1 is root"
   elif [ `id -u $1` -lt 500 ];then
    echo "$1 is a operator"
   else
    echo "$1 is a user"
   fi

9、写一个脚本,传递一个用户名参数给脚本;如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system"类的字符串

#!/bin/bash
#   check user's ID greater than 500 and the last two character of the default SHELL is sh
#   20161213


if [ $# -eq 0 ];then
    echo 'Please insert a username!'
elif `id -u $1 >>/dev/null 2>&1`;[ $? != 0 ];then
    echo "The user isn't fond" 
elif [ $(id -u $1) -ge 500 ] && [ $(grep $1 /etc/passwd|sed 's/.*://'|grep -o "sh$") == sh ];then
    echo "The user $1 can login system"
else
    echo "The user'ID is $(id -u $1),the user cannt login"
fi

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

(0)
396064847396064847
上一篇 2016-12-13
下一篇 2016-12-13

相关推荐

  • http2

    练习:     (1)基于主机名实现三个虚拟主机     (2) 每虚拟主机使用独立的访问日志和错误日志     (3) 第三个虚拟主机的/admin要进行用户访问认证     (4) 在第二个虚拟主…

    Linux干货 2016-10-09
  • 查找 -数据结构

    几种查找算法:顺序查找,折半查找,分块查找,散列表 一、顺序查找的基本思想:  从表的一端开始,向另一端逐个按给定值kx 与关键码进行比较,若找到,查找成功,并给出数据元素在表中的位置;若整个表检测完,仍未找到与kx 相同的关键码,则查找失败,给出失败信息。 说白了就是,从头到尾,一个一个地比,找着相同的就成功,找不到就失败。很明显的缺点就是查找效…

    Linux干货 2015-07-28
  • N25期–第六周作业

    请详细总结vim编辑器的使用并完成以下练习题 1、 复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; %s@^[[:space:]]\{1,\}.*@#&@ 2、 复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf…

    Linux干货 2017-01-17
  • Linux启动之grub详解,故障排除,自建linux

    概述     上篇我们以CentOS6为例介绍了一下系统启动流程,本篇将承接上篇,详细的介绍一下系统启动流程中的grub,以及系统启动过程中的各种故障的排除,以及利用现有内核自己构建一个能够正常启动的简单Linux系统。具体分为一下几个部分:     1、grub相关概念详解 &…

    Linux干货 2016-09-13
  • shell脚本编程入门

    什么是shell脚本,其实,shell脚本就是利用shell的功能所写的一个程序,这个程序是使用纯文本文件,将一些shell的语法与命令(包含外部命令)写在里面,搭配正则表达式、管道命令与数据流重定向等功能等这些命令的组合起来,以达到我们所想要的目的。 程序编程风格有两种: 过程式:以指令为中心,数据服务于指令。 对象式:以数据为中心,指令服务于数据。 过程…

    Linux干货 2016-08-18
  • linux文本处理三剑客—grep

      cat:concatenate 文本文件查看工具 cat [option] filename… -n:给显示出来的文本行加上编号 -b:非空行编号 -V:显示 ^ -E:显示行结束符$ -T:显示制表符 -A:显示所有控制符-A=-VET -s:压缩连续…

    系统运维 2016-08-05

评论列表(1条)

  • 马哥教育
    马哥教育 2016-12-16 15:00

    脚本可以先运行下~~第2个脚本,犯了一个强弱引用的错误;第3个脚本,输入 “yes” 和 “quit” 的执行结果和要求不一致;第3个脚本,漏掉了任意项;第7个脚本,建议动手换个方式实现下;等等
    不过整体看下来还是不错的。加油~