Shell Learning Notes (2)

Source: Internet
Author: User
Tags install php nfsd


I. Standard input commands read and Practice 1, read basic usage
[root@master4 day3]# vim read.sh

read -p "pls input two number:" var1 var2
echo "the nums you input is: $var1 $var2"
2. Small Script Example
[root@master4 day3]# vim read.sh

read -p "pls input two number:" var1 var2
echo "the nums you input is: $var1 $var2"
3. Set timeout period
[root@master4 day3]# vim read.sh

read -t 5 -p "pls input two number:" var1 var2
echo "the nums you input is: $var1 $var2"

[root@master4 day3]# sh read.sh 
pls input two number:the nums you input is:
4, Echo-n cooperation
[[email protected] day3]# vim echo.shecho -n "please input a num:" read numberecho $number[[email protected] day3]# sh echo.sh please input a num:4545
5, subtraction, read mode

[root@master4 day3]# vim read01.sh

#!/bin/bash
read -p "input input tow num caculate:" num1 num2
echo "$num1-$num2 = $(( $num1 - $num2))"
echo "$num1+$num2 = $(( $num1 + $num2))"
echo "$num1*$num2 = $(( $num1 * $num2))"
echo "$num1/$num2 = $(( $num1 / $num2))"
echo "$num1**$num2 = $(( $num1 ** $num2))"
echo "$num1%$num2 = $(( $num1 % $num2))"

