Linux Shell Programming Basics Notes

Source: Internet
Author: User
Tags bit set echo display function definition logical operators

1.shell variables
Local variables:
A=1 (no spaces on both sides of the equal sign)
Read-only variables:
ReadOnly variable name = variable value (cannot be changed during use)
Declaring variables:
Linux assigns a value to a variable by default as a character or string, and if a numeric operation is required, you need to declare it beforehand
DECLARE parameter variable name [= Variable value]
Declare parameters:-A: Array-i: INT-r: Read-only
To set an integer variable, in addition to the Declare-i declaration, you can:
a=$ (($a + 1)) or a=$[a+1]
To clear a variable:
unset variable name (read-only variable cannot be cleared)

To access the shell variable:
Set shows all locally defined shell variables
${a} takes the value of variable a
echo Display Variable
Echo's Parameters:
All parameters are printed and converted to the next line
-N does not convert to the next line after printing


To assign a value to a variable from the keyboard read-in:
Read [parameter] variable name
Read variable name variable name variable names ...
If you do not follow the variable name, the input is assigned to $reply
Read parameter:
-a means that the input is stored in the array variable name
-P Displays the prompt string after-p before the value is assigned, such as Read-p "?" a displays:? input


Environment variables:
Export environment variables (available for external invocation):
Export variable Name

Positional parameter variables:
The command-line arguments that are used to save the script when the script is invoked
Echo ${1},${2},...
Position parameter shift:
Shift [move forward], default forward 1
Such as:
Shift 2 Moves the two-bit to the left, usually in the loop structure


Special variables:
$ A: Holds the currently executing script file name, including the path
$#: The number of positional parameters that are passed to the script
$*: Holds all parameters passed to the script
[Email protected]: Same as $*, but each parameter is enclosed in quotation marks
$$: Holds the current process number
$!: The process number that holds the last process running in the background
$?: The exit status of the last executed script or command is stored 0 indicates success, not 0 indicates an error

Reference:
Double quotes: Weak references, to $, ', \ Invalid
Single quotes: Strong references, any special characters are masked
Backslash: Escaped

Variable substitution:
${varname:-word} If varname is undefined or empty use Word instead of varname as the return value
${varname:=word} If varname is undefined or empty, Word is assigned to varname and returned
${varname:?message} output message as error message if varname undefined or empty
${varname:+word} use Word as the return value if VarName is defined and is not empty
${varname:offset} returns the string starting at the beginning of the offset character in $varname to the end, taking the tail string
${varname:offset:length} starts the offset character in $varname, and a string of length is returned, taking a substring


2. Process Control
Sequential structure:
Nothing to say

Condition test:
Test command
Check file status, determine if string matches, numeric test, etc.
Can be used in the branch structure, but also in the loop structure
Test condition
Or
[Test conditions] (a space is required between the test condition and the left and right square brackets)

File test
Test [condition] FILE

-E If file exists
-S if file is not empty
-D If file is a directory
-F If file is a normal document
-B If file is a block device files
-C If file is a character device files
-S if file is a socket
-L If file is a symbolic connection
-P If file is a well-known pipe

-F If file has Read permission
-W If file has Write permission
-X if file has Execute permission
-G If file is owned by a valid group
-O If file is owned by a valid user
-K if file has the sticky bit set
-U if file has the Set-user-id bit set
-G If file has the Set-group-id bit set
-N If file has been modified since it was last accessed

File1-nt FILE2 if FILE1 than FILE2 new
File1-ot FILE2 If FILE1 is older than FILE2
File1-ef FILE2 If FILE1 is a hard connection file for FILE2, they point to the same I node


String test
string1 = string2 strings are equal
String1! = string2 strings are not equal
-N string1 string non-null
-Z string1 string is empty

Numerical test
Expr1-eq EXPR2 expression values are equal
Expr1-ne EXPR2 expression values are not equal
EXPR1-GT EXPR2 expression 1 greater than expression 2
EXPR1-LT EXPR2 expression 1 is less than expression 2
Expr1-ge EXPR2 expression 1 is greater than or equal to an expression 2
Expr1-le EXPR2 expression 1 is less than or equal to an expression 2

logical operators
With-a [$a = "a"-a $b = "B"] if ($a = = "a" && $b = = "B")
Or-o [$a = "a"-O $b = "B"] if ($a = = "a" | | $b = = "B")
Non -! [! a= "A"] if ($a! = "a")


Branching structure: (if branch, Case branch)
If branch structure:
If condition test 1;then
Conditional Test 1 is true when the statement
[elif condition Test 2;then]
Conditional Test 2 is true when the statement
...
[Else]
Sequence of statements when a condition test is false
Fi
#如果条件测试语句与then在一行, then use; separate from then
#条件测试语句与if, there must be at least one space separated between elif,else,then
#elif, else optional, else can only appear once
#if可以嵌套

Case Branching structure:
Case value in
Mode 1)
Statement Group 1
...
;;
Mode 2)
Statement Group 2
...
;;
......
*)
Statement Group N
Esac


Loop structure
For loop:
For variable in value 1 value 2 ... Value n
Do
Statement 1
Statement 2
...
Done

A format similar to the C language
For ((i=1;i<=6;i++))
Do
...
Done


While loop:
While condition
Do
Statement 1
Statement 2
...
Done


Select loop:
Select name in [in list]
Do
Statement list
Done


3.shell function
Function definition:
function funcname
{
 shell commands
}
or
FuncName ()
{
 shell commands
}

Parameter:
Similar to the parameters of the script, using the $1,$2 ...
such as

#!/bin/bash
My_func ()
{
 a=$1
 echo $a
}

Read input
My_func () $input The return of the

function can be used:
return

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.