while until 循环用法和 case 条件base编程

写一个脚本:
(1)能接受四个参数:start、stop、restart、status
输入start输出starting,脚本名为finished
(2)其它任意参数均报错退出
#!bin/bash
#author:jian
#date:2017-11-12
#discription:
read -p “please input a string(start|stop|restart|status):” str
case “$str” in
start)
echo “starting”
;;
stop)
echo “stoping”
;;
restart)
echo “stoping”
echo “starting”
;;
status)
echo “status”
;;
*)
echo “error”
exit 1
esac
[root@centos6 script]# sh finished.sh
please input a string(start|stop|restart|status):stop
stoping
[root@centos6 script]#
写一个脚本判,判断给定的用户是否登录了当前系统。
(1)如果登录了,则显示用户已经登录,脚本终止
(2)每3秒,查看用户是否登录。
while true ;do
if who|grep “^$1\>” &> /dev/null ;then
echo “$1 is login”;
break;
fi
sleep 3;
echo “$1 is nologin”
done;
[root@centos6 script]# sh login.sh hi
hi is nologin
hi is nologin
hi is nologin
hi is login
写一个脚本显示用户要查看的信息
cpu) display cpu information
mem) display memory information
disk) display disks information
quit) quit
非此四类选项,则提升错误,并要求用户重新选择,直到给出正确的选择为止;
cat << EOF
cpu) display cpu information
mem) display memory information
disk) display disks information
quit) quit
EOF
read str
until [ “$str” = “cpu” -o “$str” = “mem” -o “$str” = “disk” -o “$str” = “quit” ];do
read -p “please input (cpu|mem|disk|quit): ” str
done;
case $str in
cpu)
lscpu
;;
mem)
free -m
;;
disk)
fdisk -l /dev/sd[a-z]
;;
quit)
echo “exit”
exit 0
;;
esac
[root@centos6 script]# sh until.sh
cpu) display cpu information
mem) display memory information
disk) display disks information
quit) quit
mem
total used free shared buffers cached
Mem: 980 449 531 1 82 134
-/+ buffers/cache: 232 748
Swap: 2047 0 2047
写一个脚本
(1)用函数实现返回一个用户的uid和shell;用户名通过参数传递而来;
(2)提示用户输入一个用户名或输入“quit”退出;
当输入的用户名,则用调用函数显示用户信息;
当输入quit,则退出脚本;进一步的:显示键入用户的相关信息后,再次提醒输出用户名或quit;
read -p “please input username or input \”quit\” to exit: ” username
uname() {
name=$1
if id -u $name &> /dev/null ;then
userId=`grep “^$name” /etc/passwd|cut -d: -f3`
userShell=`grep “^$name” /etc/passwd|cut -d: -f7`
echo “$name”
echo “userId:$userId”
echo “userShell:$userShell”
bash “$0”
else
echo “$name is no exist”;
exit 1;
fi
}
if [ “$username” != “quit” ];then
uname $username
else
exit 0;
fi
[root@centos6 script]# sh name.sh
please input username or input “quit” to exit: root
root
userId:0
userShell:/bin/bash
please input username or input “quit” to exit:

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

(0)
上一篇 2017-11-14 15:18
下一篇 2017-11-15 15:59

相关推荐

  • lamp的搭建

    方法一编译安装amp:   1.系统环境:CentOS 6,7       CentOS6:apr,apr-util的版本为1.3.9,不适合httpd-2.4    CentOS7:apr,apr-util的版本为1.4+2.开发环境需要安装:    Developm…

    Linux干货 2016-10-16
  • puppet部署多台服务器

    利用puppet实现自动化部署 配置前准备:   图中:蓝线表示各个服务器之间通信      红线表示puppetmaster主机向各个agent主机部署信道 A主机puppet-master主机:192.168.126.129 B主机做两种服务:keepalived高性能和nginx反代  &nb…

    2017-07-23
  • shell脚本一键分区

    #!/bin/bash #实现硬盘自动分区助手 echo "当前所有分区:" echo `fdisk -l|grep "Disk /dev/[sh]d"|cut -d: -f1|awk '{print $2 &q…

    Linux干货 2016-07-26
  • 一键搭建mysql集群系列一

    一键自动安装mysql 5.7 shell脚本自动化安装二进制mysql-5.7 本节主要用到四个shell脚本 和 一台资料存储机器(IP:192.168.42.26) install_mysql.sh 自动化安装mysql脚本 ntpdate.sh 时间同步脚本 system_init.sh 系统初始化脚本 yum.sh yum源配置脚本 执行步骤: 1…

    2017-05-13
  • 初学Linux练习题

    1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中 tr ‘a-z’ ‘A-Z’ < /etc/issue  > /tmp/issue.out 2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中 3、一个linux用户给root发邮件…

    2017-11-19
  • vsftpd基于mysql实现用户认证

    一、前言   ftp介绍:     ftp全程是File Transfer Protocol(文件传输协议),方便于实文件交换;但是在文件传输以及账号密码发送时都是以明文传输,因此是一个明文协议 ftp是C/S方式:   常见的客户端有:     GUI方式:browers、FileZilla-cl…

    Linux干货 2015-06-15

评论列表(1条)

  • 马哥教育
    马哥教育 2017-12-02 09:26

    脚本写的还行,继续加油。