Two very detailed shell instance code

Source: Internet
Author: User
Tags binary to decimal chop echo command

Two very detailed shell instances
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:
Cp framework. sh myscript
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: Copy codeThe Code is 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
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 $ rval
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 $ rval
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 ;;
Esac
Done
# 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 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 calculates 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...Copy codeThe Code is 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 will 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 "$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 Array 8 7 6 5 4 3 2 1; do
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 detecting that a file name is provided by the user, we perform an Array-to-1 loop. File Array is named 10, file 8 is renamed as Array, 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-x strangescript
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-n your_script
This will return all syntax errors.
We hope that you can start writing your own shell script now and have a good time.

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.