Shell Simple Introduction (i)

Source: Internet
Author: User

Reading slow, easy to forget, on the Internet to find examples to do, pondering the examples do not understand, and then continue to Baidu.

The importance of shell scripts under Linux is not much to say. Starting with the first example:

#!/bin/"Hello world! " "

The way of execution reference: http://www.jb51.net/article/53924.htm

To modify the permissions of a file, refer to: http://www.cnblogs.com/hpcpp/articles/6961179.html.

This script is just on the screen output Hello world!

Continue adding content:

#!/bin/sha="Hello world! " num=2"A is: $a num is: ${num}nd"

The A and num here are user-defined variables,

User Variables

In shell programming, the use of user variables requires no prior declaration, and the name of the variable names follows the following rules:

1). The first character must be a letter (a-z,a-z);
2). Cannot have spaces in the middle, you can use the underscore (_);
3). Punctuation cannot be used;
4). You cannot use the keywords in bash (you can view the reserved keywords with the help command).

Note Here the variables can not have spaces around, you may try, is not executed when the error, cannot find this variable.

Accessing variable values

To take the value of a variable, simply precede the variable name with a $.

Is this syntax a bit like PHP syntax?

Let's look at a second example:

#!/bin/bash#name Display programif0  ]then    "name is notprovided. " Else  "Your name is $" fi     

This example looks like a #号开头的表示是注释.

What does that mean? This is the positional parameter, which represents the first parameter of the input, if this executes the file bash 02.sh

The output will be:

Name not provided.

If bash 02.sh Xiaozi

The output:

Your name is Xiaozi

If bash 03.sh xiaozi ni hao
The output is still unchanged and cannot be fully displayed. This is because there are spaces where the space-splitting string is considered a different string. The position of the string is also starting from 0, and the 2nd line of the script is changed to $ A, and the test is performed.
Output:

 is -SH

The location of the Xiaozi is the location of the 1,ni is 2,hao position is 3, and so on.

What should I do if I want to display the following string in full?

#!/bin/bash#name Display programif0  ]then    "name is notprovided. " Else  "Your name is [email protected]" fi     

Just change the penultimate line to [email protected].

Common internal variables

$0 Equivalent main to the C language functionargv[0]
$1, $2 ..... These are called positional parameters (positional Parameter), which are equivalent to the C-language main function argv[1] , ... argv[2]
$# Equivalent to the C language main function argc - 1 , note that the # following does not indicate a comment
[email protected] Represents a list of parameters "$1" "$2" ... , such as those that can be used in for a loop in .
$? Exit Status of the previous command
$$ The current shell's process number

The IF statement determines which branch to execute by using relational operators to determine whether an expression is true or false.

The statement ends with FI, which is to write the IF in reverse, which requires a little attention. There must be a space between expression and square brackets ([]), which is the case for [space expression space], or there will be a syntax error.

1) If ... else statement
if [Expression]then    if  is true fi
2) If ... else ... fi statement
if [Expression]then    if  is true Else  If is true fi    
3) If ... elif ... fi statement
if[Expression1]then Statement (s) to be executedifExpression1  is trueelif [Expression2]then Statement (s) to be executedifExpression2  is trueelif [Expression3]then Statement (s) to be executedifExpression3  is trueElseStatement (s) to be executedifNo expression is truefi

You can also write then and if to a line, in if [expression 1 ];then

Remember to add a semicolon in English before then.

Reference: http://c.biancheng.net/cpp/view/7005.html

Integer operation symbol-GE,-GT,-LE,-LT, don't forget to add-

    -eq           // equals    -ne           // not equal to    -gt           // greater than    -lt           // less than    GE            // greater than or equal to    le            // less than equals

Shell Simple Introduction (i)

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.