input input tow num caculate:5 2
5-2 = 3
5+2 = 7
5*2 = 10
5/2 = 2
5**2 = 25
5%2 = 1
[root@master4 day3]# 
6, conditional judgment, can only pass two parameters
#!/bin/bash
if [ $# -ne 2 ];then
    echo "USAGE:$0 NUM1 NUM2"
    exit 1
fi
#read -p "input input tow num caculate:" num1 num2
num1=$1
num2=$2
echo "$num1-$num2 = $(( $num1 - $num2))"
echo "$num1+$num2 = $(( $num1 + $num2))"
echo "$num1*$num2 = $(( $num1 * $num2))"
echo "$num1/$num2 = $(( $num1 / $num2))"
echo "$num1**$num2 = $(( $num1 ** $num2))"
echo "$num1%$num2 = $(( $num1 % $num2))"

Ii. condition Test 1, Syntax format

1.1 format 1 test< test expression >


Determine if the file exists:
[root@master4 day3]# test -f file && echo true || echo false
False
[root@master4 day3]#

[root@master4 day3]# touch file
[root@master4 day3]# test -f file && echo true || echo false
True

Does not exist, so no error:
[root@master4 day3]# rm -rf file
[root@master4 day3]# test -f file && cat file
[root@master4 day3]#
The file exists:
[root@master4 day3]# echo 1 > file
[root@master4 day3]# test -f file && cat file
1
[root@master4 day3]#
1.2 Test! (non) Usage

[root@master4 day3]# rm -rf file 
[root@master4 day3]# test ! -f file && cat file
cat: file: No such file or directory
[root@master4 day3]# 

[root@master4 day3]# echo 1 > file
[root@master4 day3]# test ! -f file && cat file
[root@master4 day3]# 
1.3 View Test Help
[root@master4 day3]# help test

2, Format 2 [< test expression >]

2.1 Simple example


[root@master4 day3]# rm -rf file 
[root@master4 day3]# [ -f file ] && echo 1 || echo 0
0
[root@master4 day3]# 

存在:
[root@master4 day3]# touch file
[root@master4 day3]# [ -f file ] && echo 1 || echo 0
1
[root@master4 day3]# 

非:
[root@master4 day3]# [ ! -f file ]&&echo 1 ||echo 0
0
[root@master4 day3]# 
3. Format 3:[[< test expression >]

[root@master4 day3]# [[ ! -f file ]]&&echo 1 ||echo 0
0
[root@master4 day3]# 
4. &&

[root@master4 day3]# [[ -f file && -d folder ]] && echo 1 || echo 0
0
[root@master4 day3]# mkdir folder
[root@master4 day3]# [[ -f file && -d folder ]] && echo 1 || echo 0
1
[root@master4 day3]#

Format 2 error:
[root@master4 day3]# [ -f file && -d folder ] && echo 1 || echo 0
-bash: [: missing `] ‘
0
[root@master4 day3]#
Need to use this:

[root@master4 day3]# [ -f file -a -d folder ] && echo 1 || echo 0
1
[root@master4 day3]#

or
[root@master4 day3]# [ -f file ] && [ -d folder ] && echo 1 || echo 0
1
[root@master4 day3]#

3. File test operator 

3.1 memory method


4, String test operators 4.1

5, two yuan comparison

 5.1 two yuan comparison of special conditions example

5.2 Two-dollar string comparison

6, the logical operator 6.1 example three, example 1, the condition Test 1 is true, 0 means false
Condition and if statement comparison
[root@master4 day3]# [ -f "$file1" ] && echo 1 || echo 0
0
[root@master4 day3]# if [ -f "$file1" ];then echo 1;else echo 0;fi
0

If [ -f "$file1" ]; then
     Echo 1
Else
     Echo 0
Fi

prompt:
1. The functions of the above two statements are equivalent.
2, the variable $file added double quotes, this is a good habit of programming, can prevent many unexpected errors.

2. Document Test Example
[root@master4 day3]# file1=/etc/services
[root@master4 day3]# file2=/etc/rc.local

True if it exists and is a file
[root@master4 day3]# [ -f "$file1" ] && echo 1 || echo 0
1

True if it exists and is a directory
[root@master4 day3]# [ -d "$file1" ] && echo 1 || echo 0
0

True if the file exists and is not empty
[root@master4 day3]# [ -s "$file1" ] && echo 1 || echo 0
1

True if it exists
[root@master4 day3]# [ -e "$file1" ] && echo 1 || echo 0
1

If you remove the quotes, the test is normal:
[root@master4 day3]# [ -f $file1 ] && echo 1 || echo 0
1
[root@master4 day3]# [ -f $file2 ] && echo 1 || echo 0
1

[root@master4 day3]# [ -f /etc/services ] && echo 1 || echo 0
1
[root@master4 day3]# [ -f /etc/rc.locl ] && echo 1 || echo 0
0
[root@master4 day3]#
[root@master4 day3]# [ -f /etc/rc.local ] && echo 1 || echo 0
1

How to use it? A: Look at the system script

If the execution is unsuccessful, execute the following
[ -x /usr/sbin/rpc.nfsd ] || exit 5

If the execution is successful, execute the following
[ -x /usr/sbin/rpc.nfsd ] && exit 5
2.1 Script Examples

3. Multi-condition file Test 3.1 example
[root@master4 day3]# [ -f $file1 -a -f $file2 ] && echo 1 || echo 0
1

[root@master4 day3]# [ -f $file1 -a -f ${file:-null} ] && echo 1 || echo 0
0

The effect of double quotes came out
[root@master4 day3]# [ -f "$file1" -a -f "$file" ] && echo 1 || echo 0
0
[root@master4 day3]# [ -f "$file1" -o -f "$file" ] && echo 1 || echo 0
1
[root@master4 day3]#

[root@master4 day3]# [ -f "$file1" -a -d "$file2" ] && echo 1 || echo 0
0


Summary:
1. "-A" and "-o" logic operators are used in [], syntax [$m-a $n]
2. "&&" and "| |" are used in [[]] to use [$m && $n]. If single brackets are used [] && []
3, note the brackets at both ends, must have a space


After the condition, you need to execute multiple statements, you need to use braces
[root@master4 day3]# vim test.sh

[ 3 -ne 3 ] || {
     Echo "I am oldboy"
     Echo "I am coming"
     Exit 1
}

If using one line, separate them with a semicolon:
[root@master4 day3]# [ 3 -ne 3 ] || { echo "I am oldboy";echo "I am coming";exit 1; }
4, String Test Example 4.1 example
Strings should be quoted [ -n "$file1" ]

[root@master4 day3]# echo $file2
/etc/rc.local
[root@master4 day3]# echo $file1
/etc/services
[root@master4 day3]# echo $file0

[root@master4 day3]# [ -n "$file" ] && echo 1 || echo 0
0

The content is empty and true:
[root@master4 day3]# [ -z "$file" ] && echo 1 || echo 0
1

[root@master4 day3]# [ -z "$file1" ] && echo 1 || echo 0
0
[root@master4 day3]# [ -n "$file1" ] && echo 1 || echo 0
1

summary:
     -z STRING string is empty
     -n STRING string is true if it is not empty
5. Multi-condition String Test 5.1 example
[root@master4 day3]# echo $file

[root@master4 day3]# echo $file1
/etc/services
[root@master4 day3]# echo $file2
/etc/rc.local

[root@master4 day3]# [ -n "$file1" -a -z "$file2" ] && echo 1 || echo 0
0

One is true, output 1
[root@master4 day3]# [[ -n "$file1" || -n "$file2" ]] && echo 1 || echo 0
1

[root@master4 day3]# [[ "$file1" = "$file2" ]] && echo 1 || echo 0
0
[root@master4 day3]#

[root@master4 day3]# [[ "$file1" != "$file2" ]] && echo 1 || echo 0
1

Compare length:
[root@master4 day3]# [[ "${#file1}" = "${#file2}" ]] && echo 1 || echo 0
1
[root@master4 day3]#

[root@master4 day3]# [[ "${#file1}" != "${#file2}" ]] && echo 1 || echo 0
0

summary:
String comparisons must be enclosed in double quotes.
[ -z "$file1" ]

[ "$file1" != "$file2" ]
6. Integer Test 6.1 Example
Do not add double quotes to integer comparisons

[root@master4 day3]# a1=10;a2=13
[root@master4 day3]# echo $a1 $a2
10 13

[root@master4 day3]# [ $a1 -eq $a2 ] && echo 1 || echo 0
0

[root@master4 day3]# [ $a1 -gt $a2 ] && echo 1 || echo 0
0
[root@master4 day3]#
[root@master4 day3]# [ $a1 -ge $a2 ] && echo 1 || echo 0
0
[root@master4 day3]#
[root@master4 day3]# [ $a1 -ne $a2 ] && echo 1 || echo 0
1
[root@master4 day3]# [ $a1 -le $a2 ] && echo 1 || echo 0
1

[root@master4 day3]# [[ $a1 -eq $a2 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ $a1 -gt $a2 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ $a1 -g\lt $a2 ]] && echo 1 || echo 0
[root@master4 day3]# [[ $a1 -lt $a2 ]] && echo 1 || echo 0
1

[root@master4 day3]# [[ $a1 = $a2 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ $a1 != $a2 ]] && echo 1 || echo 0
1
[root@master4 day3]# [[ $a1 > $a2 ]] && echo 1 || echo 0
0
[root@master4 day3]# [[ $a1 < $a2 ]] && echo 1 || echo 0
1

Double parenthesis comparison:
[root@master4 day3]# (( $a1 > $a2 )) && echo 1 || echo 0
0
[root@master4 day3]# (( $a1 < $a2 )) && echo 1 || echo 0
1
7. Test Command 7.1 Testing Example
[root@master4 day3]# test -z "$file1" && echo 1 || echo 0
0

[root@master4 day3]# test -n "$file1" && echo 1 || echo 0
1

[root@master4 day3]# test 3 -ne 3 || echo 0
0

[root@master4 day3]# test "dd" != "ff" || echo 0
[root@master4 day3]#

[root@master4 day3]# test "dd" != "dd" || echo 0
0

Test the test of the file:
[root@master4 day3]# test ! -f /etc/rc.local || echo 1
1

[root@master4 day3]# touch file1
[root@master4 day3]# touch file2
[root@master4 day3]#
[root@master4 day3]# [ file1 -nt file2 ] && echo 1 || echo 0
0
[root@master4 day3]# [ file1 -ot file2 ] && echo 1 || echo 0
1
[root@master4 day3]#

[root@master4 day3]# test file1 -ot file2 && echo 1 || echo 0
1

For the use of the operators of test,[],[[]], help test.
8. Logical operator Test 8.1 example
[root@master4 day3]# [ -f "$file1" -a "$file2" ] && echo 1 || echo 0
1

[root@master4 day3]# [ -f "$file1" -o "$file2" ] && echo 1 || echo 0
1

[root@master4 day3]# [ ! -f "$file1" -o ! "$file2" ] && echo 1 || echo 0
0

[root@master4 day3]# [ -f "$file1" -a -f "$file2" ] && echo 1 || echo 0
1
[root@master4 day3]# [ ! -f "$file1" -a -f "$file2" ] && echo 1 || echo 0
0

[root@master4 day3]# test ! -f "$file1" -a -f "$file2" && echo 1 || echo 0
0

[root@master4 day3]# test ! -f "$file1" && -f "$file23" && echo 1 || echo 0
0

[root@master4 day3]# test -f "$file1" && -o "$file2" && echo 1 || echo 0
-bash: -o: command not found
0
[root@master4 day3]# 
[root@master4 day3]# test -f "$file1" -o -f "$file2" && echo 1 || echo 0
1
Iv. Example 1, read print menu
[root@master4 day3]# cat test01.sh 
cat <<END
    1.DDD
    2.FFF
END
[root@master4 day3]# sh test01.sh 
    1.DDD
    2.FFF
1.1 menu.sh, print a simple menu
[root@master4 day3]# cat menu.sh
Menu(){
Cat <<END
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    Pls input the num you want:
END
}

Menu

Improve:
[root@master4 day3]# cat menu.sh
Menu(){
Cat <<END
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    Pls input the num you want:
END
}
Menu
Read num
Echo "you have selected $num"

test:
[root@master4 day3]# sh menu.sh
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    Pls input the num you want:
2
You have selected 2
[root@master4 day3]#

Continue to improve:
[root@master4 day3]# cat menu.sh
Menu(){
Cat <<END
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    Pls input the num you want:
END
}
Menu
Read num
Echo "you have selected $num"
[ $num -eq 1 ] && {
    Echo "starting install lamp"
    #/bin/sh /server/scripts/lamp.sh
    Exit
}

[ $num -eq 2 ] && {
    Echo "staring install lnmp"
    #/bin/sh /server/scripts/lnmp.sh
    Exit
}

[ $num -eq 3 ] && {
    Echo "this scripts logout."
    Exit
}

[ ! $num -eq 1 -o ! $num -eq 2 -o ! $num -eq 3 ] &&{
    Echo "bye"
    Exit 1
} 
1.2 Multi-level menu
[root@master4 day3]# cat menu1.sh
Menu1(){
Cat <<END
    ********************************
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    Pls input the num you want:
   *********************************
END
}

Menu2(){
Cat <<END
    ==================================
    1.[install apache]
    2.[install php]
    3.[instal mysql]
    4.[back]
    Pls input the num you want:
    ==================================
END
}

Menu1
Read -p "you input the num is:" num
[ $num -eq 1 ] && {
    Menu2
    Read -p "you input the num is:" num2
    [ $num2 -eq 1 ] && {
    Echo "start installing apache."
    Exit
    }
}

test:
[root@master4 day3]# sh menu1.sh
    ********************************
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    Pls input the num you want:
   *********************************
You input the num is:1
    ==================================
    1.[install apache]
    2.[install php]
    3.[instal mysql]
    4.[back]
    Pls input the num you want:
    ==================================
You input the num is:1
Start installing apache. 


Shell Learning Notes (2)


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.