Shell Programming Learning Notes (2)

xiaoxiao2021-03-06  91

Shell Programming Study Notes (2) Author: Badcoffee

Email: blog.oliver@gmail.com

November 2004

Original article: http://blog.9cbs.net/yayong

Copyright: Please be sure to indicate the original source, author information and this statement by hyperlink.

When implementing a command or tool with the shell, it is necessary to legally check the parameters of the command.

K shell is the most suitable programmed shell on UNIX, so the following example is implemented by KSH.

Example 1: below

The function

Implementation of whether the entry parameter is more than 0:

Check_numberic ()

{

Temst = `Echo $ 1 | BC 2> / dev / null`

IF [-z "$ TEMSTR"] || [$ 1! = $ TEMSTR] || [$ 1 -LT 0] || Echo $ TEMSTR | GREP "."> / dev / null 2> & 1;

Echo "$ 1 Must Be An Positive Interger / N"

EXIT 1

Fi

}

This function uses the BC (1) command to determine the return value of the input parameters, more concusses. Through Example 1, you can master the following syntax of KSH:

1. Syntax of the function:

FunctionName ()

{

Several command line

}

The format of the call function is:

FunctionName Param1 param2 ...

The function has its own exit state, so the function can be used as a condition for the control structure of IF, while.

Unlike the C language, do not have a parameter description when the function is defined. However, when calling a function, the shell will give these parameters to $ 1, $ 2, ... and $ *, respectively.

2. IF condition statement: <>

IF condition command string; THEN

Conditions for the true command string

Else

Conditions for the command string of time

Fi

3.Test test command detailed test command instructions, you can get the Man Test.

The TEST command is used to check if a condition is established, which can be tested in terms of values, characters, and files, which will be briefly explained below:

(1) Value test:

-EQ: It is true that

-ne: It doesn't wait for it.

-gt: greater than it is true

-ge: greater than or equal to true

-lt: less than it is true

-LE: It is true to be equal to

(2) String test:

=: Is it true

! =: It is true if it is true

-z string: string length fake is true

-N String: The string length is not in fake.

(3) File test:

-e file name: If the file exists, true

-r file name: If the file exists and readable is true

-w file name: If the file exists and can be written

-x file name: true if the file exists and executable

-s file name: If the file exists and at least one character is true

-d file name: If the file exists and is true

-f file name: If the file exists and is true for ordinary files

-c file name: If the file exists and the characteristic special file is true

-b file name: If the file exists and a special file is true

Example 2:

The following functions

Really checking if the entrance parameter is one

legal

Serial port

Baud rate

Check_baud ()

{

DEFAULT_BAUD = `stty -a | grep speed | awk -f" "{print $ 2} '`

Check_numberic $ 1

STTY $ 1IF [$? -ne 0]; then

Echo "$ 1 Must Be a Baudrate / N"

EXIT 1

Else

Stty $ default_baud

Fi

}

First, the baud rate must be a positive integer, Example 2 utilized the function of Example 1

Check_numberic did check.

Then, use Stty

Judging whether the input parameter is a baud rate value

. This method is simpler than the method of comparing all possible baud rates than prior to preset all possible baud rates.

Example 2 used the predefined variable of the shell $?

4. Pre-defined variables

A predefined variable and an environment variable are similar, and the variables defined when the shell is started.

Different, users can only use these variables and cannot be redefined. All predefined variables consist of $ characters and another symbol:

$ #: The number of positional parameters

$ *: All location parameters

The state returned after the $ ?: command

$$: Current Process Process Number

$ !: Background running the last process number

$ 0: The current executive process name

Where "$?" Is used to check if the previous command is executed correctly (in UNIX / Linux, the command exit status is 0 indicates that the command is executed correctly, and any non-0 value indicates that the command is wrong).

The most common use of "$$" variable is to use the name of the temporary file to ensure that the temporary file will not be repeated.

Related Documents: Shell Programming Learning Notes (1)

转载请注明原文地址:https://www.9cbs.com/read-96614.html

New Post(0)