Summary of LinuxShell script introduction: 3

Source: Internet
Author: User
Summary of LinuxShell script introduction: 3. split file names based on the extension. First, let's take a look at two examples: [delphi] file_jpg & quot; sample. jgp & quot; name $ {file_jpg %. *} echoFilenameis: $ name output result: Filenameissample... summary of Linux Shell script introduction: 3. split file names based on the extension. First, let's take a look at two examples: [delphi] file_jpg = "sample. jgp "name =$ {file_jpg %. *} echo File name is: $ name output result: File name is sample [delphi] extension =$ {file_jpg #*.} echo Extension is: $ extension output result: Extension is: jpg works in the first task,. expansion Get the file name in "exhibition name". We use the % operator $ {$ var %. *} meaning: 1. remove the matching string of the wildcard located on the right of % from $ var. Wildcard Matching from right to left 2. assign a value to var, so the output file name % is a non-greedy operation. It finds the shortest result of matching the wildcard from the right to the left. There is also an operator %. This operator is similar to %, but the behavior pattern is indeed greedy, which means it will match the longest string that meets the condition. In the second task, the principle is similar to %, but its running process is from left to right. The operator # Uses *. to perform greedy matching from left to right. Here is an example: in which the URL = "www.google.com" is used to batch rename a file. the following example shows how to batch rename a file. a wide range of formats are supported: first, we need to place the test image in the directory, as shown in: rename. sh is the script file that implements the function. we run the script program and get the output as follows: Let's see if the file status has actually changed. the following shows the implemented script program: [delphi] #! /Bin/bash # rename. sh count = 1; for img in *. [jJ] [pP] [gG] *. [pP] [nN] [gG] do new = image-$ count. $ {img ##*.} mv "$ img" "$ new" 2>/dev/null if [$? -Eq 0] then echo "Renaming $ img to $ new" let count ++; fi done

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.