Basic linux knowledge about the concept, basic linux knowledge

Source: Internet
Author: User

Basic linux knowledge about the concept, basic linux knowledge
1. Basic Knowledge judgment

To check the properties of a file or file d, you can use the test command.

[root @loacalhost ~]#test -e /vitest

Use the test command to check whether the directory vitest exists.

However, no results are displayed. Therefore, the "parameter" output format is improved.

[root @loacalhost ~]#test -e /vitest && echo "The dir exist" || echo "Not exist"

Mark judgment table

In addition to test, you can also use the [] Judgment symbol for judgment.

[root @loacalhost ~]#[ -z "$HOME" ];echo %

Note: Because brackets [] are used in many places and common wildcards and regular expressions are used, when [] is used as shell judgment in bash Syntax, the two ends of the brackets [] must be separated by spaces.

1. Each component in the brackets [] must be separated by a space key.

2. the variables in the brackets [] should be enclosed in double quotation marks.

3. constants in brackets []. It is best to enclose them in double quotation marks or single quotation marks.

[Root @ loacalhost ~] # Name = "rhx" # When assigning values to variables = spaces are not allowed between the left and right sides. [root @ loacalhost ~] # [$ Name = "HOME"] # the left and right sides of the = value must have spaces.
2. Conditional attention

The conditional action is to execute an action when a condition is met. If the condition is not met, other actions are executed.

A. Single Layer if... then judgment
If [condition criterion]; then # note that there is a space between the left and right sides of the condition criterion action # When the condition is set, perform the operation fi in the action # end the if judgment, equivalent to turning the Left and Right of if

Example:

#!/bin/bash#program:#       This program shows "Hello World!" in your screen#History:#205/08/3 rhx First ReleasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N): " ynif [ "$yn"  == "Y" ] || [ "$yn"  == "y" ];then    echo "Ok ,continue"exit 0fiif [ "$yn" == "N" ] || [ "$yn" == "n" ];then    echo "Oh interrupt"exit 0fiecho "I do not know what your choice is "&& exit 0
B. Multiple complex judgments if elif else
If [condition criterion 1]; then # note that condition 1 criterion has spaces on both sides of action1 # When the condition is set, perform the operation elif in action1 [condition Criterion 2]; then action2 # When condition 2 is set, execute Operation fi IN action2 # end if judgment, equivalent to turning left and right if
#!/bin/bash#program:#       This program shows "Hello World!" in your screen#History:#205/08/3 rhx First ReleasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N): " ynif [ "$yn"  == "Y" ] || [ "$yn"  == "y" ];then    echo "Ok ,continue"elif [ "$yn" == "N" ] || [ "$yn" == "n" ];then    echo "Oh interrupt"fi

In shell scripts, scritpt has some special settings for parameters. The parameter ing relationship is as follows. $0 indicates the file name.

/tmp/scrpts      opt1      op2      opt3        opt4     $0           $1       $2        $3          $4
#!/bin/bash#program:#       This program shows "Hello World!" in your screen#History:#205/08/3 rhx First ReleasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATH#read -p "Please input (Y/N): "ynif [ "$1" == "hello" ];then    echo "Hello,how are you "elif [ "$1" == "" ];then    echo "You MUST input parameters,ex> {$0 someword}"else    echo "The only parameters is 'hello',ex> {$0 hello}"fi

C. case... esac
Case $ variable in # keyword case, with the symbol $ "value1" before the variable) # we recommend that you use double brackets to enclose the content of each variable. The keyword is enclosed in parentheses ), note that this is different from the common c/c ++ language; # end of each type with two consecutive semicolons "value2 ";;*) # The last variable content will use * to represent other values, such as the default option in c/c ++; esac # End of case syntax

Example:

#!/bin/bash#program:#       This program shows "Hello World!" in your screen#History:#205/08/3 rhx First ReleasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHfunction printit(){    echo -n "Your choice is "}echo "This program will print your selection!"case $1 in    "one")        printit;echo $1 | tr 'a-z' 'A-Z'        ;;     "two")        printit;echo $1 | tr 'a-z' 'A-Z' "three")        printit;echo $1 | tr 'a-z' 'A-Z';;     *)    echo "Usage $0 {one|two|three}" ;;esac

D. Use the function
Function funcname () {program segment}

Note that $0, $1, and $2 in function are different from those in script.

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.