Updates:

You can now choose from a number of pre-installed themes

IF statement problem

Started by Keith, March 02, 2018, 09:02:06 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Keith

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:

[email protected]:~/.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

[email protected]:~/.bin$ if [ -d /media ]; then echo media present; fi
media present
[email protected]:~/.bin$

then remove USB stick:

[email protected]:~/.bin$ umount /dev/sdb1
[email protected]:~/.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...............

[email protected]:~/.bin$ if [ -d /media ]; then echo media present; fi
media present
[email protected]:~/.bin$ if [ ! -d /media ]; then echo media present; fi
[email protected]:~/.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.

Mark Greaves (PCNetSpec)

#1
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 (http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html)




What you want is something more like:
if [ -d /media/$USER/* ]; then echo "media present"; fi
or:
if [ -d /media/$USER/* ]; then echo "media present"; else echo "media not present"; fi
WARNING: You are logged into reality as 'root'
logging in as 'insane' is the only safe option.
pcnetspec.co.uk


Keith

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

Keith

Ah, but try this:

[email protected]:~/.bin$ fileref="qw"
[email protected]:~/.bin$ echo $fileref
qw
[email protected]:~/.bin$ if [ $fileref="qw" ]; then echo "qw"; fi
qw
[email protected]:~/.bin$ if [ ! $fileref="qw" ]; then echo "qw"; fi
[email protected]:~/.bin$ (so far OK)
[email protected]:~/.bin$ if [ $fileref="QW" ]; then echo "QW"; fi
QW
[email protected]:~/.bin$ if [ ! $fileref="QW" ]; then echo "QW"; fi
[email protected]:~/.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?

Mark Greaves (PCNetSpec)

Add some spaces around the "=" .. as in
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


Keith

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.

Mark Greaves (PCNetSpec)

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