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

相关推荐

  • rpm包管理、yum仓库、ftp、http服务、编译安装

    一、软件包管理 1.1 软件包管理 API:Application Programming Interface  应用程序开发接口;程序员面对的编程接口 POSIX:portable operating system:可移植操作系统接口 v   程序源代码 –> 预处理 …

    Linux干货 2016-11-27
  • Bash Shell编程初学基础篇之一

     Bash Shell编程初学基础篇之一 说明: 本文仅供初学Linux  Bash  shell学员参考学习,大神们如有兴趣请批评指正!!!    相信对于很多Linux初学者或者仅仅是听说Linux还没有接触过的同学会有一种神秘感或者不敢碰触的感觉,今天就帮大家揭开它的神秘面纱,其实并没有那么深不可测,只…

    Linux干货 2015-03-29
  • 搭建简单的ftp和http

    搭建简单的ftp和http 我们主要尝试光盘和编译两种方式安装 ftp 首先我们先安装ftp ftp可以用来做文件传输,也可以搭建ftp作为我我们的网络yum源 首先我们要先把镜像连接到我们的Linux中 进入/etc/yum.repos.d/创建一个.repo结尾的文件,在里面配置自己的yum源 [base] baseurl=file:///misc/cd…

    2017-06-15
  • 推荐-Linux Namespace的一点理解

        下文的原文来自: http://blog.csdn.net/preterhuman_peak/article/details/40857117    原文写的非常好,下文中大部分文字描述来自原文,我仅做了一点理解性的插图和实验部分。    &…

    Linux干货 2016-03-28
  • Python装饰器与面向切面编程

    新年好~ 那么,很久没有更新了,其实想想也没多少可以写的,因为Python的文档似乎很全面的说……能做的差不多只有翻译和整理了,英文过关的朋友不妨直接去doc.python.org这里查看相关资料 :) 转载请注明原作者和原文地址,多谢! 今天来讨论一下装饰器。装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理…

    Linux干货 2015-03-12
  • 用户权限以及组权限作业

    1、在/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@localhost testdir]# mkdir -p /data/testdir [roo…

    Linux干货 2016-08-08

评论列表(1条)

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

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