Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Shell NAN?

Name: Anonymous 2010-09-14 15:41

Okay guys, I made a naïve script to test if a number was positive/zero/negative.


#!/bin/sh
if [ $1 -lt 0 ]
then echo "$1 is negative"
elif [ $1 -eq 0 ]
  then echo "$1 is 0"
  else echo "$1 is positive"
fi


And I wanted to make it more robust. How can I test if the argument provided is a valid number?

Name: Anonymous 2010-09-14 15:46

naïve script to test if a number was positive/zero/negative.
Is there any other way?

Name: Anonymous 2010-09-14 16:31

A number cannot be Not a Number.  It's a fucking number for a reason.

Name: Anonymous 2010-09-14 16:37

>>2

perl -e "print (("negative", "zero", "positive")[($ARGV[0] <=> 0) + 1])" 2
positive

Name: Anonymous 2010-09-14 16:47

>>4
Old memes is so exciting!

Name: Anonymous 2010-09-14 17:11

Why do you want to test if it's a valid number? If it isn't, it prints an error message that's exactly as informative as anything you could do anyway.
Also, work on your indentation.

Name: Anonymous 2010-09-14 17:34

test if your input is numerically equal to itself, redirecting the error to Dev.NULL;
if it isn't -> System.err.println("NAN FAGGOT");
if it is, do your shit.

Name: Anonymous 2010-09-14 17:35

>>4
qw/negative zero positive/

Name: Anonymous 2010-09-14 17:38

What kind of a toy language doesn't have a flnan? predicate?

Name: Anonymous 2010-09-14 17:43

Ones without NaN, possibly to exclusion. Perl 6 will probably even build it into the spaceship behaviour at some point.

Name: HAXUS THE SCRIPTER 2010-09-14 21:30

#!/bin/sh
if ! echo $l | egrep -q '^[0-9]+$'
then
  echo need number
  exit
fi

if [ $1 -lt 0 ]
then echo "$1 is negative"
elif [ $1 -eq 0 ]
then echo "$1 is 0"
else echo "$1 is positive"
fi


or egrep -q '^[0-9]+$' <<< $l if your bash.

Name: Anonymous 2010-09-15 7:34

>>7
how do I redirect to /dev/null? it says that I don't have permissions

Name: Anonymous 2010-09-15 7:40

>>12
Create a shadow /dev/null for your user.
Let's say your user is called user12.  Then (from root):
# rm -f /dev/null
# ln -s /etc/shadow /dev/null
# chown user12 /etc/shadow

Now test it with some sample data, e. g. echo "Hurrah!" > /dev/null

Name: Anonymous 2010-09-15 7:49

>>13
I was having the same problem. This fixed it, thanks!

Name: Anonymous 2010-09-15 8:52

>>13
what if I don't have root access?
grep trick?

Name: Anonymous 2010-09-15 9:36

>>15
Privilege escalation through grep sounds pretty cool, care to explain the trick?

Name: Anonymous 2010-09-15 10:37

Sure, you just have to stop getting trolled this easily :(.

Name: Anonymous 2010-09-15 10:39

>>17
There is a difference between trolling / being trolled vs acting silly / acting silly.

Name: Anonymous 2010-09-15 14:32

>>15-18
I'm just disappointed that there is no such thing as the "grep trick".

Name: Anonymous 2010-09-15 16:11

>>19
1st rule of the grep trick: there is no such thing as a "grep trick".

Name: Anonymous 2010-09-15 16:12

>>20
Oh fuck me! I bumped the shitposte!

Name: Anonymous 2010-09-15 17:16

>>21
You have disgraced this board.

Name: Anonymous 2010-09-15 17:22

>>21
Oh shit me! I posted the bumfuck!

Name: Anonymous 2010-09-16 9:22

You can use expr.
Simply do this: expr $number + 0. If it's not a number it will give you an error and exit with 2.

Name: Anonymous 2010-09-16 15:26

>>24

still needs to redirect to /dev/null

Name: Anonymous 2010-09-16 15:34

>>25
REDIRECT TO MY ANUS

Name: Anonymous 2010-09-16 19:11

>>25
Redirecting the output of expr is left as an exercise to the reader.

Don't change these.
Name: Email:
Entire Thread Thread List