LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 09-24-2010, 12:59 AM   #1
x111
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 48

Rep: Reputation: 7
Shell script: condition always met ?


Hi everybody. I'm learning to write some shell scripts to work with the system. Now I'm on a script to mount a device by its full path.
Here is what I've done so far:
Code:
#! /bin/sh
BASEDIR="/mnt"
PARTITION=$1 # Get device path by 1st parameter

PTYPE=`blkid $PARTITION -o value -s TYPE 2>&1` #Get partition type
echo "Partition: $PARTITION, type: $PTYPE"
if [ $PTYPE ] ; then # Mountable partition
	MOUNTNAME=`echo $PARTITION | egrep -o [h,s]d.[0-9]+`
	MOUNTPOINT="$BASEDIR/$MOUNTNAME"
	MOUNTOPS=""
	if [ $PTYPE=="ntfs" ] ; then # If this is an ntfs, apply a special option
		MOUNTOPS="-t ntfs-3g"
	fi
	mkdir -p $MOUNTPOINT
	echo "Mount options for $PARTITION: $MOUNTOPS"
	mount $MOUNTOPS $PARTITION $MOUNTPOINT
	echo $MOUNTPOINT" mounted."

else # blkid returned nothing, usually this is a "container" partition and not mountable
	echo "Unknown type"
fi

exit 0
But when it comes to the type check, the condition on ntfs type is always met, which means that the mount options is always "-t ntfs-3g", regardless of what type the partition is.

Here the output of the command on an ext3 partition:
# mount-any /dev/hda8
Code:
Partition: /dev/hda8, type: ext3
Mount options for /dev/hda8: -t ntfs-3g
NTFS signature is missing.
Failed to mount '/dev/hda8': Invalid argument
The device '/dev/hda8' doesn't have a valid NTFS.
Maybe you selected the wrong device? Or the whole disk instead of a
partition (e.g. /dev/hda, not /dev/hda1)? Or the other way around?
/mnt/hda8 mounted.
I've tried replacing
Code:
if [ $PTYPE=="ntfs" ] ; then
with
Code:
if [ $PTYPE==ntfs ] ; then
But still no luck.
So anyone please tell me what I'm doing wrong and how to set this condition checking right.
Thanks for all your help and suggestion.
 
Old 09-24-2010, 01:15 AM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
You need spaces in the condition.
Try :
Code:
if [ "$PTYPE" == "ntfs" ] ; then
http://mywiki.wooledge.org/BashGuide..._and_.5B.5B.29
 
1 members found this post helpful.
Old 09-24-2010, 01:45 AM   #3
x111
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 48

Original Poster
Rep: Reputation: 7
Quote:
Originally Posted by smoker View Post
You need spaces in the condition.
Try :
Code:
if [ "$PTYPE" == "ntfs" ] ; then
http://mywiki.wooledge.org/BashGuide..._and_.5B.5B.29
Oh, thanks for your answer, smoker. It is quick and accurate. Now I have it working.

By the way, why there're "==" and "=", and "-eq" just for comparision. Do you think that bash syntax is not intuitive enough, at least for a beginner.
 
Old 09-24-2010, 01:53 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Basically (as you'll see by reading around)
Code:
-eq  is for comparing numbers
=    is for comparing strings (old school)
==   is for comparing strings (modern method)
either of the latter will work for strings.
The shell (ksh, bash) is more or less context sensitive, but I'd stick to '==' for clarity.
 
1 members found this post helpful.
Old 09-24-2010, 06:27 PM   #5
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
In addition to Chris's answer, there are different effects caused by the number of square brackets and double quotes around the condition. At least as far as the variable is concerned. See the TLDP advanced bash scripting guide.

Last edited by smoker; 09-24-2010 at 06:31 PM.
 
  


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
use case condition in the script dnaqvi Programming 11 05-21-2010 06:36 PM
Iptables script with for condition Ronin_tekorei Programming 2 04-28-2010 04:44 PM
IF condition in shell command line stepic Programming 3 04-23-2009 03:20 AM
while () not exiting even when exit condition is met aatwell Programming 28 09-15-2008 08:49 PM
If condition in script imsajjadali Red Hat 5 06-07-2004 07:52 AM


All times are GMT -5. The time now is 04:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration