Bracket wildcard usage command line
Hi,
I'm trying to use bracket wildcard on the command line like this: grep SOMETHING /var/log/node/2019/02/[01-07]/messages Here is the layout to give you an idea: ## pwd /var/log/node/2019/01 ## ls 01 03 05 07 09 11 13 15 17 19 21 23 25 27 29 31 02 04 06 08 10 12 14 16 18 20 22 24 26 28 30 What is the correct syntax to use bracket wildcard like I want to above? Thank you. |
Code:
grep "SOMETHING" /var/log/node/2019/02/{01..07}/messages |
That worked great from the command line!
Now I tried to put it in a script with 01 and 07 as variables but it won't read it: ## DAY1=01; DAY7=07 ## echo $DAY1 01 ## echo $DAY7 07 ## grep "SOMETHING" /var/log/node/2019/02/{$DAY1..$DAY7}/messages grep: /var/log/node/2019/02/{01..07}/messages: No such file or directory |
Code:
echo {01..07} Code:
a=01 Code:
c=({01..07}) Code:
c=({01..07}) Code:
for i in /var/log/node/2019/01/{01..30}/messages; do |
Quote:
|
Better check for existence in the loop and skip non-existent files
Code:
for f in /var/log/node/2019/01/{01..30}/messages; do |
Since OP has requested a solution with variables, I suggest the following:
Code:
#!/bin/bash |
But that looks a bit, well, sub-optimal, doesn't it?
Regarding post#1 The "bracket wildcard" is a "character set". Represents one character. So 0[0-7] or 1[0-7] works. But if the range spans over a decade then this method fails. BTW the character set is really a "wildcard" like * because it is matched against files, and generates what it finds. In contrast, { } generates a range unconditionally, without matching files. |
^ I agree with everything you said.
Actually, I suggested #7 because that's the only solution that came to my mind to satisfy the initial OP's request, namely to define range with variables inside a script... |
All times are GMT -5. The time now is 07:41 AM. |