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

相关推荐

  • httpd 2.2(Apache Web服务器)

    apache (Web服务器) 简介: Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。    Apache HTTP服务器是一个模块化的服…

    Linux干货 2016-03-30
  • CentOS7 实现网卡绑定

    Bonding ;理念 将多块网卡绑定同一IP地址对外提供服务,可以实现高可用 或者负载均衡。直接给两块网卡设置同一IP地址是不可以的 。通过bonding,虚拟一块网卡对外提供连接,物理网卡的 被修改为相同的MAC地址 banding工作模式 Mode 0 (balance-rr) 轮转(Round-robin)策略: 从头到尾顺序的在每一个slave 接…

    2017-12-19
  • Linux的硬链接与软链接

    Linux 的硬链接与软链接 文件由文件名与数据组成,这在 Linux 上被分成两个部分:用户数据 (user data) 与元数据 (metadata)。用户数据,即文件数据块 (data block),数据块是记录文件真实内容的地方;而元数据则是文件的附加属性,如文件大小、创建时间、所有者等信息。在 Linux 中,元数据中的 inode 号(inode…

    Linux干货 2016-10-25
  • 网卡别名及多网卡配置

    网卡别名 对于要在不同网段环境中使用的设备有很大的帮助。     要使用网卡别名首先要关闭NetworkManager这个服务,防止在后续操作中引起不必要的冲突。 [root@laodeng6 ~]# chkconfig NetworkManager off [root@laod…

    Linux干货 2016-09-06
  • Linux Bash Shell有关变量

    bash 创建一个子bash-n 查看是否有错误-x 逐行显示信息$:显示当前PIDPPID :显示父进程PIDSHLVL :显示当前shell是几级shellpstree 显示进程树-p 同时显示进程号ps -ef 显示进程信息 本地变量 :仅对当前SHELL有效,对子SHELL也无效环境变量:对当前SHELL及子SHELL均有效。 set 显示本地变量和…

    2017-09-16
  • Linux基础学习总结(六)

    一、完成如下练习 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; vim: cp /etc/rc.sysinit /tmp/rc.sysinitvim/tmp/rc.sysinit:%s@^[[:space:]]\+@#&@g sed: sed “s/…

    Linux干货 2016-10-30

评论列表(1条)

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

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