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)
469008940469008940
上一篇 2017-11-14 15:18
下一篇 2017-11-15 15:59

相关推荐

  • Linux 计划任务快速理解入门

    计划任务简单来说是一个我们规定的时间去执行我们需要的脚本、命令及需要Linux做的事情。 以前写过一个脚本,由于当时没有学过计划任务,所以写起来特别痛苦,该脚本的需求是每天晚上11:00 备份/etc目录 当时特别小白,于是这么写的: VimrebootScript ‘ #!/bin/bash whiletrue;do If [ -d/app/e…

    Linux干货 2017-05-15
  • Bash Shell之数组简介

    Bash Shell之数组简介   一、数组基本概念   数组是内存中的存储空间,连续的多个存储单元;bash中只支持一维数组,支持稀疏格式 ,参数个数没有限制。 二、数组基本语法格式   1、声明一个数组          declare…

    Linux干货 2015-05-11
  • keepalived+nginx 模型示例

    原理为: 调度器 利用 keepalived 保持高可用性,实现对系统的监控和VIP 的floating NGINX 利用upstream模块进行调度 关键点: keepalived 对NGINX 状态的监控: //利用配置文件中设定的脚本对调度器的nginx 进程的监控 实验步骤: 基于上一篇LVS-DR架构来做,具体LVS-DR架构请参考上一篇 先设置主…

    2017-05-13
  • Linux计划任务

    计划任务 工作当中有时候需要将某件事情在未来的某个特定的时间执行,而自己确不在机器旁,该如何是好?像这样在每天特定的时间内去安排做一些事情这样,一种事情我们就称之为例行性的计划任务,其实在我们的操作系统当中都有类似的例行性任务计划功能,那如何去像设定闹钟一种在Linux上指定例行性计划任务并实施,主要有两种工具:at和crontab   计划任务分…

    Linux干货 2016-09-19
  • Linux基础知识之忘记root密码

    学习之后修改linux用户密码:(物理机端才能修改) ①在读秒时按下任意键一下即可;       ②在下面的界面,选择敲a键,进入命令行模式;       ③在下面的界面的末尾输入 1(表示进入单用户模式);      …

    Linux干货 2016-07-22
  • iptables介绍和使用

    iptables/netfilter:    Packets Filter Firewall:          包过滤型防火墙:  Firewall:隔离工具,工作于主机或网络的边缘处,对经由的报文根据预先定义的规则(识别标准)进行检测,对于能够被规则匹配到的报文实行…

    Linux干货 2017-04-29

评论列表(1条)

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

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