Shell Programming--bash Basics

Source: Internet
Author: User
Tags arithmetic echo command

Create script: Vim test.sh
Script content: #! /bin/bash # "#!" is a contract tag that tells the system which shell to use to execute
echo "Hello world!"
Make script Execute permission: chmod +x./test.sh
Execute script:./test.sh #注意, be sure to write./test.sh, not test.sh. If directly written test.sh,linux will go to path inside to find there is no test.sh.

[Shell comment]: behavior comment beginning with "#", Shell has no multiline comment
[Input/output COMMAND]
echo command: Echo $variablename #从stdout输出变量值variablename
The echo-e "\ n" #-e option is used to activate the escape character, where line breaks are activated \ n
printf command: printf format-string [arguments ...]
format-string:s% d%
Arguments use spaces to split without commas
printf does not wrap like echo, and must display add line breaks (\ n)
When the parameter is more than the format control (%), format-string can be reused and all parameters can be
Read command: Read name #从stdin获取输入并赋值给变量name
[Shell variable]
Definition: variablename= "value" #变量名和等号之间不能有空格
Use: $variablename #使用一个定义过的变量, just precede the variable name with the $ symbol
Redefine: Variablename= "NewValue" #直接覆盖就行
Read-only variable: variablename= "value"
ReadOnly VariableName #用readonly命令可将变量定义为只读变量, non-modifiable
Delete variable: unset variablename #unset命令不能删除只读变量
Variable type: local variable, environment variable, shell variable

[Shell Special variables]
The file name of the current script
$n arguments passed to the script or function. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is $ A.
$# the number of arguments passed to the script or function.
$* all parameters passed to the script or function.
[email protected] All parameters passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below.
$? The exit state of the last command, or the return value of the function, 0 indicates success, and the other value indicates failure.
$$ the current shell process ID. For shell scripts, this is the process ID where the scripts are located.

[Command substitution] ' command ' # ' is an anti-quote, located below ESC, not single quote
[Variable substitution]
${var}: Variable original value
${var:-word}: If the variable var is empty or has been deleted (unset), then return to word, but do not change the value of var.
${var:=word}: If the variable var is empty or has been deleted (unset), return to Word and set the value of Var to word.
${var:?message}: If the variable var is empty or has been deleted (unset), then send message messages to the standard error output, which can be used to detect whether Var can be properly assigned. If this substitution appears in the shell script, the script will stop running.
${var:+word}: If the variable var is defined, then return to word, but do not change the value of var.

[Shell operator]
Native bash does not support simple math operations, but can be implemented by other commands, such as
Awk and expr,expr are most commonly used. such as: val= ' expr 2 + 2 '
Attention:
1. There should be a space between the expression and the operator, for example, the 2 + 2 must be written.
2. The complete expression is to be included, this character is not a single quotation mark, below the ESC key.
[Arithmetic Operation]:+,-,x,/,%,=,==,! =
Attention:
1. Multiplication sign (*) must be added in front of the backslash (\) in order to achieve the multiplication operation;
2. Conditional expressions are placed between square brackets and have spaces, such as if[$a = = $b]
Is wrong and must be written if [$a = = $b].
[Relational Operations]:-eq,-ne,-gt,-lt,-ge,-le
Relational operators only support numbers, and strings are not supported unless the value of the string is a number.
[Boolean operation]: No! , or-O, with-a
[String Operation]:=,! =,-z (length 0 is true),-n (length not 0 is true), str (non-NULL is true)
[File test operation]

[Shell string]
Strings can be in single quotes, double quotes, or without quotes.
Single quotes:
Any character in a single quotation mark is output as is, and the variable in the single-quote string is not valid;
Single quotation marks cannot appear in single quote strings (not after using escape characters for single quotes).
Double quotes:
You can have variables in double quotes, escape characters can appear
Get string length: ${#string}
Extract substring: ${string:1:4} #输出下标为1到4的字串, including 1 and 4, Word Poute starting from 0
Find sub-string subscript: Echo ' expr index ' $string ' sub_string '

[Shell Array]
Bash supports one-dimensional arrays (which do not support multidimensional arrays) and does not limit the size of arrays. The subscript of an array element is numbered starting with 0, getting the elements in the array to take advantage of subscript, the subscript can be an integer or an arithmetic expression, and its value should be greater than or equal to 0.
To define an array:
In the shell, the array is represented by parentheses, and the elements of the array are separated by a "space" symbol. The general form of the definition array is: array_name= (value1 ... valuen)
Read array element: ${array_name[index]}
Use @ or x to read all elements of an array: ${array_name[*]} or ${array_name[@]}
Gets the length of the array:
# Gets the number of array elements
length=${#array_name [@]} or length=${#array_name [*]}
# Gets the length of an array of individual elements
lengthn=${#array_name [n]}

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