1

I have been using a shell script which used to do something like this:

output=$(ls -R /logs/{abc,cde}/"$((${date}/100))"/*/out*${date}* | grep -v "Random" )
echo $output

On running this command, I used to get the files with either abc or cde at relevant location. You can ignore other variables like date etc

However, when I modified the script to take abc,cde as command line parameter instead of hardcoding in the script, it stopped working. I changed the command to:

output=$(ls -R /logs/{${list}}/"$((${date}/100))"/*/out*${date}* | grep -v "Random" )

where list is abc,cde

I tried a lot of different combinations of quotes etc but it does not seem to be working. Can someone help please with the correct syntax that it works properly

jeb
  • 78,592
  • 17
  • 171
  • 225
  • What about `$list` instead of `$(list)`? (It's not a command that needs to be executed, it's just a variable) – Dominique Dec 23 '19 at 08:36
  • @Dominique But he used `${list}` and that's okay – jeb Dec 23 '19 at 08:37
  • yes i tried using both $list and ${list}. Both did not work – Shubhi Agarwal Dec 23 '19 at 08:45
  • 3
    Does this answer your question? [how to use variables with brace expansion](https://stackoverflow.com/questions/33491233/how-to-use-variables-with-brace-expansion) – David C. Rankin Dec 23 '19 at 08:51
  • 2
    @ShubhiAgarwal, Shubhi, could you please mention complete requirement here as parsing of ls output is never recommended, imho find could be helpful here, so please edit your question with complete details and let us know then. – RavinderSingh13 Dec 23 '19 at 08:52

2 Answers2

1

It doesn't work, because the expansion order prevents it.

The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion

You could solve it with using eval, but eval should be avoided at all.

Probably it's better to use find here.

find /logs -name 'abc/...' -o -name 'cde/...' 

This works even with variables

Or use the regex logic.

find /logs/ -regex '/logs/\(abc\|cde\).*'
jeb
  • 78,592
  • 17
  • 171
  • 225
  • @ShubhiAgarwal: as this solves your question, please accept the answer, so other users know this question is resolved and no more effort is spent looking to solve it. – Dominique Dec 23 '19 at 10:32
1

Try with this

  1. Assign abc and cde to an array ex - declare -a ARR=(abc cde)

  2. Assign it to variable like below.

    output=ls -R /logs/${ARR[@]/"$((${date}/100))"/*/out*${date}* | grep -v "Random"

SLS
  • 445
  • 1
  • 6
  • 20