Yes, I can't believe I am writing this but it seems I can't compare one variable to a string, then second variable to another string and combine those in AND (&&) clause in IF statement for a bash script.
I am trying to do that on Linux Mint 20, GNU bash version 5.0.16. Inside the code you gonna see all the alternative statements I tried and none of those worked.
The script needs to ping my Internet connection twice and if in both cases there is 100% loss of packets I need to start a number of bash commands {not included here} to reconnect the connection. So it is a script for unattended reconnection of internet.
Here is the code:
#!/bin/bash
# redirect *pint output to a text file
ping -q -c 8 8.8.8.8 > ~/bash/pingOut.txt
# get transfer loss percentage
lossPerc=$(awk '$5=="received," {print $6}' ~/bash/pingOut.txt)
echo "lossPerc is ${lossPerc}"
# read prev loss percentage
prevLossPerc=$(cat ~/bash/loss-perc.txt)
echo "prevLossPerc is ${prevLossPerc}"
# write latest loss percentage to file
echo "$lossPerc" > ~/bash/loss-perc.txt
#if [[ "$lossPerc" == '+8' ]] && [[ "$prevLostPerc" == '+8' ]] => NO
#if [ "$lossPerc" == '+8' ] && [ "$prevLostPerc" == '+8' ] => NO
#if [ "$lossPerc" = '+8' ] && [ "$prevLostPerc" = '+8' ] => no error but not working as expected ------- line 19 -----
#if [[ $lossPerc = '+8' ]] && [[ $prevLostPerc = '+8' ]] => NO
#if [[ $lossPerc = "+8" ]] && [[ $prevLostPerc = "+8" ]] => NO
#if [ $lossPerc = '+8' ] && [ $prevLostPerc = '+8' ] => NO
#if [ $lossPerc = "+8" ] && [ $prevLostPerc = "+8" ] => NO
#if [[ "$lossPerc" = '+8' && "$prevLostPerc" = '+8' ]] => NO
#if [[ "$lossPerc" == "+8" && "$prevLostPerc" == "+8" ]] => NO
#if [ "$lossPerc" = '+8' && "$prevLostPerc" = '+8' ] NO
then
echo "Now I need to do a number of things to reconnect my i-net connection!"
fi
So basically I tried all the commented *if statements and all of those but one give errors in the shell. The errors are typically something like [: =: unexpected operator or [[: not found or [: missing. On line 19, there is a statement {marked with extra comment} which doesn't give error but when the condition is true it does nothing i.e. it is not the correct statement either.
Aucun commentaire:
Enregistrer un commentaire