Instance
General programming steps
Now let's discuss the general steps for writing a script. Any excellent script should have help and input parameters. And write a pseudo script (Framework. Sh) that contains the framework structure required by most scripts, which is a very good idea. At this time, when writing a new script, we only need to execute the Copy command:
Cpframework. shmyscript
Then insert your own function.
Let's look at two more examples:
Binary to decimal conversion
The script b2d converts the binary number (such as 1101) to the corresponding decimal number. This is also an example of a mathematical operation using the expr command:
#! /Bin/sh
# VIM: setsw = 4ts = 4et:
Help ()
{
Cat
Usage: B2H [-H] binarynum
Options:-hhelptext
Example: b2h111010
Willreturn58
Help
Exit0
}
Error ()
{
# Printanerrorandexit
Echo "$1"
Exit1
}
Lastchar ()
{
# Returnthelastcharacterofastringin $ rval
If [-z "$1"]; then
# Emptystring
Rval = ""
Return
Fi
# Wcputssomespacebehindtheoutputthisiswhyweneedsed:
Numofchar = 'echo-n "$1" | WC-c | SED's // g''
# Nowcutoutthelastchar
Rval = 'echo-n "$1" | cut-B $ numofchar'
}
Chop ()
{
# Removethelastcharacterinstringandreturnitin $ rval
If [-z "$1"]; then
# Emptystring
Rval = ""
Return
Fi
# Wcputssomespacebehindtheoutputthisiswhyweneedsed:
Numofchar = 'echo-n "$1" | WC-c | SED's // g''
If ["$ numofchar" = "1"]; then
# Onlyonecharinstring
Rval = ""
Return
Fi
Numofcharminus1 = 'expr $ numofchar "-" 1'
# Nowcutallbutthelastchar:
Rval = 'echo-n "$1" | cut-b0 ${numofcharminus1 }'
}
While [-n "$1"]; do
Case $ 1In
-H) Help; shift1; # functionhelpiscalled
--) Shift; break; # endofoptions
-*) Error "error: nosuchoption $1.-hforhelp ";;
*) Break ;;
Esac
Done
# Themainprogram
Sum = 0
Weight = 1
# Oneargmustbegiven:
[-Z "$1"] & Help
Binnum = "$1"
Binnumorig = "$1"
While [-n "$ binnum"]; do
Lastchar "$ binnum"
If ["$ rval" = "1"]; then
Sum = 'expr "$ weight" $ sum "'
Fi
# Removethelastpositionin $ binnum
Chop "$ binnum"
Binnum = "$ rval"
Weight = 'expr "$ weight" "*" 2'
Done
Echo "binary $ binnumorigisdecimal $ sum"
#
The script uses decimal and binary weights (, 16,...). For example, binary "10" can be converted to decimal:
0*1 1*2 = 2
To obtain a single binary number, we use the lastchar function. This function uses WC-C to calculate the number of characters, and then uses the cut command to retrieve the last character. The Chop function removes the last character.
File loop Program
Maybe you want to save all emails to one of the people in a file, but after a few months, this file may become so large that the access to this file may be slowed down. The following script rotatefile can solve this problem. This script can rename the email storage file (assuming outmail) to outmail.1, and change outmail.1 to outmail.2 and so on...
#! /Bin/sh
# VIM: setsw = 4ts = 4et:
Ver = "0.1"
Help ()
{
Cat
Usage: rotatefile [-H] filename
Options:-hhelptext
Example: rotatefileout
Thiswille. grenameout.2toout. 3, out.1toout. 2, outtoout.1
Andcreateanemptyout-File
Themaxnumberis10
Version $ ver
Help
Exit0
}
Error ()
{
Echo "$1"
Exit1
}
While [-n "$1"]; do
Case $ 1In
-H) Help; shift1 ;;
--) Break ;;
-*) Echo "error: nosuchoption $1.-hforhelp"; exit1 ;;
*) Break ;;
Esac
Done
# Inputcheck:
If [-z "$1"]; then
Error "error: youmustspecifyafile, use-hforhelp"
Fi
Filen = "$1"
# Renameany.1,. 2 etcfile:
Fornin987654321; do
If [-F "$ filen. $ N"]; then
P = 'expr $ n 1'
Echo "Mv $ filen. $ N $ filen. $ P"
MV $ filen. $ N $ filen. $ P
Fi
Done
# Renametheoriginalfile:
If [-F "$ filen"]; then
Echo "Mv $ filen $ filen.1"
MV $ filen $ filen.1
Fi
Echotouch $ filen
Touch $ filen
How does this script work? After the user provides a file name, we perform a 9-1 loop. File 9 is named 10, file 8 is renamed to 9, and so on. After the loop is completed, we name the original file as file 1 and create an empty file with the same name as the original file.
Debugging
The simplest debugging command is the echo command. You can use echo to print any variable value in any suspected error. This is why most shell Programmers spend 80 of their time debugging programs. The benefit of a shell program is that it does not need to be re-compiled, and it does not take much time to insert an echo command.
Shell also has a real debugging mode. If an error occurs in the script "strangescript", you can debug it as follows:
Sh-xstrangescript
This will execute the script and display the values of all variables.
Shell also has a mode that only checks the syntax without executing the script. It can be used as follows:
Sh-nyour_script
This will return all syntax errors.
We hope that you can start writing your own shell script now and have a good time.