Author Topic: IF statement problem  (Read 1086 times)

0 Members and 1 Guest are viewing this topic.

Offline Keith

  • Hero Member
  • *****
  • Posts: 861
  • Karma: 11
  • Gender: Male
  • Linux Novice
    • View Profile
    • Awards
IF statement problem
« on: March 02, 2018, 09:02:06 pm »
Well, I know this must be a trivial problem but it's got me stumped!

As part of a mini project I want to detect whether or not a USB stick is inserted.  Part of the script is as follows:
With USB stick inserted:

keith@T500:~/.bin$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev              966912        0    966912   0% /dev
tmpfs             197460     6484    190976   4% /run
……. lots of lines……..
mpfs             197460       68    197392   1% /run/user/1000
/dev/sdb1       30286416  4064832  26221584  14% /media/keith/AUDIO

keith@T500:~/.bin$ if [ -d /media ]; then echo media present; fi
media present
keith@T500:~/.bin$

then remove USB stick:

keith@T500:~/.bin$ umount /dev/sdb1
keith@T500:~/.bin$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev              966912        0    966912   0% /dev
tmpfs             197460     6484    190976   4% /run
……. lots of lines……..
mpfs             197460       68    197392   1% /run/user/1000
………..no /media shown…………...

keith@T500:~/.bin$ if [ -d /media ]; then echo media present; fi
media present
keith@T500:~/.bin$ if [ ! -d /media ]; then echo media present; fi
keith@T500:~/.bin$

The IF statement appears to be working the wrong way round.  What am I doing incorrectly?  It's driving me mad!
Advice would be welcome.

Online Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 17934
  • Karma: 461
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: IF statement problem
« Reply #1 on: March 02, 2018, 11:12:48 pm »
What that says is - if /media exists and is a directory (-d), then echo "media present" to standard out, then finish.

The problem is /media ALWAYS exists, and is ALWAYS a directory.

See here for what "if" options mean
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html



What you want is something more like:
Code: [Select]
if [ -d /media/$USER/* ]; then echo "media present"; fi
or:
Code: [Select]
if [ -d /media/$USER/* ]; then echo "media present"; else echo "media not present"; fi
« Last Edit: March 03, 2018, 11:55:10 am by Mark Greaves (PCNetSpec) »
WARNING: You are logged into reality as 'root'
logging in as 'insane' is the only safe option.
pcnetspec.co.uk

Offline Keith

  • Hero Member
  • *****
  • Posts: 861
  • Karma: 11
  • Gender: Male
  • Linux Novice
    • View Profile
    • Awards
Re: IF statement problem
« Reply #2 on: March 03, 2018, 11:30:18 am »
Oh - of course!!!  In fact I have used that trick in another context but didn't realise I had cracked it. 
Thank you for your explanation about  /media being always there - obvious now you mention it, but then I always was a bit slow. 

Thank you, Mark, as always. 
  Keith

Offline Keith

  • Hero Member
  • *****
  • Posts: 861
  • Karma: 11
  • Gender: Male
  • Linux Novice
    • View Profile
    • Awards
Re: IF statement problem
« Reply #3 on: March 03, 2018, 01:39:32 pm »
Ah, but try this:

keith@T500:~/.bin$ fileref="qw"
keith@T500:~/.bin$ echo $fileref
qw
keith@T500:~/.bin$ if [ $fileref="qw" ]; then echo "qw"; fi
qw
keith@T500:~/.bin$ if [ ! $fileref="qw" ]; then echo "qw"; fi
keith@T500:~/.bin$ (so far OK)
keith@T500:~/.bin$ if [ $fileref="QW" ]; then echo "QW"; fi
QW
keith@T500:~/.bin$ if [ ! $fileref="QW" ]; then echo "QW"; fi
keith@T500:~/.bin$


The last two statements give wrong answers as $fileref="qw" as shown earlier. 
A slightly different problem, I grant you, but relevant.  Can you help me with this one?

Online Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 17934
  • Karma: 461
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: IF statement problem
« Reply #4 on: March 03, 2018, 02:55:54 pm »
Add some spaces around the "=" .. as in
Code: [Select]
fileref="qw"
if [ $fileref = "qw" ]; then echo "qw"; fi
if [ $fileref = "QW" ]; then echo "QW"; fi
WARNING: You are logged into reality as 'root'
logging in as 'insane' is the only safe option.
pcnetspec.co.uk

Offline Keith

  • Hero Member
  • *****
  • Posts: 861
  • Karma: 11
  • Gender: Male
  • Linux Novice
    • View Profile
    • Awards
Re: IF statement problem
« Reply #5 on: March 03, 2018, 03:07:11 pm »
It must be really nice to know what you are doing! 
I thought I had tried every combination of spaces and non-spaces, which requirement changes from command to command.

Thank you Mark.

Online Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 17934
  • Karma: 461
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: IF statement problem
« Reply #6 on: March 03, 2018, 03:43:03 pm »
I never claimed to know what I'm doing .. seriously, that'd be a HUGE mistake ;)
WARNING: You are logged into reality as 'root'
logging in as 'insane' is the only safe option.
pcnetspec.co.uk

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal