Shell Learning 35-day----tilde expansion with wildcard characters

Source: Internet
Author: User
Tags alphabetic character posix

Tilde expansion with wildcard characters

two types of expansion in the shell that are related to file names . The first one is the wave number expansion . , The second type is a wildcard expansion .

Wave Number expansion

If the first character of the command line string is the tilde (~), or variable designation ( For example path or cdpath variable ) (~) ,shell

The purpose of the tilde expansion is You can specify the user who executes the program either directly or indirectly is the current user :
Command :vi ~/.profile        and vi  $HOME/.profile same

Command : VI ~root/.profile edit the . profile file for user root

Case analysis:First Command, Shellwill be~Replace$HOME,that is, the root directory of the current user.a second command,is theShellin the system's password vault.,need to find a userRoot,then the~rootdisplacement toRootthe root directory.

Benefits of using wave numbers :

1. This is a conceptual representation of a profile

2. This avoids the direct encoding of the path name in the program , For example :

There is a bash script :

printf "Enter Username:"

Read user

vi/home/$user/.profile Edit the user's . Profile File

This procedure assumes that all users ' root directories are under /home . If this changes any further , ( For example , the user subdirectory is stored in a subdirectory of the department directory according to the department ), then the script has to be rewritten. . However, if you use the tilde to expand , you can avoid rewriting the situation :

printf "Enter Username:"

Read user

vi/home/$user/.profile Edit the user's . Profile File

This way , the program works regardless of where the user's root directory is .

Using wildcard characters

Searching for special characters in the file name is also one of the services provided by the Shell .

Basic wildcard characters

Wildcard characters

The

*

Any of the string characters

[Set]

Any character in the set

[!set]

Any characters that are not in the set

?

Any single character

? wildcard matches any single character So if your directory contains demo.a,demo.b,demo.txt these three files demo.? match for DEMO.A,DEMO.B, but demo.txt does not match

Asterisk (*) is a powerful and widely used wildcard character It matches any character string using an expression demo.* Web designers can also use *.html expression matches their input file

a set structure is a list of groups of characters ( For example , ABC), a range of inclusions ( such as A- z), or a combination of the two. . If you want the dash to be part of the list , Just put it on the first or last one. .

wildcard characters using the set structure

An expression

Match a single character

[ABC]

A, b or c

[.,;]

Period , comma , or semicolon

[-_]

Dash or underline

[A-c]

A, b or c

[A-z]

Any one lowercase letter

[!0-9]

Any one non-numeric character

[0-9!]

Any number will be an exclamation point

[A-za-z]

Any one uppercase or lowercase letter

[a-za-z0_9_-]

Any one letter , any number , underscore or dash

In the original wildcard rebate , demo.[ AB] and demo.[ A-z] both match demo.a and demo.b, but demo.txt do not match .

The exclamation mark after the left parenthesis is used to "negate" a set. For example [!.;] matches any one character except a period and a semicolon ; [!a-za-z] matches any one non-alphabetic character .

The scope notation is convenient,But there are too many assumptions about the characters that are contained within the range.the more secure way is:Specify all uppercase letters, respectively,Lowercase Letters,Digital,or arbitrary sub-range(For example[F-q]. [2-6]).don't want to specify a range on punctuation characters,or use it in mixed letter-case.,like[A-z]with the[A-z]such a usage,There is no guarantee that the exact match will include all the letters you want.,and no other characters you don't want..The bigger problem is:Such scopes do not provide complete portability between computers of different types.

Another problem is:in many countries, the default system language environment and the pureASCIIthe character set is different..in order to solve this problem, POSIXThe standard presents the square brackets expression,used to denote letters .,Digital,punctuation and other types of characters,and has portability.The same element appears in the square brackets expression under the regular expression,They can be used in compatiblePOSIXof theShellwithin theShellin wildcard mode,However, you should try to avoid applying it to portableShellin the script.

Accustomed to the,When you perform a wildcard expansion, the Linux shella file with a point number beginning with the file name is ignored.like this."Dot file" is typically used as a program configuration file or as a startup file(it's usually hidden.,need to usels-ato view).like aShellof the$HOME/.profile,ex/viEditor's$HOME/.EXRC,as wellBashwith thegdbused byGNU ReadLineof the program library$HOME/.inputrc.

To see this type of file , you need to provide a point number explicitly before the pattern . For example :

Echo. * Show hidden files

Note : hidden files are only a customary usage . On user-level software, he does. , but the core program (Kernel) It is not considered that a file with a dot at the beginning is different from other files .

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Shell Learning 35-day----tilde expansion with wildcard characters

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.