Shell Programming (3)

xiaoxiao2021-03-06  56

Example

General programming step

Now let's discuss the general steps to write a script. Any excellent script should have help and input parameters. And write a fake script (framework.sh), which contains the framework structure that most scripts need, is a very good idea. At this time, we only need to execute the copy command when writing a new script:

CP Framework.sh Myscript

Then insert your own function.

Let us look at two examples:

Binary to decimal conversion

The script B2D converts the binary number (such as 1101) into the corresponding decimal number. This is also an example of mathematical operation with an expr command:

#! / bin / sh

# vim: set sw = 4 TS = 4 et:

Help ()

{

Cat << Help

B2H - Convert Binary to Decimal

USAGE: B2H [-H] binarynum

Options: -h Help Text

EXAMPLE: B2H 111010

Will Return 58

Help

EXIT 0

}

Error ()

{

#print an error and exit

Echo "$ 1"

EXIT 1

}

Lastchar ()

{

# RETURN THE Last Character of A String In $ RAL

IF [-z "$ 1"]; then

# EMPTY STRING

Rval = ""

Return

Fi

# wc puts some space behind the output this is why we need sed:

Numofchar = `echo -n" $ 1 "| wc -c | sed 's /// g'`

# Now cut out the last char

Rval = `echo -n" $ 1 "| cut -b $ numofchar`

}

Chop ()

{

# Remove The Last Character in String and Return It in $ RALAL

IF [-z "$ 1"]; then

# EMPTY STRING

Rval = ""

Return

Fi

# wc puts some space behind the output this is why we need sed:

Numofchar = `echo -n" $ 1 "| wc -c | sed 's /// g'`

IF [$ NumOfchar "=" 1 "]; then

# Only One Char in String

Rval = ""

Return

Fi

Numofcharminus1 = `expr $ numofchar" - "1`

# Now cut all but the last char:

Rval = `echo -n" $ 1 "| cut -b 0 - $ {numofcharminus1}`

}

While [-n "$ 1"]; do

Case $ 1 in

-H) Help; shift 1 ;; # function help is called

-) SHIFT; Break ;; # end of options

- *) Error "Error: No Such Option $ 1. -h for help" ;;

*) BREAK ;;

Esacdone

# The main program

SUM = 0

Weight = 1

# One arg must be given:

[-z "$ 1"] && Help

Binnum = "$ 1"

Binnumorig = "$ 1"

While [-n "$ binnum"]; do

Lastchar "$ binnum"

IF [$ RVAL "=" 1 "]; then

Sum = `expr" $ weight "" "" $ sum "`

Fi

# REMOVE The Last Position in $ BINNUM

Chop "$ binnum"

Binnum = "$ RVAL"

Weight = `expr" $ weight "" * "2`

DONE

Echo "Binary $ BINNUMORIG IS DECIMAL $ SUM"

#

The algorithm used by this script is to use decimal and binary value (1, 2, 4, 8, 16, ..), such as binary "10" can be converted into Ten-based:

0 * 1 1 * 2 = 2

In order to get a single binary number, we use the lastchar function. This function uses the WC-C calculating the number of characters and then uses the CUT command to take out a character. The function of the CHOP function is to remove the last character.

File cycle program

Perhaps you want to save all sent messages to one of the people in a file, but after a few months, this file may become great to make the accesses for the file slow down. The following script RotateFile can solve this problem. This script can rename the mail save file (assuming to Outmail) is Outmail.1, and it has become Outmail.2, etc. for Outmail.1, etc. ...

#! / bin / sh

# vim: set sw = 4 TS = 4 et:

Ver = "0.1"

Help ()

{

Cat << Help

RotateFile - Rotate the file name

USAGE: ROTATEFILE [-H] FileName

Options: -h Help Text

Example: RotateFile Out

This Will E.G Rename Out.2 to out.3, out to out.2, out to out.1

AND CREATE AN EMPTY OUT-FILE

THE MAX NUMBER IS 10

Version $ VER

Help

EXIT 0

}

Error ()

{

Echo "$ 1"

EXIT 1

}

While [-n "$ 1"]; do

Case $ 1 in

-H) HELP; Shift 1 ;;

-) BREAK ;;

- *) Echo "Error: No Such Option $ 1. -h for help"; exit 1 ;;

*) BREAK ;;

ESAC

DONE

# input check:

IF [-z "$ 1"]; then

Error "error: you must specify a file, use -h for help"

Fi

FILEN = "$ 1"

# Rename Any .1, .2 etc file:

For N in 9 8 7 6 5 4 3 2 1; DoiF [-f "$ filen. $ n"]; then

P = `EXPR $ N 1`

Echo "MV $ Filen. $ n $ filen. $ p"

MV $ Filen. $ N $ filen. $ p

Fi

DONE

# Rename The Original File:

IF [-f "$ filen"]; then

Echo "MV $ Filen $ Filen.1"

MV $ Filen $ Filen.1

Fi

Echo Touch $ Filen

Touch $ Filen

How is this script work? After the user provides a file name, we will perform a cycle of 9 to 1. The file 9 is named 10, and the file 8 is renamed 9 and so on. After the loop is complete, we name the original file to file 1 to establish a empty file with the same name as the original file.

debugging

The easiest debug command is of course using an echo command. You can print any variable values ​​using Echo in any place where you doubt. This is also the cause of 80% of the time to debug the procedure. The advantage of the shell program does not need to recompile, and insert an echo command does not need much time.

The shell also has a real debug mode. If there is an error in the script "STRANGEScript", you can debug this:

SH -X StrangeScript

This will execute the script and display the value of all variables.

There is still a mode that doesn't need to perform scripts just check the syntax. It can be used like this:

sh -n your_script

This will return all syntax errors.

We hope that you can start writing your own shell script, I hope you have fun.

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

New Post(0)