BASH if elif not working

pizzipie

New Member
Joined
Sep 20, 2020
Messages
13
Reaction score
8
Credits
230
I am trying to distinguish between a gzipped file and a non-gzipped file and I can't get the if statements to work.

CODE: ==================================
echo -- Restoring $database --
echo

PS3='Pick a Database to Restore ... '

select bakup in $(ls -d "$folder"/* )
do
break;
done

echo "0..."
echo $bakup # picked file
var=${bakup:(-2)} # last two chars from picked file
echo "1... " $var

if [$var="gz"] # line 38 - gzipped lines show "gz"
then echo "gunzip" # then going to gunzip prior to mysql
elif [$var="ql"] # line 40 - sql files show "ql
then echo "mysql" # going direct to mysql
fi

echo "end"
exit

CODE END ===================================


OUTPUT 1. ==================================

Pick a Database to Restore ... 53
0...
/home/rick/Desktop/mydb-bakup-folder/2020Sep18-13:31:43_accounts.sql.gz
1... gz
./restoreBakup.sh: line 38: [gz=gz]: command not found
./restoreBakup.sh: line 40: [gz=ql]: command not found
end

OUTPUT 2. =====================================

If I change lines 38 & 40 so that $var is var I get:

Pick a Database to Restore ... 53
0...
/home/rick/Desktop/mydb-bakup-folder/2020Sep18-13:31:43_accounts.sql.gz
1... gz # shows for gzipped file
./restoreBakup.sh: line 38: [var=gz]: command not found
./restoreBakup.sh: line 40: [var=ql]: command not found
end
 


Not a BASH expert, but I think the problem is
Code:
[CODE]$var="gz"

I think you're using the wrong operator and making as assignment, not doing a comparison.

Try
Code:
$var=="gz"

'=' is for assignment
'==' is for comparison

keith
 
if $var="gz" and if $var=="gz" produces the same result.

O'Reilly Shell Scripting say to use = not == for comparison
 
Upon further research I think you may need to do

if "$var" == "gz"...

keith
 
I did a bunch of research and came up with the code below and it works just fine!
NOTE: string comparisons!! if [[ $ext_gz == "gz" ]] compared to what I started with.

Hope this helps someone.

R

CODE ================
echo "-- Start"
echo
PS3='Pick a Database to Restore ... '

select bakup in $(ls -d "$folder"/* )
do
break;
done

ext=$(echo $bakup | awk -F "." '{print $2, $3}') # $2 gets sql - $3 gets gz extension

IFS=" " read -ra ext_array <<< $ext #split $bakup into array of extensions

ext_sql=${ext_array[0]} # sql comparison string
ext_gz=${ext_array[1]} # gz comparison string

if [[ $ext_gz == "gz" ]]
then $(echo gunzip $bakup) # if gz extension then gunzip the file
var=${bakup:0:(-3)} # sql file to go to mysql
SOMEHOW THIS FORUM SITE PUT A FROWNY FACE HERE
elif [[ $ext_sql == "sql" ]]
then var=$bakup # sql file to go to mysql
else echo "Error - check backup file:"
fi

mysql -u$user -p$passwd < $var

echo "-- Finish"

CODE END =================
 
Just for your information, when you are going to include code fragments in your postings, click the three dots on the upper right and select 'code', then put your code fragment there. I think that might avoid things happening like the smiley face insert.......

keith
 
Code:
 I found the three dots

Thank you keith I didn't look long enough to find this

R
 

Members online


Latest posts

Top