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 01-22-2009, 03:37 PM   #1
cat555
LQ Newbie
 
Registered: Nov 2008
Posts: 12

Rep: Reputation: 0
Smile Division by 0 error


Hi,
I am writing a script that among other things will be checking for various files on mount points. One of the conditions is that unless the server has failed over the df command will show root ( / ). If when checking the files the script comes across /, I want it to skip it, otherwise to complete the printf statement.
This portion of the script is

if ${MNTD} == "/" then
> /dev/null
else
printf '%12 ......
When running, if the script comes across a / I am receiving a 'Division by 0' error and it will abort the query.
Any suggestions would be greatly appreciated.
Thanks
 
Old 01-22-2009, 04:05 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I don't think you can do this:
.....then > /dev/null......

the "then" statement needs to be followed by a command.

the redirection operator (>) needs to be preceded by a statement (command) that produces an output.

Thus, I think your construct fails twice...

http://tldp.org Grab the "Bash Guide for Beginners"
 
Old 01-22-2009, 04:16 PM   #3
cat555
LQ Newbie
 
Registered: Nov 2008
Posts: 12

Original Poster
Rep: Reputation: 0
I was originally told to have it go like this -

if ${MNTD} == "/" then
null
else

printf '%12s %11s........

and it still comes up with the 'Division by 0' error.
It also didn't seem to matter if I used ' or " around the /.
Does it have something to do with root?
Are you not supposed to look at root like that?
heck I am reaching, sorry
 
Old 01-22-2009, 04:21 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Cat555 -

By all means, please look at the link Pixellany gave you ...
... and try something like this (cut/pasted from that link; notice the single equals sign, and the square brackets)(an old Bourne shell alias for "test"):
Code:
#!/bin/bash
  if [ "foo" = "foo" ]; then
    echo expression evaluated as true
  fi
'Hope that helps .. PSM

Last edited by paulsm4; 01-22-2009 at 04:22 PM.
 
Old 01-22-2009, 04:21 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The redirection is permitted, it simply equals the null command. My thought is that the division by 0 error does not come out of this portion of code. Despite the fact that the syntax is wrong in bash. It should be
Code:
if [ ${MNTD} == "/" ]
then
  > /dev/null
else
  echo hello
fi
However this construct does not make sense. First if you want to do nothing either do a null command or do true. In bash the null command is represented by a colon. Second if you want to do nothing if the condition is true, reverse the test and do something if the condition is not true, that is
Code:
if [ ${MNTD} != "/" ]
then
  echo hello
fi
Hope this helps (apart reading the fancy manual).
 
Old 01-22-2009, 04:59 PM   #6
cat555
LQ Newbie
 
Registered: Nov 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Hi again,
I'm sorry, I must have mis-represented what I was doing.
This problem is an added portion to a c shell script.
I had originally put it in as


set W='/'
if (${MNTD} == $W) then
null
else
printf '%12s %11s ........

I doesn't seem to matter what I do to it, I keep getting the 'Division by 0' error.
Without this portion in the script the report it mails me is fine unless I look for files in databases that have not failed over. I am really just trying to clean up the output which at this time reflects back to root


(KB) DAY TO DAY
SIZE USED AVAIL %USED MOUNTED DELTA
734003200 418876136 312665264 58% /oradata/abc/db01 418876136
5072896 1796088 3251216 36% /oradata/abc/db02 1796088
1888070656 643387648 1234959048 35% /oradata/abc/db03 643387648
.
.
.
4128448 1501620 2417116 39% / 1501620
4128448 1501620 2417116 39% / 1501620
4128448 1501620 2417116 39% / 1501620

the last mount points since they are not on the server revert to root

I would like it to ignore those files that are not on the server, but keep them in the report if they do failover.

Hopefully this makes sense.
 
Old 01-22-2009, 05:17 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
In C-shell you have to force both the right hand and the left hand sides of the expression to be strings, that is embed them in double quotes. Even the ${MNTD} - if equals to "/" - is interpreted as division if you don't put double quotes:
Code:
if ( "${MNTD}" == "$W" ) then
 
Old 01-22-2009, 05:46 PM   #8
cat555
LQ Newbie
 
Registered: Nov 2008
Posts: 12

Original Poster
Rep: Reputation: 0
SUCCESS!!!!!!!!!!

This is the code I ended up with. I couldn't get 'null' towork for whatever reason, so I went the opposite way.
Thanks for all your help (and I like the sites)
-----
set W='/'
if ( "${MNTD}" != "${W}" ) then
printf '%12s %11s . . . . . .
endif
end
-----

thanks again!
 
Old 01-22-2009, 05:58 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
FYI, the null command is a single colon either in sh, ksh, bash, tcsh, csh. When someone suggests to use a null command, he/she means ":", since null is not a keyword or built-in in any shell language. Consider this for example:
Code:
: > testfile
if testfile is existent, this command empties it. You can also issue the null command alone
Code:
:
and it simply does.... nothing!
 
  


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
bandwith division in squid jatender Linux - Server 1 10-16-2007 06:34 AM
file division using C fs11 Programming 46 08-17-2007 06:04 PM
C++ - Division by 0 and 'nan' Nightfox Programming 1 05-03-2006 12:58 PM
math division in javascript rblampain Programming 2 09-07-2005 05:07 AM
ip address class division emailssent Linux - Networking 3 10-08-2004 07:38 AM


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