Linux shell script basic learning (5)

Source: Internet
Author: User
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 )!

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.