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

相关推荐

  • Linux的正则表达式grep,egrep

    Linux的正则表达式grep,egrep 一、概念 正则表达式是对字符串操作的一种逻辑公式,用事先定义好的一组特殊字符,组成一个“规则字符集合”,根据用户指定的文本模式对目标文件进行逐行搜索匹配,显示能被模式匹配到的结果。 给定一个正则表达式和另一个目标字符串,我们可以从给定的字符串中通过匹配模型,过滤字符串中不想要的的字符串,得到目标字符串,减少工作量。…

    Linux干货 2017-05-07
  • 自制linux和内核编译

    自制linux和内核编译 1、分区并创建文件系统 fdisk /dev/sdb分两个必要的分区/dev/sdb1对应/boot /dev/sdb2对应根/mkfs.ext4 /dev/sdb1mkfs.ext4 /dev/sdb2 2、挂载boot mkdir/mnt/bootmount /dev/sdb1 /mnt/boot 3、安装grub grub-i…

    Linux干货 2016-09-16
  • vim编辑器

    一、vim简介    vi: Visual Interface,文本编辑器     文本: ASCII, Unicode     文本编辑种类:         行编辑器: sed…

    Linux干货 2016-08-10
  • 【LINUX编译安装程序】Centos7.4编译安装httpd 2.4.29

    举例:Centos7.4编译安装httpd 2.4.29

    Linux干货 2018-03-17
  • VIM入门及进阶

    什么是VIM?     VIM类似于Vi编辑器, 它是一个功能强大、可高度定制的文本编辑器, 是一个纯粹的自由软件。注意:vi和vim不完全相同 为什么要使用VIM?     相信大多数人接触Linux时使用的第一个文本编辑器都不是VIM,很多人看到VIM复杂的命令操作就望而祛步…

    系统运维 2016-03-04
  • LVS模型练习

    本次涉及到的练习:nat练习、dr练习、FWM机制练习、mysql集群练习、httpd的集群练习、httpd和mysql结合调度的练习 yum install ipvsadm nat练习     设置:     (1)centos7.2作为lvs调度,有两块网卡,(公网地址)其中…

    Linux干货 2016-10-30

评论列表(1条)

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

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