Reputation: 43
So My question is I am trying to figure how to read the output from the commands i posted.
So the output pastes a block of text showing the result of the import and in the text it also either says exit code = 0 if it has worked or exit code = 255 if it has not. So i want the string to take that exit code from the text and then depending on the error code it should either re-run the command and try again three times or quit the program if it has worked.
#!/bin/bash
#
i="0"
while [ $i -ne 3 ]
do
string1=$(sudo -u nginx php /var/www/html/shell/global/full_import.php --import finance_backup)
string2=$(sudo -u nginx php /var/www/html/shell/global/full_import.php --import finance_deactivate_quotes)
string3=$(sudo -u nginx php /var/www/html/shell/global/full_import.php --import finance)
if [ "$string1" == "code = 255" ] || [ "$string2" == "code = 255" ] || [ "$string3" == "code = 255" ]
then
sudo -u nginx php /var/www/html/shell/global/full_import.php --import finance_backup
sudo -u nginx php /var/www/html/shell/global/full_import.php --import finance_deactivate_quotes
sudo -u nginx php /var/www/html/shell/full_import.php --import finance
((i++))
sleep 3s
echo it works
fi
string3="exit code = 0"
echo It works
done
Upvotes: 0
Views: 233
Reputation: 27225
You can use grep -o
to extract a part of a string. In cases in which you don't care about the extracted string but only about whether that string was found or not you can use grep -q
:
import() {
sudo -u nginx php /var/www/html/shell/global/full_import.php --import "$@"
}
exitCodeIs0() {
grep -qF 'exit code = 0' <<< "$*"
}
for i in {1..3}; do
echo "Importing..."
output1=$(import finance_backup)
output2=$(import finance_deactivate_quotes)
output3=$(import finance)
if exitCodeIs0 "$output1" && exitCodeIs0 "$output2" && exitCodeIs0 "$output3"; then
echo "Success."
break
fi
echo "Error."
done
This won't print the output of the nginx
commands. If you want to see the output then add printf %s\\n "$output1" "$output2" "$output3"
in front of the if
.
It would be strange if the exit code would only be returned in text form. Most likely you can (and should) use the real exit code rather than extracting it from the textual output:
import() {
sudo -u nginx php /var/www/html/shell/global/full_import.php --import "$@"
}
for i in {1..3}; do
echo "Importing..."
status=0
import finance_backup
(( status += $? ))
import finance_deactivate_quotes
(( status += $? ))
import finance
(( status += $? ))
if (( status == 0 )); then
echo "Success."
break
fi
echo "Error."
done
This will print the output of the nginx
commands. If you don't want to see the output then add >&-
at the end of the command, directly after the "$@"
.
Up until now we ran all three imports again even if only one of them failed. You said you wanted to do it that way but it seems odd to me. The commands seem to be independent so it would make sense to only run those commands again which failed:
import() {
sudo -u nginx php /var/www/html/shell/global/full_import.php --import "$@"
}
for arg in finance_backup finance_deactivate_quotes finance; do
for i in {1..3}; do
if import "$arg"; then
echo "Success."
break
fi
echo "Error."
done
done
Upvotes: 1