Wild Card in Script

Latest response

Hello. I am trying to run a Shell script and I get the following output to the console.

 ./georgetest.sh
+ file9='/u01/jde/interface/outbound/EXAMPLE_USD_ALL_OTHB_*.txt'
+ '[' -f '/u01/jde/interface/outbound/EXAMPLE_USD_ALL_OTHB_*.txt' ']'

There are no error messages. I am wondering if it is beause of the Wild Card that is in the script. Here is my script.

#!/bin/bash -x

file9="/u01/jde/interface/outbound/EXAMPLE_USD_ALL_OTHB_*.txt"

#
if [ -f "$file9" ]
then
        echo "Sending an attachment." | mutt -a $( printf -- '-a %q'/u01/jde/interface/outbound/EXAMPLE_USD_ALL_OTHB_*.txt) -s "Example USD All OTHB *" xxxx@drew-marine.com,xxxxx@drew-marine.com,xxxxxx@drew-marine.com,xxxxx@drew-marine.com,xxxxx@drew-marine.com,xxxxxx@drew-marine.com,
        echo $file9 " mailed at time below:" >> "/u01/jde/interface/outbound/example2-files.log"
        date >> "/u01/jde/interface/outbound/example2-files.log"
        rm -f /u01/jde/interface/outbound/backup/EXAMPLE_USD_ALL_OTHB_*.txt
        cp $file9 /u01/jde/interface/outbound/backup/
        rm -f $file9
fi

I tried enclosing "$( printf -- '-a %q'/u01/jde/interface/outbound/EXAMPLE_USD_ALL_OTHB_*.txt) -s" in double quotes but the result is the same. There is nothing in th elog file either.

Responses

Hey George,
can you enclose your actual script in 3 tildes to start and finish (to make it easier to see what we are helping with ;-)
"~~~"
script goes here
"~~~"

and make sure to include the entire script - it appears there is a # missing, but I think that translates to a Heading marker or something. I'm sure we can get you through this though ;-)
I believe your if statement should not have double-quotes

if [ -f ${file9} ]

Are you trying to essentially "deal" with ALL files in the directory which begin with "EXAMPLE_USD_ALL_OTHB_"?

I would do something like

MYSTRING="EXAMPLE_USD_ALL_OTHB_"
for FILE in `find /u01/jde/interface/outbound/${STRING}*.txt`
do 
  echo $FILE
done

Or in the example you provided


echo "Sending an attachment." | mutt -a $( printf -- '-a %q'${FILE}) -s "Example USD All OTHB *"

which, I believe could simply be

echo "Sending an attachment." | mutt -a ${FILE} -s "Example USD All OTHB *" 

I'll apologize in advance if I'm not interpreting the goal correctly.

Here is an example:

[jradtke@neo tmp]$ touch EXAMPLE_USD_ALL_OTHB_A.txt
[jradtke@neo tmp]$ touch EXAMPLE_USD_ALL_OTHB_9.txt
[jradtke@neo tmp]$ touch EXAMPLE_USD_ALL_OTHB_2.txt
[jradtke@neo tmp]$ STRING="EXAMPLE_USD_ALL_OTHB_"
[jradtke@neo tmp]$ for FILE in `find /home/jradtke/tmp/${STRING}*.txt`; do echo $FILE; done
/home/jradtke/tmp/EXAMPLE_USD_ALL_OTHB_2.txt
/home/jradtke/tmp/EXAMPLE_USD_ALL_OTHB_9.txt
/home/jradtke/tmp/EXAMPLE_USD_ALL_OTHB_A.txt

Here is my code.

"~~~"

!/bin/bash -x

file9="/u01/jde/interface/outbound/george_test_ALL_OTHB_*.txt"

if [ -f {$file9} ]
then
echo "Sending an attachment." | mutt -a $( printf -- '-a %q'/u01/jde/interface/outbound/george_test_ALL_OTHB_.txt) -s "Example USD All OTHB *" gbottarini@drew-marine.com
echo $file9 " mailed at time below:" >> "/u01/jde/interface/outbound/george-test-files.log"
date >> "/u01/jde/interface/outbound/george-test-files.log"
rm -f /u01/jde/interface/outbound/backup/george_test_ALL_OTHB_
.txt
cp $file9 /u01/jde/interface/outbound/backup/

rm -f $file9

fi

#
"~~~"

I'm sorry George - I didn't realize the double-quotes weren't escaped. You just need the tilde's - no double-quotes

~~~
code
~~~

Sorry I am confused. you want me to cut and paste my code in between the tildas here in this comment and not in the code?

George, I added formatting to your original post for you, and also redacted the company name you included in the script, as it's best not to include this kind of information.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.