Learn more about Linux Shell Scripting Basics (vii)

Source: Internet
Author: User
Tags binary to decimal chop
Linux shell Script Basic Learning this part if only look at the theoretical part of the previous, although there are some examples, but not enough system, here will be a concrete example to show you the Linux shell script programming to help you improve the Linux Shell Foundation learning and improve.
Part 2nd Example
Now let's discuss the general steps for writing a script. Any good script should have help and input parameters. and write a pseudo-script (framework.sh), which contains the framework structure that most scripts need, 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 one more example:
Conversion of binary to decimal
The script b2d converts the binary number (for example, 1101) to the corresponding decimal number. This is also an example of a mathematical operation with the expr command:

#!/bin/sh# vim:set sw=4 ts=4 et:help () {Cat <b2h--Convert binary to DECIMALUSAGE:B2H [-h] binarynumoptions:-H Help TEXTEXAMPLE:B2H 111010will return 58HELPexit 0}error () {# Print an error and Exitecho "$" exit 1}lastchar () {# return the L AST character of a string in $rvalif [-Z ']; then# empty stringrval= "" returnfi# WC puts some space behind the output this was why we need sed:numofchar= ' echo-n ' | wc-c | Sed ' s///g ' # now cut out the last charrval= ' Echo-n "$" | Cut-b $numofchar '}chop () {# Remove the last character in string and return an IT in $rvalif [-Z] $ "]; then# empty STRINGRV Al= "" returnfi# WC puts some space behind the output this was why we need sed:numofchar= ' echo-n ' | wc-c | Sed ' s///g ' if ["$numofchar" = "1"]; then# only one char in stringrval= "" returnfinumofcharminus1= ' expr $numofchar "-" 1 ' # now cut all, and the last Char:rval= ' E Cho-n "$" | Cut-b 0-${numofcharminus1} '}while [-n ' $ ']; Docase $ in-h) Help;shift 1;; # function Help is called--) shift;break;; # End of options-*) error "error:no such option $. -H for help ";; *) break;; esacdone# the main programsum=0weight=1# one arg must be given:[-Z "$"] && helpbinnum= "$" binnumorig= "$" while [-N "$binnum"]; Dolastchar "$binnum" if ["$rval" = "1"]; thensum= ' expr ' $weight "+" "$sum" fi# remove the last position in $binnumchop "$binnum" binnum= "$rval" weight= ' expr "$weig HT "" * "2 ' Doneecho" binary $binnumorig is decimal $sum "

The algorithm used by the script is to take advantage of decimal and binary number weights (1,2,4,8,16,..), such as the binary "10" can be converted to decimal:

0 * 1 + 1 * 2 = 2


In order to get a single binary number we are using the Lastchar function. The function uses Wc–c to calculate the number of characters, and then uses the Cut command to remove the end character. The function of the chop function is to remove the last character.
This example of a Linux shell script has helped us with the conversion, and the next time we will be example a file loop program.

Above is the detailed introduction of the Linux shell Script Basic Learning (vii) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.