How do I show the results found in an if then statement?
up vote
1
down vote
favorite
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.
bash shell-script
add a comment |
up vote
1
down vote
favorite
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.
bash shell-script
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
bash shell-script
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.
bash shell-script
bash shell-script
edited Nov 26 at 1:00
Rui F Ribeiro
38.3k1475127
38.3k1475127
asked Aug 3 '16 at 12:46
Ohitsspencer
83
83
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
add a comment |
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
can't you use if..elif..else..fi statement ?
– user994144
Aug 3 '16 at 12:50
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
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
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
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 finalfor
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
add a comment |
up vote
1
down vote
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
add a comment |
up vote
0
down vote
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
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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 finalfor
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
add a comment |
up vote
3
down vote
accepted
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 finalfor
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
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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# }"
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# }"
answered Aug 3 '16 at 13:07
Stephen Harris
23k24176
23k24176
+1. BTW, a minor modification of that finalfor
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
add a comment |
+1. BTW, a minor modification of that finalfor
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. 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. 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
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
up vote
1
down vote
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
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
edited Aug 3 '16 at 13:21
answered Aug 3 '16 at 13:15
sjsam
1,1551919
1,1551919
add a comment |
add a comment |
up vote
0
down vote
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
add a comment |
up vote
0
down vote
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
add a comment |
up vote
0
down vote
up vote
0
down vote
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
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
answered Jan 3 '17 at 15:13
Talal
1032
1032
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f300030%2fhow-do-i-show-the-results-found-in-an-if-then-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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