This article describes how to write a Linux command line utility that is simple enough for end users. This article begins with an overview of reliable command line best practices and ends with a detailed study of an effective page selection tool.
This article describes how to write a Linux command line utility that is simple enough for end users. This article begins with an overview of reliable command line best practices and ends with a detailed study of an effective page selection tool, providing you with the background knowledge required to write your own utility.
This article demonstrates how to compile a Linux command line utility similar to standard commands such as cat, ls, pr, and mv. I selected a utility named selpg, which represents SELect PaGes. Selpg allows you to specify the range of pages extracted from input text, which can come from a file or another process. Selpg is created based on the actual conventions for creating commands in Linux. these conventions include:
Independent work
The command line option that is used as a component in the command pipeline to accept modifications to its behavior by reading standard input or file name parameters and writing to standard output and standard errors. Not long ago I developed selpg for a customer. Then I published it on a UNIX mail list, and many members told me they found it a useful tool.
The utility reads text input from a standard input or from a file name given as a command line parameter. It allows you to specify the page range from the input and will be output later. For example, if the input contains 100 pages, you can specify to print only 35th to 65 pages. This feature is of practical value because printing selected pages on a printer avoids wasting paper. Another example is that the original file is large and has been printed before, but some pages are not correctly printed due to printer jam or other reasons. In this case, you can use this tool to print only the pages to be printed.
In addition to examples of Linux utilities, this article also has the following features:
It illustrates the capabilities of the Linux software development environment with examples. It demonstrates some system calls and appropriate use of C library functions, including fopen, fclose, access, setvbuf, perror, strerror, and popen. It implements a thorough error check that is intended for a general purpose utility (instead of a one-time program. It warns of potential problems, such as buffer overflow that may occur during programming in C, and provides suggestions on how to prevent these problems. It demonstrates how to parse manually-encoded command line parameters. It demonstrates how to use this tool in an MPs queue and when the input, output, and error stream are redirected.
Linux command line principles
Writers of general Linux utilities should observe certain guidelines in the code. These guidelines have been developed for a long time and help ensure that users use the utility in a more flexible manner, especially with other commands (built-in or written by users) and shell collaboration-this collaboration is one of the ways to use Linux as a development environment. The selpg utility uses examples to illustrate all the principles and features listed below. (Note: In the following examples, the "$" symbol represents a shell prompt and does not need to be entered .)
Linux command line principle 1. input
The following two methods are allowed:
The name of the file specified on the command line. For example:
$ Command input_file
In this example, the command should read the file input_file.
Standard input (stdin), which is a terminal (the user's keyboard) by default ). Example: $ command
Here, all the content entered before the user input the Control-D (file end indicator) is the command input. However, you can use the shell operator "<" to redirect the standard input to the source file, as shown below:
$ Command <input_file
Here, the command will read its standard input, but the shell/kernel has redirected it, so the standard input comes from input_file.
Using the shell operator "|" (pipe) can also make the standard input come from the standard output of another program, as shown below:
$ Other_command | command
Here, the standard output (stdout) of other_command is transparently transmitted to the standard input of command by shell/kernel.
Linux command line principle 2. output
The output should be written to the standard output. by default, the standard output is also a terminal (that is, the user's screen ):
$ Command
In this example, the command output appears on the screen.
Similarly, you can use the shell operator ">" to redirect the standard output to a file.
$ Command> output_file
Here, the command is still written to its standard output, but the shell/kernel redirects it, so the output is written to output_file. Or, use the "|" operator. the output of command can be the standard input of another program, as shown below:
$ Command | other_command
In this example, the shell/kernel arranges command output as the input of other_command.