LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2010, 07:04 PM   #1
Karsh
LQ Newbie
 
Registered: Dec 2003
Location: Portugal
Distribution: Debian
Posts: 5

Rep: Reputation: 0
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.
 
Old 08-13-2010, 07:19 PM   #2
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
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.

Last edited by Meson; 08-13-2010 at 07:47 PM.
 
Old 08-13-2010, 07:34 PM   #3
Karsh
LQ Newbie
 
Registered: Dec 2003
Location: Portugal
Distribution: Debian
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a lot!

Last edited by Karsh; 08-13-2010 at 07:35 PM.
 
Old 08-13-2010, 07:46 PM   #4
Karsh
LQ Newbie
 
Registered: Dec 2003
Location: Portugal
Distribution: Debian
Posts: 5

Original Poster
Rep: Reputation: 0
I have another related question if you don't mind.

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.

Last edited by Karsh; 08-13-2010 at 07:47 PM.
 
Old 08-13-2010, 07:48 PM   #5
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
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.
 
Old 08-13-2010, 07:55 PM   #6
Karsh
LQ Newbie
 
Registered: Dec 2003
Location: Portugal
Distribution: Debian
Posts: 5

Original Poster
Rep: Reputation: 0
Everything working fine now. Thanks!
 
  


Reply

Tags
slash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Forward slash (/) in filenames. Oxagast Linux - Software 10 07-21-2009 09:04 PM
No slash in the Bash | LFS-6.3 dr.acul Linux From Scratch 4 06-24-2008 07:56 AM
question mark and forward slash osc~ Slackware 2 08-16-2007 12:32 PM
Script to convert DOS backslash to UNIX forward slash pro_chandan Linux - Software 1 10-24-2006 10:09 AM
forward slash being treated as escape character in shell? debiant Linux - General 1 07-19-2006 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration