Shell file test, menu presentation idea

Source: Internet
Author: User

---restore content starts---

File test expression
-F file exists and is a normal file
-D file exists and is a directory file
-S file size is not 0 true
-e file exists then true

-R file is saved and readable #文件属性对于root有时不好用
-W Write
-X execution
-L exists and is a linked file
F1-nt F2 F1 than F2 new is true
F1-ot F2 F1 than F2 old is true
These operators can be used for [[]],[],test almost universal, more operators to view man test.

Production Example:
Prog= "Rpcbind"
[-f/etc/sysconfig/$prog] &&. /etc/sysconfig/$prog #如果存在则加载
[-f/etc/sysconfig/$prog] | | Exit 1 #不存在退出


Case 2, curly braces can execute multiple commands
[$q-eq 3]&&{
Echo 1
Echo 2
Echo 3
}

Determine whether it is an integer: Remove "non-numeric" equals itself ["' Echo ' 123a|sed-r ' s#[^0-9]# #g '" = "123a"]

#!/bin/sh
Read-p "pls input" num: "NUM1 num2
A=num1
B=num2
#no. 1
[$#-ne 2]&&{# "$#" indicates the number of variables entered
echo "Usage:num1 num2"
Exit 1
}
#no. 2
[' echo ' $a ' |sed-r ' s#[^0-9]# #g ' "=" $a "]| | {
echo "First arg must be int"
Exit 2
}
#no. 3
[$1-lt $b]&&{
echo "$a < $b"
Exit 3
}

Enterprise Comprehensive case: 1. When the user enters 1 o'clock, the output "start install lamp." "Then execute/server/script/lamp.sh, script content output" lamp is install "after exit script
2. When the user outputs 2 o'clock, the output "start install LNMP. "Then execute/server/script/lnmp.sh, script content output" LNMP is install "after exiting the script
3. Exit the current menu and script when entering 3 o'clock
4. Exit the script when you enter any other ego and give the hint "input error"
5. To make a conditional judgment on the executed script, for example: whether the script exists, whether it can be executed, etc.
Print Simple Selection Menu Example 1,
Cat <<end
1.wangfei
2.gongli
END
Read-p "which do you like?:" A
echo "I guess,you like $a"
Print Simple Selection Menu Example 2, function notation,
Menu () {
Cat <<end
1.[install Lamp]
2.[install LNMP]
3.[exit]
pls input the num you want:
END
}
Menu
Read num
["$num" = "1"]&&{
echo "Start install lamp."
[-x/server/script/lamp.sh]| | {
echo "lamp.sh Error"
Exit 2}
. /server/script/lamp.sh
}
["$num" = "3"]&&{
echo "Bye"
Exit 0
}
echo "Iput Error"
Exit 1
To print a multi-level menu Example:

Cat << EOF
|-----------System Infomation-----------
| DATE: $DATE
| HOSTNAME: $HOSTNAME
| USER: $USER
| IP: $IPADDR
| disk_used: $DISK _SDA
| Cpu_average: $cpu _uptime
----------------------------------------
|****please Enter Your choice:[0-5]****|
----------------------------------------
(1) Configure $SERVER 1 Service
(2) Configure $SERVER 2 Service
(3) Configure Networke Service
(4) Change Passwd
(5) Logs
(0) Quit
Eof
Read-p "Please enter your choice[0-5]:" Input
Case $input in
#web Service
1)
Clear
While ["$flag"-eq 0]
Do
Cat << EOF
----------------------------------------
|****please Enter Your choice:[0-3]****|
----------------------------------------
(1) Start $SERVER 1 Service
(2) Stop $SERVER 1 Service
(3) ReStart $SERVER 1 Service
(0) Back
Eof
Read-p "Please enter your choice[0-3]:" INPUT1
Case $input 1 in
1)
Echo-e "\n>>>>>>>>>>> $DATE Start $SERVER 1" >>/log.txt
$SERVER 1_start 2>>/log.txt
If [$? = = 0];then
echo "Start $SERVER 1 .... ..... ..... ..... .............................. [OK] "
Else
echo "Start $SERVER 1 .... ..... ..... ..... .............................. [FAILED] "
Fi
Sleep 5
Clear
;;
2)
Echo-e "\n>>>>>>>>>>> $DATE Stop $SERVER 1" >>/log.txt
$SERVER 1_stop 2>>/log.txt
If [$? = = 0];then
echo "Stop $SERVER 1 ..... ..... ..... ..... .............................. [OK] "
Else
echo "Stop $SERVER 1 ..... ..... ..... ..... .............................. [FAILED] "
Fi
Sleep 5
Clear
;;
3)
Echo-e "\n>>>>>>>>>>> $DATE ReStart $SERVER 1" >>/log.txt
/USR/LOCAL/TDOA/PHP/SBIN/PHP-FPM Restart 2>>/log.txt
$SERVER 1_restart 2>>/log.txt
If [$? = = 0];then
echo "Restart $SERVER 1 .... ..... ..... ................................ [OK] "
Else
echo "Restart $SERVER 1 .... ..... ..... ................................ [FAILED] "
Fi
Sleep 5
Clear
;;
0)
Clear
Break
;;
*) echo "----------------------------------"
echo "|            Warning!!! |"
echo "|   Please Enter right choice! |"
echo "----------------------------------"

---restore content ends---

Shell file test, menu presentation idea

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.