You are not logged in.

#1 2010-02-23 23:52:41

DeeCodeUh
Member
From: Michigan, USA
Registered: 2007-11-27
Posts: 176

A little bash scripting help..

Hello, this is a script I made in /etc/rc.d/sd to mount my SD card as my home partition in my eee pc.
Here's the script:
http://pastebin.ca/1807492


The script works, but I keep getting this error.

[root@UpEarly coda]# /etc/rc.d/sd start
:: Mounting SD card...                                                   [BUSY] Waiting for SD card to exist...
Card exists!
/etc/rc.d/sd: line 20: [: too many arguments
Mounted at /home/coda
                                                                         [DONE]

What's wrong with the if statement at line 20?

Last edited by DeeCodeUh (2010-02-24 00:00:54)

Offline

#2 2010-02-24 00:32:34

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: A little bash scripting help..

Does the following work?

if [ "$(df | grep $MNTPNT)"=="" ];

My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#3 2010-02-24 00:35:38

DeeCodeUh
Member
From: Michigan, USA
Registered: 2007-11-27
Posts: 176

Re: A little bash scripting help..

I talked to the #bash channel on irc, and they said that this was best:

if cat /proc/mounts | grep -q $MNTPNT;

They also made it clear that square brackets are not part of an if statement.
Way to go for online tutorials teaching me otherwise.

Last edited by DeeCodeUh (2010-02-24 00:37:32)

Offline

#4 2010-02-24 00:54:48

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: A little bash scripting help..

Every tutorial I've seen goes out of its way to say that [ is not part of the syntax. Did you get linked to it from ubuntu forums or something? >.>

Offline

#5 2010-02-24 00:55:09

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: A little bash scripting help..

Xyne wrote:

Does the following work?

if [ "$(df | grep $MNTPNT)"=="" ];

Hmmm - I do believe there should be only _one_ equal sign!!
I make the same mistake myself - all the time ;-)

Other than that - there are more ways than one to skin a cat ...
just to give yall something else to chew on -

to check if the device is present:

dev="`blkid | grep $UUID | cut -d ':' -f 1`"
test "@$dev" = @ && die "-- device not present"  # testing for an empty string can be tricky - a 'space' will not match! ...

check if actually mounted before umounting ...

 df -a | grep  -q "$MNTPOINT$" && umount $MNTPOINT

A common scripting error is to forget to double-quote the result of a command - that's when errors about 'unary operators' or 'too many arguments' come about ...
_Always_ quote the result of any command !!

Last edited by perbh (2010-02-24 01:24:28)

Offline

#6 2010-02-24 01:15:24

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: A little bash scripting help..

I just checked it... "==" is synonymous with "=". I was more focused on wrapping the call in quotation marks though.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#7 2010-02-24 01:26:51

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: A little bash scripting help..

Xyne wrote:

I just checked it... "==" is synonymous with "=". I was more focused on wrapping the call in quotation marks though.

ooops *blushing* - that must be something new ... i _know_ I have had errors in the past with double equal signs (c-habit!!)
Sorry ...

Other than that - I've always had problems with
if test .... and if [ ... ], so these days if I just want to check if true or false I use '&&' or '||'.
If I want to handle both cases, I do 'if test $? -eq 0; then ...; else ...; fi'

Last edited by perbh (2010-02-24 01:30:52)

Offline

#8 2010-02-24 01:42:49

DeeCodeUh
Member
From: Michigan, USA
Registered: 2007-11-27
Posts: 176

Re: A little bash scripting help..

Daenyth wrote:

Every tutorial I've seen goes out of its way to say that [ is not part of the syntax. Did you get linked to it from ubuntu forums or something? >.>

lol

Offline

#9 2010-02-24 04:56:09

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: A little bash scripting help..

perbh wrote:

Other than that - I've always had problems with
if test .... and if [ ... ], so these days if I just want to check if true or false I use '&&' or '||'.
If I want to handle both cases, I do 'if test $? -eq 0; then ...; else ...; fi'

Maybe this will help ... "test" and "[ ]" are synonymous really.

test
[
    Evaluate a conditional expression expr. Each operator and operand must be a separate argument. 
    When the [ form is used, the last argument to the command must be a ].

In other words... you only use test or [] in your "if" statement if your comparing one thing to another, and that will "evaluate" as being either true or false... BUT if your just checking to see if blah blah blah is true... then it's not necc to use those "test" or "[ ]".

Regarding this if statement:

if cat /proc/mounts | grep -q $MNTPNT;

Easiest way for me to remember when to use this is if you just run the command on the command line without the "if" and it returns "something", that means it is "true", if it runs and you get nothing back.. it's returning as "false".

Example: you can't very well compare/run "x = b" on the command line and have it return anything...so it has to be "evaluated"...that's where "test" or "[ ]" come into play.
But using "test" or "[ ]" to check if x does equal b, and if x does equal b it does returns true, and if x doesn't equal b it returns false.

Anyway...that's how I remember it wink

Last edited by crouse (2010-02-24 04:58:03)

Offline

Board footer

Powered by FluxBB