Regular expression matching

Hi,
I have a variable in my script that gets its value from a procstack output. It could be a number of any length, or it could just be a '1' with 0 or more white spaces around it. I would like to detect when this variable is just a 1 and not a 1234, for example. This is as far as I got:

#!/bin/ksh
var1="1"
if [[ ${var1} = ^1$ ]] then
echo $var1
else echo "Bad"
fi
exit

And the execution goes like this:

./borrame.sh
Bad

How can I get it to check that the value is just 1, with either spaces or not around it, but cernainly not digits?

Thanks in advance. I get the feelign this is quite easy-peasy but I can�t get my head around regular expressions yet.

T.

Your code only matches a single 1 when it is the first and last character on a line.

You need to add more regex than that!

You can use expr:

expr "$var" : '^ *1 *$' >/dev/null && echo OK

if x is the desired output, Add some number , say N in the output and try to check id the final output is x+N :slight_smile:

as
'1' + 1 = '2'
and is also
' 1 ' + 1 = '2'