Need to use asterisk in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to use asterisk in variable
# 1  
Old 08-27-2014
Need to use asterisk in variable

Hi All,

I am having a challange to pass the asterisk in the variable. Basically, I am writing a shell script to check if a marker file exists but when I am assigning the varialbe it cannot use the wildcard asterisk as expected, therefore, my program is always outputs "Marker file is not arrived". Can someone pease help me how do I get it correct.

Marker file will arrive as Marker_YYYYMMDD.tmp but I would like to define it as below ...

Code:
 
FLAG=/test/Marker_*.tmp
        if [ -f "$FLAG" ]
        then
           echo "Marker file exists"
        else
           echo "Marker file is not arrived,"
        fi

# 2  
Old 08-27-2014
Try any one of:
Code:
FLAG="/test/Marker_*.tmp"
FLAG='/test/Marker_*.tmp'
FLAG=/test/Marker_\*.tmp
FLAG=/test/Marker_"*".tmp
FLAG=/test/Marker_'*'.tmp

# 3  
Old 08-27-2014
Code:
FLAG=/test/Marker_*.tmp
if [ -f ${FLAG} ]
then
 echo "Marker file exists"
else
 echo "Marker file is not arrived,"
fi

# 4  
Old 08-27-2014
Indeed the use of double quotes around "$FLAG" prevents the expansion of the wildcard. They can be left out, but then the test will fail if there are more files present with this pattern. To test for the presence of one of more files you could try:

Code:
FLAG=/test/Marker_*.tmp

file_exists() {
  for _i do
    [ -f "$_i" ] && break
  done
}

if file_exists $FLAG
then
  echo "One or more marker files exist"
else
  echo "No marker files have arrived yet"
fi



---
Quote:
Originally Posted by Don Cragun
Try any one of:
Code:
FLAG="/test/Marker_*.tmp"
FLAG='/test/Marker_*.tmp'
FLAG=/test/Marker_\*.tmp
FLAG=/test/Marker_"*".tmp
FLAG=/test/Marker_'*'.tmp

That should not be necessary, since wildcards are not expanded in variable assignments...

Last edited by Scrutinizer; 08-27-2014 at 04:54 AM..
# 5  
Old 08-27-2014
Thank you all for your kind help. It works like a charm Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to assign * asterisk to variable?

How can I assign asterisk to variable I have try a="\* " but I was not succesful :( any idea ? thanks (5 Replies)
Discussion started by: kvok
5 Replies

2. AIX

Replace string with asterisk(*) in variable

I was trying to replace a string ( for eg - @@asterisk@@ to * ) in variable using cat $INFILE | while read LINE do stmt1=`echo $LINE | sed 's/@@asterisk@@/\*/g'` stmt=$stmt' '$stmt1 stmt2=`echo $LINE` STATEMENT=$STATEMENT' '$stmt2 done echo 'Statement with sed -- > '... (5 Replies)
Discussion started by: Vaddadi
5 Replies

3. Shell Programming and Scripting

How to ignore * (asterisk) in a variable

I am using a shell script to read SQL statements stored in a DB2 table and write them out to a file. The problem I have is that some SQL statements have an "*" in them which gets resolved as the list of files in the current directory when I run the script. How can I prevent the "*" from being... (7 Replies)
Discussion started by: bradtri2
7 Replies

4. Shell Programming and Scripting

passing asterisk to a script as variable

I'm writing a script that will ssh to a number of hosts and run commands. I'm a bit stumped at the moment as some of the commands that I need to run contain wildcards (i.e. *), and so far I have not figured out how to escape the * character so the script doesn't expand it. More specifically, here's... (9 Replies)
Discussion started by: GKnight
9 Replies
Login or Register to Ask a Question