LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Small problem with bash and forward slash (https://www.linuxquestions.org/questions/linux-newbie-8/small-problem-with-bash-and-forward-slash-826109/)

Karsh 08-13-2010 07:04 PM

Small problem with bash and forward slash
 
Hi,

I have a script that grabs a numerical string from a text file that sometimes can be several slashes which in that case should have a certain action.

So:

tm=2

if [ tm="/" ]
then res=0
else
res=1
fi

This always gives me res = 0. What do I have to do so it gives me res = 1? I tried several ways in the IF like escaping the forward slash but that condition is always true.

Thanks.

Meson 08-13-2010 07:19 PM

You need some spaces around that equals sign. And in general you should use [[ ]].

Code:

tm=2

if [[ $tm == "/" ]]; then

    res=0

else

    res=1

fi

The = vs == doesn't really make a difference here, but I think it reads easier.

Karsh 08-13-2010 07:34 PM

Thanks a lot!

Karsh 08-13-2010 07:46 PM

I have another related question if you don't mind. :p

So:

Code:

nr=1
tm=2

if [[ nr != "1" || tm == "/" ]]; then
  res=0
else
  res=1
fi

Should give res = 1 right? I get res = 0. :confused:

Meson 08-13-2010 07:48 PM

Oops! I had a typo in my previous post. I've emitted that post but here it is again:

Code:

tm=2

if [[ $tm == "/" ]]; then

    res=0

else

    res=1

fi

You need a $ when referencing a variable.

Karsh 08-13-2010 07:55 PM

Everything working fine now. Thanks!


All times are GMT -5. The time now is 02:04 PM.