The foundation of the Linuxshell script has been divided into several parts. the content of the control process is now finished. this is the last part about heredocument. The example here is a little complicated, we will analyze this complex Linuxshell script. The Linux shell script has been divided into several parts, and the content of the control process is now finished. this is the last part about here document. The example here is a little complicated, we will analyze this complex Linux shell script.
6. Here documents
To pass several lines of text to a command, here documents is a good method. It is very useful to write a helpful text for each script. if we have the here documents, we do not need to use the echo function to output a row. A "Here document" starts with <, followed by a string, which must also appear at the end of the here document. The following is an example. In this example, we rename multiple files and print the help using here documents:
#! /Bin/sh # we have less than 3 arguments. Print the help text: if [$ #-lt 3]; thencat
This is a complex example. Let's discuss it in detail. The first if expression determines whether the number of input command line parameters is smaller than 3 (special variable $ # indicates the number of parameters included ). If the number of input parameters is less than three, the help text is passed to the cat command and then printed on the screen by the cat command. Print the help text and exit the program. If the input parameter is equal to or greater than three, we assign the first parameter to the variable OLD, and the second parameter to the variable NEW. Next, we use the shift command to delete the first and second parameters from the parameter list, so that the original third parameter becomes the first parameter in the parameter list $. Then we start the loop. the command line parameter list is assigned to the variable $ file one by one. Then we determine whether the file exists. If yes, we use the sed command to search for and replace the file to generate a new file name. Then, assign the command result in the backslash to newfile. In this way, we can achieve our goal: get the old and new file names. Then use the mv command to rename the file. In this way, we can understand this complicated Linux shell script.
The above is a detailed introduction to basic learning of Linux shell scripts (5). For more information, see PHP Chinese network (www.php1.cn )!