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

相关推荐

  • 上下文管理练习(为加法函数计时)

    上下文管理(为加法函数计时) 为加法函数计时 使用装饰器显示该函数的执行时长 使用上下文管理显示该函数的执行时长 装饰器实现 import time import datetime from functools import wraps def logger(fn): @wraps(fn) # wraps(fn)(wrapper) def wrapper(*…

    2017-11-18
  • Linux的获取系统的帮助信息及man文档说明

    Linux的获取系统的帮助信息及man文档说明 帮助命令有内部命令帮助和外部命令帮助两种 内部命令获得帮助使用下面命令 # help COMMAND 例如: [root@localhost ~]# type type type is a shell builtin [root@localhost ~]# help type type: type [-afpt…

    2018-02-28
  • ansible-yaml初级语法(hosts、remote_user、tasks)

    环境准备:     1、主控节点IP:172.16.16.9     2、两个被控节点:1)172.16.16.48   2)172.16.16.50     3、hosts配置如下:       &…

    Linux干货 2016-11-28
  • 数组的基本应用

                                   数组 变量:存储单个元素的内存空间v 数组:存储多个元素的连续的内存空间,相当于多个变量的集合。v 数组名和索引 索引:编号从0…

    Linux干货 2016-08-24
  • Linux 的shell脚本编程

    shell脚本编程 程序:指令+数据 程序编辑风格:             过程式:以指令为中心,数据服务于指令             对象式:以数据为中心,指令服务于数据 shell程序:提供了编程能力,解释执行 计算机:运行二进制指…

    Linux干货 2016-08-15
  • Linux网络属性管理(一)

    MarkdownPad Document Linux网络属性管理(一) 将一台linux主机接入互联网一般需要配置以下几个方面的参数: IP/mask –>  IP地址和子网掩码 路由 –> 默认网关 DNS服务器 配置方式 静态指定:   1、ifcfg:ifconfig,route,netstat   2、i…

    Linux干货 2016-06-12

评论列表(1条)

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

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