Issue with Lists

Hey guys, so I wrote this simple script. The first time I typed it all out, I had the issue where whatever choice I entered, it would simply tell me it was a "bad selection" aka the else output.

I redid everything, and now no matter the choice, it does the backup option..

My brain hurts, and I'm sure it's something simple. If anyone can offer some insight it would be wonderful. Thanks!

DATETIME=�$(date +%d.%m.%Y_%H:%M:%S)�
BKFILE=~/Desktop/bash/backup/backup$DATETIME.tar.gz
BKFOLDER=~/Desktop/bash/code/
BKRESULT=~/Desktop/bash/backup/backup$DATETIME.txt
LIST=�Backup Quit�
select OPT in $LIST; do
if [ $OPT = "Backup" ]; then
tar -zcvf $BKFILE $BKFOLDER > $BKRESULT
clear
echo backup saved to:
echo $BKFILE
echo and the results writed to:
echo $BKRESULT
exit
elif [ $OPT = "Quit" ]; then
clear
echo Backup aborded by user
exit
else
clear
echo bad selection
exit
fi
done

At first I thought perhaps somebody had actually installed something that throws errors due to bad spelling :cool:

Ahh well ... check your equals ... there's more than 1 way to do equality checks ...

haha sorry, I'm doing this based on an exercise online. I copied and pasted the reference instead of my actual code. I corrected the spelling in mine :stuck_out_tongue:

and what do you mean check the equals?

---------- Post updated at 02:19 PM ---------- Previous update was at 02:15 PM ----------

lemme redirect at this:

I wrote my own little script to isolate the problem, and I'm finding the same issue

#!/bin/bash

LIST="One Two Three"

select OPT in $LIST; do
if [ $OPT="One" ]; then
       echo "You chose one!"
elif [ $OPT="Two" ]; then
       echo "You chose two!"
elif [ $OPT="Three" ]; then
       echo "You chose three!"
fi
exit
done

this is giving me the same issue of echoing "You chose one!" regardless of my choice.

=
^ check them. How do you use them in korn shell?

Edit:

sorry, just being hesitant on giving you the answer :slight_smile: Being kind of new here, not 100% sure how they spot homework questions here, and I don't want to break any rules :slight_smile: So - you're learning? Great - learn :wink: I'll give you hints ... lol

I suppose I was asking for clarification on what I'm checking for. and I don't know the answer to your question.

Just started playing around with Bash scripting.

---------- Post updated at 02:27 PM ---------- Previous update was at 02:22 PM ----------

oh, well I'm at work right now.. at my 9-5.. had some downtime and I'm trying to learn bash scripting to be more of an asset to my company.. would really appreciate help lol.

Definitely not homework.

if [ "$OPT" = 'One' ]; then

Do you do any java or c programming?
I believe the usage of using = for compares is similar in those languages.

thank you! I knew it would be something simple.

Really appreciated your help :slight_smile:

Huh, saw the quotes, didn't think that was the issue :slight_smile:
I thought the issue was = vs ==

the issue was spaces around '='.
The quotation is just the BestPractice.

1 Like

Oh right *facepalm* .. totally missed that :o
I saw the quotes, thought "well, that might help, but shouldn't matter"
I saw the =, and thought "Oh, that's not right, needs to be =="
and for some reason didn't register the spaces.

I don't pick up on those details as quick sometimes, as I'm just used to writing code like this:

if [[ "${VAR}" == "Value" ]]
then
  echo true
else
  echo false
fi

I believe the double [[ isn't really necessary either, is it? (question for different thread - no worries :slight_smile: )

The standard ways to perform this comparison are:

if [ "${VAR}" = 'Value' ]
then
  echo true
else
  echo false
fi

and:

if test "${VAR}" = 'Value'
then
  echo true
else
  echo false
fi

Single quotes or double quotes surrounding Value are good conventions, but not strictly necessary in this case. Some shells accept == , some shells accept [[ expr ]] , but portable shell scripts should use one of the above forms instead.

1 Like

Good to know - Thank-you!