Formatting long Lines A shell script that formats multiple lines of characters _linux shell

Source: Internet
Author: User

It hasn't been updated in a long time.
Think you should continue reading the script.

#14 Formatting Long Lines

#!/bin/sh
# A version of FMT, using Nroff. Adds Two useful flags:-W X for line width
#  and-h to enable hyphenation for better.
While getopts "HW:" OPT; Do case
 $opt in
  h) hyph=1        ;;
  W) width= "$OPTARG"   ;;
 Esac
done
shift $ (($OPTIND-1))
Nroff << EOF
. ll ${width:-72}
. na
hy ${hyph:-0}
. Pl 1
$ (cat "$@")
EOF
exit 0

The script above is used to format the output software.
The main use of the getopts to obtain processing parameters
Our uniform format under the command line is:

Command-options parameters

Getopts statement. The format of the statement is:
Getopts option_string Variable
Where option_string contains a valid single character option. If the getopts command finds a hyphen on the command line,
Then it will be compared with the character Fu Tong after the hyphen option_string. If there is a match, set the value of the variable variable to this option.
If there is no match, the variable is set to?. When Getopts discovers that there are no characters after the hyphen, a Non-zero state value is returned.
In a shell program, you can use the return value of getopts to create a loop.


Sometimes there is a value in the option, and the getopts command also supports this feature. Then you need to add a colon to the option_string letter. When the getopts command discovers a colon, the value is read from the command line after this option. If the value exists, a special variable optarg is present. If the value does not exist, the getopts command holds a question mark in the Optarg and displays a message on the standard error output.
------------
optstring option string, matched one by one
VarName options for success each time it matches
arg argument list, which takes the command line argument list when it is not written

$OPTIND special variable, option index increments by one, with an initial value of 1
$OPTARG Special variables, option argument, with different values in different situations
------------

Shift is used to reassign position parameters, in fact, to move the left one position to the parameter, such as: $1<--$2,$2<--$3 old $ will be deleted.

------------

Script explanation
Shift $ (($OPTIND-1) makes $@ the last argument, which is the file name.
${WIDTH:-72},${HYPH:-0} is used to define default values.
Nroff is now rarely used, and data discovery is used to format a text file as a line printer or a terminal screen.

It feels like the script is not very useful, and Linux has a ready-made command to do this function.
He is FMT.

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.