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

相关推荐

  • 文本编辑秘籍之vim宝典

    一、初识Vim Vim是从 vi 发展出来的一个文本编辑器。其功能非常强大,熟练掌握vim的常用操作和快捷操作能让我们从繁杂的文本处理任务中杀出血路,在运维的道路上愉快前行。 vim使用简单命令组合来完成复杂操作,同时也支持基本正则表达式。 二、拨开vim迷雾: 2.1 使用vim打开一个文件: 如果该文件存在,文件被打开并显示内容;如果该文件不存在,当编辑…

    Linux干货 2016-08-10
  • 马哥教育网络班21期+第2周课程练习

    一:在Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例。     在Linux中,文件管理类命令主要有cp,mv,rm三个     其中cp命令的使用格式如下:           …

    Linux干货 2016-07-16
  • 文件搜索之find的使用

    一.find介绍:   find是Linxu中一个十分常用的文件查找工具,其工作原理是通过遍历启起始路径下文件层级结构完成的,其特点是查找速度慢,能做到实时查找和精确查找。 二.使用方法: find  start_path search_conditions  action 1.statr_path:所要查找的文件的起始位置,默…

    Linux干货 2016-11-01
  • Linux文件类型及颜色标识整理

    Linux文件类型及颜色标识整理 M21陆东贵 使用工具: ls命令     ls –l:以常格式显示文件及目录的详细信息     例如:-rw-r–r–  1 root root       0 10月 …

    Linux干货 2016-10-18
  • RAID解说

    RAID(RedundantArrays of Inexpensive Disks,RAID),又叫独立的磁盘阵列。有“价格便宜具有冗余能力的磁盘阵列”之意。原理是利用数组方式来作磁盘组,配合数据分散排列的设计,提升数据的安全性。磁盘阵列是由很多价格较便宜的磁盘,组合成一个容量巨大的磁盘组,利用个别磁盘提供数据所产生加成效果提升整个磁盘系统效能。利用这项技术…

    2017-03-14
  • HTTP协议

    HTTP协议   HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议。HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)。HTTP协议工作于客户端-服务端架构之上。浏览器作为HT…

    Linux干货 2017-02-15

评论列表(1条)

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

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