The symbol and syntax of Shell basics

Source: Internet
Author: User

shell scripts have now become a very common scripting language, so widely used, there is no doubt that it has its unique. What is the difference between a shell scripting language and other languages, such as C + +? C + + and other languages are compiled languages, which means that the program is compiled to generate executable binaries before each execution, whereas the shell is an interpreted language, and it must rely on the interpreter to interpret the commands row by line, and now our familiar and commonly used interpreter is the BASH interpreter. So when learning to use an interpreted language, it is essential to download an interpreter first. Let's look at a simple shell script:

#! /bin/bashint_val=312char_val= ' A ' float_val=3.12string_val= "aaaaaa" Echo $int _valecho $char _valecho $float _valecho $ String_valecho $int _val ' + ' $char _val ' + ' $string _val ' + ' $float _val ' echo  ${char_val}a$int_val
The results of the operation are as follows:


It can be seen from the results of the run: The interpreter we rely on IS bash, and the shell script defines the use of variables and joins a number of different variables much more easily than we do with C + + implementations!

The shell script specifies that the variables need not be defined before they are used, that the variables to be used are user-defined, that all variables under the shell script are string types, so that we can connect the strings according to our own requirements, as shown in the code above, and only need to use ' + ' in the connection process. Number or the required characters.

The following is specific from the shell's symbol and shell syntax to learn memory.

A. Shell symbol

1. After you have written a shell specimen, how can you not execute the script to your own desired result?

A: There are several ways: (1) Add executable permissions to the script: chmod +x + file name, and then execute the./+ file name

(2)/bin/bah + file name

(3) source./+ File name

(4). ./+ file Name

What are the variables in 2.shell?

A: By type can be divided into local variables and environment variables, you can use Env to view all the environment variables in the current system. While local variables exist only in the current shell process, we can use the SET command to view all variables (including local variables and environment variables) in the current shell process.

Here with an example:

(1) define a variable in the current shell: value=100 (note that there can be no spaces on either side of the equal sign, or it will be interpreted by the shell as command and command line arguments)

(2) Export this variable to an environment variable using export

(3) Use ENV to view the command

As shown


(4) Use unset to delete defined environment variables or local variables

3.shell How do I reference a variable?

A: As you can see in the first example, defining a variable does not require a type, just use the $+ variable name when you take the value of the variable.

4. How do we get all the qualifying conditions in this catalogue?

A: Here, we refer to wildcards, common wildcard characters are: * 、?、 [], respectively, explained as follows:

(1) *: matches 0 or more arbitrary characters

(2)? : Matches an arbitrary character

(3) []: matches one occurrence of any character in square brackets

Examples are as follows:


5. How do I use command substitution?

A: The command substitution is that the shell executes the commands within the symbol first, and then immediately substitutions the output to the current command line.

Here's an example:

#! /bin/bashdate= ' Date ' echo $DATE

#! /bin/bashdate=$ (date) echo $DATE

the results of the above two procedures are as follows:

6. How is the escape character used when creating a file?

A: The general file name is defined with the user, but when you want to touch a file named $ $, the transfer character comes in handy.


Single and double quotes in a 7.shell script have something in common?

A: In a shell script, both single and double quotes simply hold the literal meaning of the character, meaning that the contents of the character are not interpreted.

Two. Shell basic syntax

1. Condition Testing

In the C language, conditional judgments are used in the IF statement, whereas in shell scripts, the two commonly used conditions test the command when testing and [. The test result is true returns 0, which returns 1 for false (as opposed to the return condition of the language). Here are some examples of tests:

(1)

#! /bin/bashread valtest $val-gt 10echo $? [$val-gt]echo $?
The results of the operation are as follows:

Of course, in addition to using the command test, the command can also be added to determine the parameters:

(2)

<span style= "FONT-SIZE:18PX;" ><strong>#! /bin/bashread input[-D $input]echo $? [-F $input]echo $? [-Z $input]echo $? [-N $input]echo $?</strong></span>
The results of the operation are as follows:


(3) The judgment of the string

<span style= "FONT-SIZE:18PX;" ><strong>#! /bin/bahstr1= "I am Boy" str2= "T am GILR" ["x$str1" = = "X$str2"]</strong></span>
<span style= "FONT-SIZE:18PX;" ><strong>echo $?</strong></span>
general strings are used to add X to the string to prevent an empty string from appearing when compared

Of course, test commands can also be performed with or non-logical operations, the method of comparing strings above is one of the examples

The syntax used is: [Test condition-a test condition]

[! Test conditions]

[Test Condition-o test condition]

#! /bin/bashstr1= "I am a beautiful girl" [-F test.sh-a "x$str1" = = "I am a beautiful girl"]echo $?
The run result is 1.
2.if/then/elif/fi This is equivalent to the IF statement in our C language, as in the following example:

#! /bin/bashread sexif  ["xsex" = = "Xman"];thenecho "You are a Boyelif [" xsex "= =" Xwoman "]echo" You are a Girl "ELSEEC Ho "Say Nothing" fi

But here, it is important to note that there can be no empty statements between the IF statement and Fi, which is separated by: always true. Examples are as follows:

#! /bin/bashread Inputif [-D  $input];thenecho ' $input is dir ' fiif:; then echo '/is always true ' fi
In addition to this, the shell script also provides && | | command, which serves the equivalent of if...then/if ... not then (example below)

3.case/esac it is equivalent to the switch statement in the C language, as in the following example:

#! /bin/bashread valcase $val in a)  service iptables start echo "Start"; b) Service iptables Stopecho "Stop";; c) Service iptables Restartecho "Restart";; Esac
In the code above;; The symbol is similar to break in a switch statement.
4. The loop statement, the Loop statement under the shell script is very different from the C-language loop statement, as in the following example:

#! /bin/bashfor i in [email protected]do echo ' $i ' done


#! /bin/bashfor ((i=0; i<100; i++)) Doecho "$i" done

Of course the second method is more similar to the C language!

5.while/do/done, examples are as follows:

#! /bin/bashecho "printf 1+1=?" Read Valwhile [  $val-eq 2]do echo "You is a smart man" done
6. Positional parameters and special variables

(1) $ A: Indicates the file name of the executable on the command line

(2) $, $ ...: Indicates parameter 1 on the command line, parameter 2, and so on

(3) [email protected]: Indicates the parameter list

(4) $#: Indicates the number of command line arguments

(5) $?: Indicates the exit code for the current process

(6) $ (): Indicates command substitution

(7) $$: Indicates the shell's process number

7. Functions: In shell scripts, functions are usually defined first, not written when defined

Parameters and return values, just write the function name when calling. The general procedure is executed from the place where the function is called.









Symbol and syntax for Shell Basics

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.