Shell Programming Basics

Source: Internet
Author: User

In this paper, the basic syntax of the shell is briefly recorded for future use!

1. Shell variables

When you define a variable name without the dollar sign $, you only need to add the dollar sign before the variable name when using the variable. As

1 name="LW"2echo ${name}     #或者 $name, but it is recommended to add braces

Note: When assigning a value to a variable, the equals sign "=" cannot have spaces on either side

The name of the variable follows the 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 using the help command).

2. Shell string

a shell string can use either single or double quotation marks. However, there are some differences between these two types of quotes in the process of use.

1) Single quote: The string in single quotation marks has two limitations: the variable in single quotation marks is not valid; single quotation marks cannot appear in single quotation marks (not after using the escape character for single quotes). In short, any character in a single quote will be output as it is.

2) Double quotes: double quotes with no single quotation marks, you can output variables, or you can use escape characters.

Such as:

1 course="python"2echo "The course is: ${course}"

3) Other operations of the string

 1  first= "hello"  2  " python " 3   #字符串的拼接  4  newstr= "${first}, ${ last   " 5  Newstr=${first}", "${last   6   #获取字符串的长度  7  len=${#first}  8   #提取子字符串  9  substr=${first:0 : 5 } 

3. Shell Array

Array definitions:array= (value1, value2, Value3, Valuen)

Such as:

1 course= (' python ', ' shell ', 'perl')2 val1=${course[1]} #获取course [ 1] Data in 3echo ${course[@]}        #输出数组中的全部值

4. If,for,while Statement

Numeric comparison operations:

-eq: equals -ne: Not Equal to

-GT: Greater than -ge: greater than or equal to

-lt: Less than -le: Less than or equal to

if: num1=${2*6} num2=${12}

1)if statement

 1  if  Test ${num1}-eq ${num2}  2  then  3  " equal " 4  elif  Test ${num1}-gt ${num2}  Span style= "color: #008080;" >5  echo   "num1 bigger than num2"  Span style= "color: #008080;" >6  7  echo  num1 less   than num2 " 8  fi  

2) for statement

1 # for in 'seq'            #输出1 ~2for in  3456            4563  Do 4 Echo $i 5  Done   

3) While statement

1  while  : 2  Do 3 Echo "Hello" 4  Done 5 4 ) until statement 6 until condition 7  Do 8 Command 9  Done   

5. Shell function

such as the addition function:

1 Add () {2     Echo '$:'$1', $:'$23Return $ (($1+$2))4 }5Add3 46 Echo "The sum is: $?"

A few special character meanings:

$#: Number of arguments passed to script

$*: Displays all parameters passed to the script in a single string

$$: The current process ID number for the script to run

$!: ID number of the last process running in the background

[Email protected]: Same as $# , but use quotation marks and return each parameter in quotation marks.

$-: Displays the current options used by the Shell , same as the set command function.

$?: Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.

Reference Links:

Http://www.w3cschool.cc/linux/linux-shell-func.html

Shell Programming 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.