Basic shell programming conditions, iterative process control, and arithmetic operations

Source: Internet
Author: User

Basic shell programming

1. Start Script Programming

<1> #! The/bin/sh line is the shebang line, which tells the system that the following command is executed by shell.

#!/bin/shPERSON="Yuesichiu"echo $PERSONecho "what is your name?"read PERSONecho "Hello,$PERSON"

Generally, the variable name must be in uppercase. "=" no blank characters are allowed on both sides!

<2> special Variables

$ ----- Indicates the ID of the Current Shell Process

? ---- Output status of the previous command

----- Options called when the current shell is started

! ---- PID of the previous command running in the background

0 ---- File Name of the current script

1-9 ---- 1st to 9th command line parameters provided when the current script is called: $1 is the value of 1st command line parameters, and so on

 

Echo $?

2. Process Control

<1> conditional Process Control

If Conditon

Then

Something happenings

Fi

#!/bin/shecho "Guess the secret color:"read COLORif [ $COLOR="pink" ]thenecho "You are correct."#elif [ $COLOR="red" ] #  echo "you are close."else   echo "You are stupid!"fi

The test command is used to calculate the value of a condition.

The test can contain the following parameters:

-D: the specified file exists and is a directory.

-E: the specified file exists.

-F: the specified file exists and is a common file.

-G all file group ids match with file IDS

-The first file specified by NT is newer than the last file (file1-nt file2)

-The previous file specified by ot is older than the previous one (file1-nt file2)

-Rxw: the user who executes the command has the read and write permissions.

-S file exists and is not empty

Case statement format

#! /Bin/sh

# Caseselect
Echo = N "enter a number from 1 to 5 :"
Read ans
Case $ ans in
1) echo "select 1"
;;
2) echo "select 2"
;;
3) echo "select 3"
;;
4) echo "select 4"
;;
*) Echo "'basename $ 0': This is not between 1 and 4"> & 2
Exit 1
;;
Esac <2> Iterative Process Control

 

Supplement: shell arithmetic operations

BASH Shell has four arithmetic operations:
1: Use expr external program

Add r = 'expr 4 + 5' // reverse quotation marks
Echo $ R
Note! There must be a blank space between '4' and ''5 '.
R = 'expr 4 * 5' # error
Multiplication r = 'expr 4 \ * 5'

2: use $ (())
R = $(4 + 5 ))
Echo $ R

3: use $ []

R = $[4 + 5]
Echo $ R

Multiplication
R = 'expr 4 \ * 5'
R = $(4*5 ))
R = $ [4*5]
Echo $ R

Division
R = 'expr 40/5'
R = $(40/5 ))
R = $ [40/5]
Echo $ R

Subtraction
R = 'expr 40-5'
R = $(40-5 ))
R = $ [40-5]
Echo $ R

Returns the remainder.
R = $ [100% 43]
Echo $ R

Power (such as power 3 of 2)
R = $(2 ** 3 ))
R = $[2 ** 3]
Echo $ R
Note: expr has no power.

4: Use the let Command
Addition:
N = 10
Let n = n + 1
Echo $ N # n = 11

Multiplication:
Let M = N * 10
Echo $ m

Division:
Let R = m/10
Echo $ R

Calculate the remainder:
Let R = m % 7
Echo $ R

Multiplication limit:
Let R = m ** 2
Echo $ R

Although bash shell has four arithmetic operations, not each of them is cross-platform. We recommend that you use expr.
In addition, we often haveAdd 1 Operation, The following four methods can be used:
M = $ [M + 1]
M = 'expr $ m + 1'
M = $ ($ m + 1 ))
Let M = m + 1

Shell programming is prone to errors:

-Bash:./configure:/bin/sh ^ m: Bad Interpreter: no such file or directory

This is because the file is in DOC format. In VI Mode: Set ff, you can view the format: Set FF = UNIX to convert the DOC format to Unix format.

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.