Bird Book Shell Learning (iii) Shell script Program Design Essentials Summary

Source: Internet
Author: User




A matter of note



1. The beginning of a script behavior:


#!/bin/bash



declare the name of the shell that this script requires

2. "#!" is out of the program Other than the one that starts with # is the comment content

3. For the use of the system command, you can set the primary environment variables such as path at the beginning of the program

4. How to run the script:

SH example.sh or similar bash example.sh

chmod +x example.sh;./example.sh ################################ both are performed in a new child process

SOURCE example.sh or. The example.sh is the direct execution of the command in the parent process.

Two simple scripts2.1 Interactive scripts to handle simple input and output

The

first is to read the user input Read command, the output command is echo , for example:

read-p ' Please input your first name: ' FirstName

Echo ' Your first name is: $firstname '

The variables in the shell are not declared to be more arbitrary than the specified type, at the time of the reference, the $ variable name or the ${variable name}

In

addition to this way of passing through the arguments when executing the script, the parameter names in the shell are built

-in $0,$1,$2,$4 ...

here, $ $ denotes the file name, starting with the first argument, and so on. There are other special parameter names outside the first, as follows:

$#: Indicates the number of parameters

[Email protected]: On behalf of "$", "$" ... The meaning

$*: On behalf of "$ $ ..." The meaning

eg

#! / bin / bash

echo 'filename:' $ 0

echo 'args size:' $ #

echo 'first arg:' $ 1

2.2 Numerical operation shell can only support the basic operation of integers: +-* /%

1) .declare-i var can declare the variable as an integer, and then operate on the variable, the operation of the variable is like this:

declare -i v1

declare -i v2

v3 = $ (($ v1 + $ v2))

# Can also be calculated like this:

declare -i v3 = $ v1 * $ v2 #There must be no spaces between expressions

2) .var = $ ((operation content)) // There is space allowed in the operation content eg:

echo $ ((19 + 78))

2.3 Judgment in the shell

You can use the test command to judge in the shell, eg:


test -e ./example.sh #test whether the file exists

Common test types and parameters are as follows: Linux private dishes from Bird Brother


In addition to using the test command to judge the condition, i can also use [] (the syntax of the brackets to judge, the test parameters are the same as test)

[] Syntax requires that there is a space between all the quantities in []


[b "$ name" b == b "xiaoyi" b] #b both represent spaces





Three complex scripts 3.1 The standard format of conditional statements if else:

if [condition judgment 1]; then

  command1

elif [condition judgment 2]; then

  command2

else

  command3

fi

Among them, the conditions can be connected with && or ||

The standard format of case ... esac is as follows:

case $ variableName in

"First Variable Content")

   Program segment

   ;; #The end of each type is similar to break

"Second Variable Content")

   Program segment

   ;;

*) # Similar to default, replace with *

   Other blocks that do not meet the conditions

   ;;

esac

3.2 Loop statement Several formats of loop are as follows:

while do done

while [condition]

do

  Program segment

done

until do done

until [condition]

do

 Program segment

done

for ... do ... done

for var in cond1 cond2 cond3 ...

do

 Program segment

done


for ((initial value; limit value; execution step))

do

 Program segment

done

3.3 Functions

function fname () {

   Program segment

}


examples:

#! / bin / bash

# if --- else-exmaple

declare -i v1 = 10

declare -i v2 = 15


if [$ v1 -ge $ v2]; then

echo '' $ v1 'is bigger than' $ v2 ''

else

echo '' $ v1 'is smaller than' $ v2 ''

fi


# case ... esac example

read -p 'Please input your name:' name

case $ name in

"xiaoyi")

echo 'hello xiaoyi'

;;

"xiaoyi1")

echo 'hello xiaoyi1'

;;

*)

echo 'no case matched!'

;;

esac


# loop example

declare lv = 1

while [$ lv -le 10]

do

echo $ lv

lv = $ (($ {lv} +1))

done


until [$ lv -le 1]

do

echo $ lv

lv = $ (($ {lv} -1))

done


for c in 'c1' 'c2' 'c3'

do

echo $ c

done


for ((i = 0; i <10; i = i + 1))

do

echo $ i

done


function printHello () {

echo 'hello'

}


printHello













Bird book shell learning (3) summary of main points of shell script programming



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.