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.www58058.com/88401

(0)
469008940469008940
上一篇 2017-11-14 15:18
下一篇 2017-11-15 15:59

相關推薦

  • 對inode的初步理解

    1.什么是inode?     inode中文譯作”索引節點“,是linux操作系統中的一種數據結構,用來存儲文件的元數據信息。在linux系統中每個文件都會分配一個inode,我們也可以把inode看作指針,它永遠指向文件的具體存儲位置。 2.inode中包含了什么信息? * inode 編號 * 用來識別文件類型,以及用于 …

    2017-07-18
  • ansible實踐

    本偏文章以上圖拓撲部署 首先準備了6臺為centos7的主機(確保selinux和iptables不會成阻礙!關閉他們) 我們先來安裝ansible吧! 配置EPEL源過后使用 yum -y install ansible  ?。。。。。。。。。。。。。。。。。。。。。。。。。。。。?! 出師不利啊,安裝ansible就失敗了。!?。。。?! 看了一下…

    Linux干貨 2017-01-27
  • 8.5_Linux文本處理工具和grep之正則表達式的使用

    Linux文本處理工具使用 文件查看工具的使用 cat 命令的使用  此命令一般用于查看文件內容比較多 NAME        cat – concatenate files and print on the&…

    Linux干貨 2016-08-08
  • CollabNetSubversionEdge服務搭建

    準備目錄并設置權限 [root@localhost ~]# mkdir /var/svn [root@localhost ~]# chown bohui:bohui /var/svn/ [root@localhost ~]# chmod 700 /v…

    Linux干貨 2015-10-18
  • 帶你走進PI的世界-Raspbian Pi上實現LAMP

     初識   Raspbian PI                   前段時間,出于興趣,入手一個樹莓派(Raspbian PI…

    Linux干貨 2016-01-05
  • 馬哥教育網絡班20期+第四周課程練習

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 cp -r /etc/skel/ /home/tuser1 chmod o-rwx /home/tuser1/.* 2、編輯/etc/group文件,添加組hadoop。 e…

    Linux干貨 2016-06-26

評論列表(1條)

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

    腳本寫的還行,繼續加油。

欧美性久久久久