简体   繁体   中英

Variable expansion within posix-extended regex

I've been trying to add a user-defined file extension into a regex with no success. Here's my script:

#!/bin/bash

echo -n "Enter which file extension are you looking for: "

read fExt

find . -maxdepth 1 -type f -regextype posix-extended -regex '^./q[0-9]{1,2}[a-z]?.\"$fExt\"$'

The file names I'm looking for look like

q1.txt
q22b.pdf
q3c.JPG

This version doesn't work so I've been using multiple ext1|ext2|... cases, which obviously isn't modular. How would I add $fExt into my -regex input? Is it possible in the first place, or is there an easier way to go about this? Thanks!

The way you've used single-quotes prevents $fExt from being expanded.

Assuming a bash-like shell, the regex you seem to want could be specified as follows:

"^q[0-9]{1,2}[a-z]?\.$fExt\$"

or

$'^q[0-9]{1,2}[a-z]?\\.'"$fExt"'$'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM