[wplug] shell script madness

Mike Griffin mike at dmrnetworks.com
Wed Jul 16 10:49:49 EDT 2003


I need to refer to you the Advanced Bash Scripting Guide. From there;

	An if can test any command, not just conditions enclosed within 
brackets.

	if cmp a b &> /dev/null  # Suppress output.
	then echo "Files a and b are identical."
	else echo "Files a and b differ."
	fi

	if grep -q Bash file
	then echo "File contains at least one occurrence of Bash."
	fi

	if COMMAND_WHOSE_EXIT_STATUS_IS_0_UNLESS_ERROR_OCCURRED
	then echo "Command succeeded."
	else echo "Command failed."
	fi



now for an example to show you how your test is failing to do what you 
think it should be doing.

[root at escalus root]# /bin/mount | grep hello
[root at escalus root]# echo $?
1
[root at escalus root]# /bin/mount | grep hello | wc -l
       0
[root at escalus root]# echo $?
0

As you can see, the command exits cleanly, returning an exit status of 
'0' to the testing statement.
you DO NOT need to pipe the command to `wc -l`  simple using 
`/bin/mount | grep PITT-NW02` would return the value you're looking for.


The difference between the two IF statements is that one is a test, the 
other is a comparison.
if [ `mount | grep PITT-NW02 | wc -l` ] is always going to return the 
value of `wc -l`, not an exit status, wether the mount exists or not. 
That's why I thought you wanted a comparison:
if [ `mount | grep PITT-NW02 | wc -l` != 0 ]; then echo "lala"; fi



Mike



On Wednesday, July 16, 2003, at 10:15  AM, Alexandros Papadopoulos 
wrote:

> On Wed, 16 Jul 2003, Mike Griffin wrote:
>
>> What is it that you're trying to do?
>> I think you're trying to see if files exist on that mount, correct?
>
> Just trying to know if something is mounted or not.
>
>> how about:
>>
>> if [ `mount | grep PITT-NW02 | wc -l` != 0 ]
>> then
>>    echo "Files exist on this mount"
>> fi
>
> This works indeed, but isn't this test statement supposed to be the 
> same
> with the ones I was using?
>
> What's the difference of:
> if [ `mount | grep lala | wc -l` ]
> and
> if [ `mount | grep lala | wc -l` != 0 ]
>
> ?
>
> I thought that if the test statement returns non-zero, the statement
> executes, therefore rendering comparisons with zero in tests
> completely redundant...
>
> Thanks for the help
>
> -A
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug
>




More information about the wplug mailing list