Bash programming basics and for loops

Source: Internet
Author: User

Bash Programming Essentials

variables :

Local variable: Valid only for the current shell process, not valid for other shells other than the current shell (including its parent, child, etc.);

Environment variables: Valid for Shell processes and their child shell processes;

Local variables: Valid only within a certain piece of code space in the current shell process, usually for function local programs;

Positional variables: $, $, ...

$ is the first parameter passed to the shell script

$ is the second parameter passed to the shell script ...

Special variables:

$# is the number of arguments passed to the script

$ is the name of the script itself

[email protected] is a list of all parameters passed to the script

$* is to display all the parameters passed to the script in a single string, with more than 9 parameters, unlike positional variables

$$ is the current process ID number for the script to run

$? is to display the exit status of the last command, 0 indicates successful execution, and other means execution failed .

A reference to a variable

${ variable name}:{} can be omitted, abbreviated to $ variable name

the role of quotation marks in variable references

"" : double quotes, weak references, display the value represented by the variable;

"' : Single quotation marks, strong references, display the contents of quotation marks, do not display the value of the variable;

a=1, echo ' $a ' The result is $ A

        `` : Anti-quote, command Reference, command substitution, will quote the execution result of the command in quotation marks; effect same as $ (command)

a= ' Echo1 ', echo $a; The result is 1, which assigns the result of Echo 1 to a, which is a=1

declaring variables

Declaration of the Shaping variable:

Let Var_name=value

Declare-i Var_name=value

Declaration of environment variables:

Exportvar_name=value

Declare-x Var_name=value

Format of the script:

The first line illustrates the interpreter, that is, which interpreter is used to interpret the script, such as #!/bin/bash, which uses bash to explain the script's run, which must be written shelf the first line.

Other lines that start with the # number are interpreted, and the interpreter automatically skips over and does not perform

Running the script

Use the chmod +x script name to give script execution permission to indicate that the path runs

The direct pass script runs to the Bash interpreter, where

- N : A syntax error in the test script

- x : Displays each step of the script execution to the terminal so that the script can be debugged

Arithmetic operations in a script

$[ arithmetic expressions]

Let script name = arithmetic expression

$ ( arithmetic expressions)

$ (exprargu1 argu2 ARGU3)

Process Control for scripts

for loop :

Format:

for variable in list;d o

Loop body

Done

The variable takes a value in the list and takes the value into the loop body, and the value in the list is exhausted, then the loop executes, and the number of cycles is the number of values in the list.

List: A collection of strings that contain at least one element

(1) give directly the

(2) Value list: {start. End}, for example: {1..10}

seq Last : from 1 to last, each 1 takes a value, {SEQ 3} indicates { ?

seq First Last: from first to last, take one value at every 1

seq First STEP last : From first to last, each step takes a value; {SEQ 2 2 10} = {2,4,6,8,10}

(3) available is a list of command execution results, using anti-quotes.

For loop instance

Calculates all the even numbers within 1-100 and

#!/bin/bashdeclare-i sum=0for i in ' seq 2 2 ';d o sum=$[$sum + $i]doneecho $sum

for special circumstances of the cycle

(1) for omitted, the script parameter list is automatically obtained;

(2) C Programming Style:         

for (variable assignment; cyclic condition; modified expression); Do CMD1 CMD2 done

     (3) Loop nesting: The outer loop executes once, the inner loop executes before the outer loop is executed, the outer loop executes once, and the inner loop may execute multiple times.

 fori in [list1]; do                 CMD1                 forj in [LIST2]; do                      cmd2                     ...                 done            done 

for Loop Advanced Instance

print 99 multiplication table

#!/bin/bashfor i in {1..9};d o to J in ' seq $i ';d o num=$[$i * $j] echo-e-n "$[j]x$[i]= $num \ T" Done Echodone

650) this.width=650; "src=" Http://bashbiancheng.qiniudn.com/1.png "width=" 736 "height=" 204 "alt=" 1.png "/>


This article is from the "Linux Learning path" blog, so be sure to keep this source http://linuxu.blog.51cto.com/9471357/1587178

Bash programming basics and for loops

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.