shell脚本参数练习

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

!/bin/bash

#

login_user=0

nologin_user=0

for i in $(cat /etc/passwd | cut -d : -f 7);do

if [ $i == “/sbin/nologin” ];then

let nologin_user++

else

let login_user++

fi

done

echo “login_user=$login_user,nologin_user=$nologin_user.”

2、写一个脚本

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

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

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

#!/bin/bash

#

hostname=$(echo $(hostname))

if [[ $hostname == “localhost” ]];then

hostname www.magedu.com

else

echo “$hostname”

fi

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

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

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

#!/bin/bash

#

if [ $# -lt 1 ];then

echo “At least one device file.”

exit 1

fi

if [ -e $@ ];then

echo “$(fdisk -l $@)”

else

echo “Unknown device”

exit 2

fi

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

脚本能够接受一个参数;

(1)如果参数1为quit,则显示退出脚本;

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

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

#!/bin/bash

#

cat << EOF

1.quit

2.yes

EOF

read -p “Please enter(quit or yes):” name

if [[ $name == quit ]];then

echo “exit the script.”

elif [[ $name == yes ]];then

echo “Continue with the script.”

else

echo “Unknown scirpt.”

exit 2

fi

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-20160603.tar.bz2

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

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

#!/bin/bash

#Use tar and gzip, bzip2, xz archive compression/etc/directories, and name it/backups/etc

-20string606string3./ backups/etc – 20string606string3. The tar. Bz ip2./

backups/etc – 20string606string3. The tar. Xz

cat << EOF

gzip)Archive using tar and gzip

bzip2)Archive using tar and gzip

xz)Archive using tar and gzip

EOF

read -p “please enter[gzip|bzip2|xz]:” string

if [[ $string == gzip ]];then

tar -zcf /backups/etc-$(date +%Y%m%d).tar.gz /etc

elif [[ $string == bzip2 ]];then

tar -jcf /backups/etc-$(date +%Y%m%d).tar.bz2 /etc

elif [[ $string == xz ]];then

tar -Jcf /backups/etc-$(date +%Y%m%d).tar.xz /etc

else  echo “Compression error.”

exit 1

fi

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

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

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

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

(4)其它为无法判断;

#!/bin/bash

#

if [ $# -lt 1 ];then

echo “At lease one arguments.”

exit 1

fi

if [ -f $1 ];then

echo “Normal access.”

elif [ -d $1 ];then

echo “Use  cd command.”

elif [ -L $1 ];then

echo “Access path.”

else  echo “Unknown file.”

fi

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

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

#!/bin/bash

#

hostname=$(echo `hostname`)

if [ -z “$hostname” -o “$hostname” == “loclahost” -o “$hostname” == “none” ];then

hostname www.magedu.com

else

echo “$hostname”

fi

 

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/87296

(0)
N27_shijinlongN27_shijinlong
上一篇 2017-09-17 19:57
下一篇 2017-09-17 21:51

相关推荐

  • ArchLinux折腾记

    刚接触linux的时候, 听到前辈说到这样一个系统,  说是linux 技能纯熟才能驾驭的了ArchLinux , 最近正好有个半天闲, 于是抽空,  佐以官方文档,  来认认真真的折腾一下ArchLinux Contents ArchLinux简介  准备工作  分区设置  基本功能安装 &nbs…

    Linux干货 2016-09-29
  • MongoDB

    Edit MongoDB 手册 MongoDB 手册 第一章 Introduction MongoDB入门学习目录(建议) Databases Collections Documents 第二章 部署安装 1. Import the MongoDB public key 2. Configure the package management system (…

    Linux干货 2017-04-08
  • 模块化的php编译amp和fpm化的php编译amp;

    编译安装amp 一,编译环境前准备工作 (1) 系统环境:CentOS 6,7     CentOS 6:apr, apr-util的版本为1.3.9,不适用于httpd-2.4;     CentOS 7:apr, a…

    Linux干货 2016-10-17
  • 02葵花宝典之bash特性及文件入门

    文件管理 目录 命令替换 命令执行状态 元数据 时间戳

    2018-03-11
  • bash 函数

        函数可以让多条shell的命令组成语句块,实现代码重用和模块化编程。让我们在不用重复编写一些脚本,下面让我们来了解函数。 1,函数的简介 函数在脚本中使用应先定义,声明,再可以调用。 函数function 是由若干条shell 命令组成的语句块,实现代码重用和模块化编程。 函数与shell 程序形式上是相似的,…

    Linux干货 2016-08-24
  • iptables之froward

    先克隆一台centos服务器添加虚拟网卡 同时两个服务器配上host only的网卡 echo 1 > /proc/sys/net/ipv4/ip_forward打开网卡间转发 有回包 但是没有10.0.0.3还是没有ping通原因是192.168.68.134 的网关指向了192.168.68.2 而不是192.168.68.144添加一条指向192…

    2017-11-12