I am trying to write a bash script that is suppose to find the file that has a specific string in it. The script calls another script that returns strings of the format:
url=title
title is the string I am looking for. title can have values that look like this for example: 'A Soldier Of The Legion'.
I am trying to find the file in the /tmp/audiobooksdirectory that contains the title, 'A Soldier Of The Legion'. All the files in /tmp/audiobooks have names that end with AB.yaml.
Here is the script:
#!/bin/sh
get_pairs='/home/me/util/scripts/get-pairs.sh'
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in `$get_pairs`
do
echo "pair $i"
url=`echo $i | cut -d= -f1`
apptitle=`echo $i | cut -d= -f2- | cut -c1-52`
echo "grep -l $apptitle /tmp/audiobooks/*AB.yaml | head -1"
the_file=$(grep -l $apptitle /tmp/audiobooks/*AB.yaml | head -1)
echo "the_file=$the_file"
if [ -z $the_file ]
then
echo "No hiera file found for $apptitle ... skipping"
continue
fi
appname=`basename $the_file .yaml`
echo "url is[$url] and apptitle is [$apptitle] appname is [$appname]"
exit 0
done
IFS=$SAVEIFS
The output the script produces is this:
pair http://www.example.com/product/B06XK9FGYD='A Soldier Of The Legion'
grep -l 'A Soldier Of The Legion' /tmp/audiobooks/*AB.yaml | head -1
the_file=
No hiera file found for 'A Soldier Of The Legion' ... skipping
pair http://www.example.com/product/B01GWQI0OS='Art of War Sun Tzu'
grep -l 'Art of War Sun Tzu' /tmp/audiobooks/*AB.yaml | head -1
the_file=
No hiera file found for 'Art of War Sun Tzu' ... skipping
pair http://www.example.com/product/B0717333MM='Bartleby, the Scrivener (version 2)'
grep -l 'Bartleby, the Scrivener (version 2)' /tmp/audiobooks/*AB.yaml | head -1
the_file=/tmp/audiobooks/BartlebyTheScrivener_AMZAD_AB.yaml
url is[http://www.example.com/product/B0717333MM] and apptitle is ['Bartleby, the Scrivener (version 2)'] appname is [BartlebyTheScrivener_AMZAD_AB]
The odd things is that each of the grep commands I echo out work when I run them from the command line ... for example:
$ grep -l 'A Soldier Of The Legion' /tmp/audiobooks/*AB.yaml | head -1
/tmp/audiobooks/A_Soldier_of_the_Legion_AB.yaml
And the script works for the title, 'Bartleby, the Scrivener (version 2)'.
IFS=$'\n\b'for assignment of the literal string as opposed toIFS=$(echo -en "\n\b"). There is no need to use command substitution to setIFS.