LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-23-2005, 11:19 AM   #1
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Rep: Reputation: 30
bash script error near 'else' ?


I've been working on my first script below:

#
# Script to mount and list everything on a USB drive. The drive is unmounted at the end
#

echo "Mounting USB drive"
mount /mnt/usb

# Did the previous command throw up anything?
if [$? -gt 0] then
# There was a problem check it out
if [$? -eq 32] then
# Drive is already mounted, just display it's contents and unmount
ls /mnt/usb -al
umount /mnt/usb

echo "Unmounting USB drive"
else
# Some other problem, assume nothing connected
echo "There are currently no compatible USB drives connected"
fi

exit 1
else
# Drive mounted without problem, display it's contents and unmount it
ls /mnt/usb -al
umount /mnt/usb

echo "Unmounting USB drive"
fi

exit 0


but when I run it I get the following error:

line 17: syntax error near unexpected token 'else'

As far as I can see everything is OK, does any know what the problem is?
 
Old 01-23-2005, 11:34 AM   #2
meblost
Member
 
Registered: May 2004
Location: At Keyboard
Distribution: Mandrake 10.0, SuSE 9.0
Posts: 114

Rep: Reputation: 15
Try putting the thens on their own line and adding a space between each bracket and what it encloses. ie [ $? -eq 32 ] instead of [$? -eq 32].
 
Old 01-23-2005, 11:45 AM   #3
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Original Poster
Rep: Reputation: 30
Thanks meblost, 100% correct. Those changes sorted it out perfectly. Just one more question, do you know of any way to suppress the OS output. When a drive isn't connected I get the bash line

mount: /dev/sdb1 is not a valid block device

before my line about no compatible drives connected. Ideally I would just like my output to be displayed.

Thanks for you help so far
 
Old 01-23-2005, 02:53 PM   #4
LasseW
Member
 
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108

Rep: Reputation: 15
There's another error in the script: the first $? is the return code from the mount command. The if statement will execute a test command and the second $? is the return code from that command, probably not what you intended. To test the same rc in both ifs, assign it to a variable immediatley after mount:

RC=$?

Then use the variable in both if statements.
 
Old 01-23-2005, 03:37 PM   #5
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
for redirection, try '(some command) > /dev/null' or '(same command) 2> /dev/null'; I'm guessing you want the second.
 
Old 01-23-2005, 03:41 PM   #6
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Original Poster
Rep: Reputation: 30
LasseW - I'd just discovered the error when I read your post. Thanks for the reply, I didn't realise that an if statement would affect the $? value.

jonaskoelker - I'm just compiling at the moment, but I'll give it a try if this thing ever finishes!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
run-parts gives error on bash script ChoKamir Programming 9 04-11-2017 07:18 AM
Bash Script and Loop error handling Kedelfor Programming 5 05-22-2005 02:22 PM
bash script error corbis_demon Linux - Newbie 7 09-27-2004 10:41 AM
bash script problem...or user error? Jalalabee Programming 2 09-10-2003 06:18 AM
BASH script error in RH 7.3 ..whaaaa??? Pauly Linux - Software 7 12-13-2002 09:47 AM

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

All times are GMT -5. The time now is 09:02 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