Linux Shell script basic learning (5)

Source: Internet
Author: User

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]; then

Cat <

Ren -- renames a number of files using sed Regular Expressions

Usage: Ren 'regexp' 'replace 'files...

Example: rename all *. HTM files in *. html:

Ren 'htm $ ''html' *. htm

Help

Exit 0

Fi

Old = "$1"

New = "$2"

# The shift command removes one argument from the list

# Command line arguments.

Shift

Shift

# $ * Contains now all the files:

For file in $ *; do

If [-F "$ file"]; then

Newfile = 'echo "$ file" | sed "s/$ {old}/$ {New}/g "'

If [-F "$ newfile"]; then

Echo "error: $ newfile exists already"

Else

Echo "Renaming $ file to $ newfile ..."

Mv "$ file" "$ newfile"

Fi

Fi

Done

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. After the help text is printed, Program exits. 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.

Related Article

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.