Shehan
Shehan

Reputation: 13

save wild-card in variable in shell script and evaluate/expand them at runtime

I am having trouble running the script below (in Cygwin on win 7 mind you). Lets call it "myscript.sh" When I run it, the following is what I input:

yearmonth: 2011-03
daypattern: 2{5,6,7}
logfilename: error*
query: WARN

#! /bin/bash
yearmonth=''
daypattern=''
logfilename=''
sPath=''
q=''

echo -n "yearmonth: "
read yearmonth

echo -n "daypattern: "
read daypattern

echo -n "logfilename: "
read logfilename

echo -n "query: "
read q

cat "$yearmonth/$daypattern/$logfilename" | grep --color $q

The output I get is:

cat: /2011-03/2{5,6,7}/error* No such directory of file exists.

However, if I enter daypattern=25 OR daypattern=26 etc. the script will work. Also, of course if I type the command in the shell itself, the wildcards are expanded as expected. But this is not what I want. I want to be able to PROMPT the user to enter the expressions as they need, and then later, in the script, execute these commands.

Any ideas how this can be possible?

Your help is much appreciated.

Upvotes: 1

Views: 1681

Answers (2)

RNA
RNA

Reputation: 153591

Use quote to prevent wildcard expansion:

$ a="*.py"
$ echo $a
google.py pair.py recipe-523047-1.py
$ echo "$a"
*.py

Upvotes: 0

jabbie
jabbie

Reputation: 2714

Try eval, this should work for the {a,d} and * cases

  eval grep --color $q  ${yearmonth}/${daypattern}/${logfilename}

Upvotes: 1

Related Questions