# if a flag = 1 digit, pad it....



## cgjoker (Aug 13, 2003)

Hi there.

I am trying to do a function in which if a flag has a value of only 1 digit, a 0 will be placed in front of it so that it will be two digits, such as, if flag=2, the result would be 02.

flag=02
#if($flag >= '0' && $flag <= '9')
if($flag -eq '[0-9]')
then
flag=0$flag
echo "not good $flag"
#flag=${flag}
#echo "good $flag"
else
flag=${flag}
echo "good $flag"
#flag=0$flag
#echo "not good $flag"
fi


----------



## Squashman (Apr 4, 2003)

I think you want to compare how many bytes or characters are in the variable. Based on that information you could then change the variables contents.

I know you don't use Bash but it may be the same. To get the string lenght in bash I do this.
stringlength=${#variablename}


----------



## AGCurry (Jun 15, 2005)

Most modern shells have printf, either as a builtin or standalone program.

x=$(printf "%02d" $x)

should work nicely.


----------



## Squashman (Apr 4, 2003)

That is pretty slick. Much better then my idea. I always forget all the things you can do with printf.


----------



## cgjoker (Aug 13, 2003)

That was slick and it works perfectly.

Thanks a tonne.
c.


----------



## cgjoker (Aug 13, 2003)

After further testing, I realized it doesn't quite work for instances where the flag is a two digit int. The flag can be 1 digit, or two. If it is 1, then place a 0 infront of it, if it is 2, then leave it as it is.

I get the following error:

printf: 3016-002 09 is not completely converted.
00


----------



## AGCurry (Jun 15, 2005)

joker, it should work for either case. Look at your data. It looks like your error is a result of bad data or incorrect parsing.

If I run:

printf "%02d" 3016-002

I get the error you listed.

You could prevent this by validating the flag, perhaps like this (and actually, this would work for the padding as well!):

case "$flag" in
[0-9]|[0-9][0-9]) flag=$(printf "%02d" $flag) ;;
*) echo "bad flag: $flag"
error_flag=Y ;;
esac


----------



## cgjoker (Aug 13, 2003)

when I statically put a value to flag in the same script, such as flag=02 case... it works. But the flag value is actually coming from a file. Here is how I am obtaining the value of flag.

flag=`grep 'flag' $INFOfile | sed -e 's/flag=//g'`

when I parse the INFOfile for the flag, I get that error. Otherwise, yes, it works fine.


----------



## cgjoker (Aug 13, 2003)

I think I got it working....

case "$flag" in
[0-9]) flag=$(printf "%02d" $flag) ;;
[0-9][0-9]) flag=$flag ;;
*) echo "bad flag: $flag"
error_flag=Y ;;
esac


----------



## AGCurry (Jun 15, 2005)

Have you looked at $INFOfile to make sure that there isn't any data existing on the line AFTER "flag=xx"?


----------



## cgjoker (Aug 13, 2003)

There is data on the next line, but no, there is no subsquent data on the same line.


----------



## Squashman (Apr 4, 2003)

So what exactly is in the INFOfile. Could you post those contents here.

You could just use the code I posted above to test the length of the string just to rule out there being any extra characters. seen or unseen.


----------



## AGCurry (Jun 15, 2005)

cgjoker said:


> when I statically put a value to flag in the same script, such as flag=02 case... it works. But the flag value is actually coming from a file. Here is how I am obtaining the value of flag.
> 
> flag=`grep 'flag' $INFOfile | sed -e 's/flag=//g'`
> 
> when I parse the INFOfile for the flag, I get that error. Otherwise, yes, it works fine.


Are there multiple instances of "flag=" in $INFOfile? If so, your assignment listed above could end up with multiple number tokens...


----------



## cgjoker (Aug 13, 2003)

There are multiple instances of flag...

DIRECTORY=my_directory
flag1=12
flag2=2
flag3=5


----------



## Squashman (Apr 4, 2003)

Agcurry hit on the head. Your flag variable gets assigned a whole bunch of tokens, your variable actually gets assigned this value
*flag1=12 flag2=2 flag3=5*

Which is why you get the error message. Your sed statement will never give you the format you want because flag is followed by another character before the equals sign.


----------



## cgjoker (Aug 13, 2003)

I see your point. This code here is working for me however... (because the flag1 distinguishes it from the other flag variables)

flag=`grep 'flag1' $INFOfile | sed -e 's/flag1=//g'`
case "$flag" in
[0-9]) flag=$(printf "%02d" $flag) ;;
[0-9][0-9]) flag=$flag ;;
*) echo "bad flag: $flag"
error_flag=Y ;;
esac


----------



## AGCurry (Jun 15, 2005)

Okay, but your results will be unpredictable, depending on what's in your data file...

I don't know what the overall purpose of your process is, but I would think that most times you'd want to put the case statement inside of a "while read" or "for flag in" loop.


----------



## cgjoker (Aug 13, 2003)

i suppose the for flag in 'numeric' loop wouldn't be such a bad idea. ill probably incorporate that as well.

thanks.


----------

