I am giving more details about my requirement
I got a file which has two columns, first column is of city and the second is of temperature
I search for the city and get the temperature value and dsplay based on that value
If temperature might be a space or number of space or non numeric or a negative value......I need to display what value
it is and change it to in all the above cases
if it is a positive value ( either decimal) then I need to used it for addition or subtraction
as per your above, it seems you need to give each value seperatly and it is exactly checking for that paticular value,
say suppose
" ") echo got a space;;
its only checking for one space, if i get more spaces its not considering
length=`echo $temp | wc -c`
if [ $length -le 0 ]
then
temp=0
fi
if [ $temp -lt 0 ]
then
echo "$city Negative temp"
temp=0
else
if [ $temp = 0 ]
then
echo "$city No temp" temp=0
fi
fi
Assuming that this is processing a file, then there is a high cost to calculating the length this way. Have you considered:-
length="${#temp}"
You should also consider a case statement to branch to appropriate code as if...then...else....if....fi....fi can be confusing to read through. If you only have three options, it would be neater as a start to have something like:-
for val in a b c a b a
do
if [ $val = a ]
then
echo val=a
elif [ $val = b ]
then
echo val=b
else
echo other
fi
done
Overall it would be better to write it as:
for val in a b c a b a
do
case "$val" in
a) echo val=a ;;
b) echo val=b ;;
*) echo other ;;
esac
done
Can you adapt this to fit your input and generate your output? You might need to slice up your variable and work on the first character, so if you are holding the the value val then the first character is "${val%${val#?}"
Slightly longer than a magical awk but perhaps more readable and/or flexible depending what you want to do next:-
while read label val
do
valtest="${val%${val##?}}" # Get first character of value
case "$valtest" in
"-") echo "$label Negative value" ;;
"#") echo "$label Invalid value" ;;
[0-9]) echo "$label $val" ;;
"") echo "$label No value" ;;
*) echo "$label Unexpected value" ;;
esac
done < file
I hope that this helps. I've tested in on HP-UX 11.11, RHEL 6.1 & AIX 4.3.3 to 6.1