1

So, what I am trying to do is look for a set of files, and then have it echo which ones it has found. For example, this is what I have

    #!/bin/bash

    LaunchDaemon="LaunchDaemon.plist" 
    launchAgent="LaunchAgent"
    mobileLaunchDaemon="MobileDaemon.plist"
    mobileAgent="MobileAgent"

    if [ -f "$LaunchDaemon" ] || [ -f "$launchAgent" ] || [ -f "$mobileLaunchDaemon" ] || [ -f "$mobileAgent" ]
then
    echo "<result>Found</result>"
else
    echo "<result>Not Found</result>"
fi

So, this will tell me if it can find any of them, but it won't tell me which one it is finding. I created another with elif statements inside, which works sort of, but if it finds the first one, it stops there and won't tell me if the others are there as well. So, I hope this makes sense, I'm just trying to find a way to display which of the files have been found.

  • can't you use if..elif..else..fi statement ? – user994144 Aug 3 '16 at 12:50
  • How about having separate if statements for each of the possible things and build the result string in a variable which is echoed out only in the end? – zagrimsan Aug 3 '16 at 13:01
3

You need to split your if test into multiple

if [ -f "$LaunchDaemon" ]
then
  echo $LaunchDaemon found
elif [ -f "$launchAgent" ]
then
  echo $launchAgent found
elif [ ... 
  ...
else
  echo Not found
fi

type structure (fill in the blanks yourself.).

If you want to find all matches just do it as multiple tests, setting a variable to see if anything was found or not

found=0
if [ -f "$LaunchDaemon" ]
then
  echo $LaunchDaemon found
  found=1
fi
if [ -f "$launchAgent" ]
then
  echo $launchAgent found
  found=1
fi
...
if [ $found == 0 ]
then
  echo Not found
fi

Now sometimes we want to set a variable so we know which one was found. This is common when trying to find a matching program or file (eg in the old days we might have had /usr/lib/sendmail or /usr/sbin/sendmail depending on the OS distribution, so we'd need to search to find it).

found=
for f in "$LaunchDaemon" "$launchAgent" "$mobileLaunchDaemon" "$mobileAgent"
do
  [[ -f "$f" ]] && found="$f"
done

Now we have $found pointing to a found entry and can test on that.

if [ -n "$found" ]
then
  echo Found: $found
else
  echo Nothing found
fi

The second loop can also find all versions with a minor change:

found=
for f in "$LaunchDaemon" "$launchAgent" "$mobileLaunchDaemon" "$mobileAgent"
do
  [[ -f "$f" ]] && found="$found $f"
done

The downside of this is that there may be a leading space in front, so we should remove that:

found="${found# }"
  • +1. BTW, a minor modification of that final for loop can be used to choose a file to use from one of several dirs - e.g. for d in /etc/program /usr/share/program /usr/local/etc ~/.program ; do [ -e "$d/conf" ] && configfile="$d/conf" ; done; [ -z "$configfile" ] && echo "no config found" && exit 1 - the last one found will be used, so dirs are in order from lowest to highest priority. – cas Aug 4 '16 at 5:03
1

An array would be the best approach here

#!/bin/bash
#Store the filenames in an array, also less management overhead
arry=( "LaunchDaemon.plist" "LaunchAgent" "MobileDaemon.plist" "MobileAgent" )
#Optioinally if you wish to add one more file to check you could
#uncomment the below line
#arry+=("$1") #One more file added from the command line
for i in "${arry[@]}" #Iterate through each element using a for loop
do
 if [ -f "$i" ]
 then
  echo "<result> $i Found</result>" 
 else
  echo "<result> $i Not Found</result>"
 fi
done
0

try this

#!/bin/bash
search_LaunchDaemon="LaunchDaemon.plist"
search_launchAgent="LaunchAgent"
search_mobileLaunchDaemon="MobileDaemon.plist"

for filex  in ${!search_*}
do
found=${!filex}
#echo  -e "${filex}=${!filex}"
#we remove the prefix "search_"
IFS="_" read part1 part2 <<< "${filex}"

if [[ -f $found ]];
then
echo "I have found ${part2}"
else
echo "${part2} not found!"
fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.