Shell script - check value is an integer (or any number?)

OK - stackoverflow has numerous multiline answers to my conundrum, none of which seem to work in my use case…

I’ve got a shell script - it verifies that it’s called with 2 arguments… It then verifies the $2 argument is a number between one, and three :

if [ $SIZIO -lt 1 ] && [ $SIZIO -gt 3 ] ; then
        echo "1 = 5%, 2 = 7%, 3 = 10% - THERE ARE NO OTHER >>ACCEPTABLE<< VALUES MORON!"
        exit 1
fi

Tried this - but it doesn’t work :

# if ! [[ $1 =~ '^[0-9]+$' ]] ; then 
#       echo "That crap's not a number dipshit..." ; 
#       exit 1
# fi

Ideally - I’d like to make my if statement an and then or, condition e.g. :

if [ $SIZIO -lt 1 ] && [ $SIZIO -gt 3 ] || [ ! $SIZIO >>>integer<<< ]; then
        echo "1 = 5%, 2 = 7%, 3 = 10% - THERE ARE NO OTHER >>ACCEPTABLE<< VALUES MORON!"
        exit 1
fi

i.e. three conditions to NOT exit - the last (or - with “||”) to test if the value’s an integer…

What the script actually does is it takes an image file, reduces it, then blows it up, using “scale” not “resize” in imagemagick, “scale” does a better job at pixelating bitmaps, “resize” is smart - but it tries various stuff for LESS pixelization! I want some pixelization - so that it looks like something from a 256 colour 320x240 MS-DOS game from 1991 or whatever - think Prince of Persia or Commander Keen…

I use a case statement :

case $SIZIO in
	1)
		VAL="5"
	;;
	2)
		# because sometimes 5% is too much, but 10% is not enough
		VAL="7"
	;;
	3)
		VAL="10"
	;;
	# anything else situation probably redundant
	*)
		echo "not a number ya gitmoron!"
		exit 1
	;;
esac

to verify and set the “scale” $VAL, this kinda sorta works, but the shell script barfs completely, if I type e.g. “a”, the script crashes out ungracefully :

This is not about obscuring stuff or whatever (i.e. pixelating people’s faces from photos), it’s about creating Pixel Art (which I happen to be a fan of - used to spend hours and hours in DeluxePaint). Been creating stuff in InkScape, plans for a webcomic thingie, I’ve been mulling over…

Here’s what happens when I try some other value that’s not a number, or is even a number outside the range of <0 or >3 :

╭─x@titan ~/Pictures/working/FarmyardFreaks  
╰─➤  pigzel8 karenkkunny.png g
/home/x/bin/pigzel8: line 56: syntax error: unexpected end of file

– – – edit – – –
there was originally a typo in my shell script (didn’t comment out ALL of one of my if statements - that’s fixed sort/kinda) :

That case statement with “*)” should cater for this scenario, but it’s not… I’m not too bothered, even drunk (it is after beer o’clock here in Perth West Oz, after all) I should remember to use only 1, 2 or 3… But I’d like it to be completely idiot and moron proof (even though I probably won’t share this anywhere).

	*)
		echo "not a number ya gitmoron!"
		exit 1

– – edit 2 – – –
and that reminds me - it’s f–king ages since I last committed my shell scripts to my private git repo! And it’s kinda/sorta broken - but not a job to fix, or contemplate after some topshelf XXXX beverage consumption…

1 Like

I guess I could do 1 or 2 or 3, and exit otherwise… but that’s inelegant IMHO…

if [ ! $SIZIO -eq 1 ] || [ ! $SIZIO -ne 2 ] || [ ! $SIZIO -ne 3] ; then
    do some shit
fi

and damnit I’m too drunk to figure out if that’s even logical… So it’s good night from him, and him :

Try
if ! [[ $1 =~ [1]+$ ]]


  1. 0-9 ↩︎

If an apple is less than 1 AND the same apple is greater than 3?

As @nevj pointed out, if you put it in single quotes, it is taking literally. So, with Regex you need to keep it free.
This is one of the vast amount of confusing things when using a Bourne derivative…

It exits, when you provide it any other value than 1, 2 or 3. That’s what your case statement is saying and you have probably written it yourself.

╭─x@titan ~/Pictures/working/FarmyardFreaks  
╰─➤  pigzel8 karenkkunny.png g
/home/x/bin/pigzel8: line 56: syntax error: unexpected end of file

If I enter an integer that’s not between 1 and 3, it exits “gracefully”… but if I put something else in there - e.g. “g” - it exits in an ugly fashion “syntax error: unexpected end of file”… What I’d really like is to just exit “gracefully” if the argument isn’t an integer…

	*)
		echo "not a number ya gitmoron!"
		exit 1

this doesn’t work if it’s not an integer (haven’t tried something as silly as a floating point number…

It’s not a huge issue - I can life, and work, with my solution, but it’s untidy… I’d have expected “*” to mean"anything" not taken care of in the first three cases, but it’s not working like that…
– edit –
just thought about it some more - I probably don’t need the if to check if it’s between 1 and 3… Just the case statement should work :smiley:

I might abstract it further… break it up into functions / procedures (I know the difference between a function and a procedure in Pascal, but not in a sh/csh/ksh/bash/zsh script) and remove the test between values logic…

if [ $XXXX -lt 1 ]  || [ $XXXX -gt 3 ] ; 
    then echo "1 = 5%, 2 = 7%, 3 = 10% - THERE ARE NO 
                   OTHER>>ACCEPTABLE<< VALUES " 
  exit 1 
fi
1 Like