Two very detailed shell instances
General programming steps
Now let's discuss the general steps to write a script. Any good script should have help and input parameters. and write a pseudo script (framework.sh) that contains the framework structure that most scripts require, and is a very good idea. At this point, we only need to execute the copy command when writing a new script:
CP framework.sh MyScript
And then insert your own function.
Let's look at two more examples:
Binary to decimal conversion
The script b2d converts the binary number (for example, 1101) to the corresponding decimal number. This is also an example of a mathematical operation using the expr command:
Copy Code code as follows:
#!/bin/sh
# Vim:set sw=4 ts=4 et:
Help ()
{
Cat < B2H-convert binary to Decimal
USAGE:B2H [-h] Binarynum
OPTIONS:-H Help text
EXAMPLE:B2H 111010
would return 58
Help
Exit 0
}
Error ()
{
# Print an error and exit
echo "$"
Exit 1
}
Lastchar ()
{
# return ' last character ' a string in $rval
If [-Z "$"]; Then
# empty string
Rval= ""
Return
Fi
# WC puts some space behind the ' output ' is why we need sed:
Numofchar= ' Echo-n ' "$" | wc-c | Sed ' s///g '
# now cut out of the last Char
Rval= ' Echo-n ' "$" | Cut-b $numofchar '
}
Chop ()
{
# Remove the last character in string and return it in $rval
If [-Z "$"]; Then
# empty string
Rval= ""
Return
Fi
# WC puts some space behind the ' output ' is why we need sed:
Numofchar= ' Echo-n ' "$" | 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 char:
Rval= ' Echo-n ' "$" | Cut-b 0-${numofcharminus1} '
}
While [-N ' $]; Todo
Case is in
-h) Help;shift 1;; # function Help is called
-Shift;break;; # End of options
-*) Error "error:no such option $. -H for help ";;
*) break;;
Esac
Done
# The main program
Sum=0
Weight=1
# One ARG must be given:
[-Z ' $] && Help
Binnum= "$"
Binnumorig= "$"
While [-N "$binnum"]; Todo
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 script uses an algorithm that uses decimal and binary number weights (1,2,4,8,16,..), such as binary "10" to be converted to decimal:
0 * 1 + 1 * 2 = 2
In order to get a single binary number, we used the Lastchar function. The function uses WC? c to calculate the number of characters, and then use the Cut command to remove the end of a character. The function of the chop function is to remove the last character.
File Loop procedure
Perhaps you want to save all messages sent to one of the people in a file, but after a few months, the file may become so large that it slows down access to the file. The following script rotatefile can solve this problem. This script can rename the Mail save file (assumed to be outmail) as OUTMAIL.1, and for OUTMAIL.1 becomes outmail.2 and so on ...
Copy Code code as follows:
#!/bin/sh
# Vim:set sw=4 ts=4 et:
Ver= "0.1"
Help ()
{
Cat < Rotatefile--Rotate the file name
Usage:rotatefile [-h] FileName
OPTIONS:-H Help text
Example:rotatefile out
This is e.g rename out.2 to Out.3, Out.1 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 "$"
Exit 1
}
While [-N ' $]; Todo
Case is in
-h) Help;shift 1;;
-) break;;
-*) echo "error:no such option $. -h for help; exit 1;;
*) break;;
Esac
Done
# input Check:
If [-Z "$"]; Then
Error "Error:you must specify a file, use-h for help"
Fi
Filen= "$"
# Rename any. 1,. 2 etc File:
For N in Array 8 7 6 5 4 3 2 1; Todo
If [-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 does this script work? After testing the user for a filename, we do a loop of array to 1. The file array is named 10, file 8 is renamed array, and so on. After the loop completes, 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, of course, is to use the echo command. You can use Echo to print any variable values in any suspect error area. This is why most shell programmers spend 80% of their time debugging programs. The advantage of a shell program is that you don't need to recompile, and it doesn't take much time to insert an echo command.
The shell also has a real debug mode. If there are errors in the script "Strangescript", you can debug this:
Sh-x Strangescript
This executes the script and displays the values of all the variables.
The shell also has a pattern that does not need to execute a script just to check the syntax. You can use this:
Sh-n Your_script
This will return all syntax errors.
We hope you can start writing your own shell script now and hope you enjoy yourself.