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.