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
In the command pipeline, as a component, the command line option that reads standard input or file name parameters and writes to standard output and standard errors accepts the modification of its behavior. 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 utilities in a more flexible manner, especially those built in with other commands 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). By default, it is the terminal, that is, the user's keyboard ). For example:
$ command
Here, all the content entered before the end of the Control-D file is the command input.
However, you can use the shell operator "<" to redirect the standard input. You can also 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 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 the 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.