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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-23-2005, 12:19 PM
|
#1
|
Member
Registered: Jun 2004
Distribution: Slack
Posts: 214
Rep:
|
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?
|
|
|
01-23-2005, 12:34 PM
|
#2
|
Member
Registered: May 2004
Location: At Keyboard
Distribution: Mandrake 10.0, SuSE 9.0
Posts: 114
Rep:
|
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].
|
|
|
01-23-2005, 12:45 PM
|
#3
|
Member
Registered: Jun 2004
Distribution: Slack
Posts: 214
Original Poster
Rep:
|
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
|
|
|
01-23-2005, 03:53 PM
|
#4
|
Member
Registered: Oct 2004
Distribution: Fedora 7, OpenSuse 10.2
Posts: 108
Rep:
|
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.
|
|
|
01-23-2005, 04:37 PM
|
#5
|
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524
Rep:
|
for redirection, try '(some command) > /dev/null' or '(same command) 2> /dev/null'; I'm guessing you want the second.
|
|
|
01-23-2005, 04:41 PM
|
#6
|
Member
Registered: Jun 2004
Distribution: Slack
Posts: 214
Original Poster
Rep:
|
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!
|
|
|
All times are GMT -5. The time now is 01:16 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|