Not able to assign 0 (zero) to the existing variable

Hi, I have a variable which read the value from the lines, and if it is empty string (5 bytes), then I have to assign this variable to zero "0", it able to detect the data is empty by showing me the message "empty", but not able to assign it as zero.

WTHP=`echo "$file" | cut -c158-162`
 
if [[ $WTHP = " " ]]
then
echo "empty" >> $Log
$WTHP = 0
echo "$WTHP=" $WTHP >> $Log
fi

it will show me error message "not found".
I have tried to code it as below:
1) WTHP = 0
OR
2) WTHP = [0]
OR
3) WTHP = "0"
OR
4) WTHP_1 = 0, then echo this variable, but still will failed to assign zero to this variable still will show me same error message.

Please advise.

Hi, a test for an empty string is "" , not " " :

if [[ $WTHP == "" ]]

The correct syntax with double brackets is 2 equal signs.

With an assignment there can be no spaces:

WTHP=0

I am used to check for zero length and the old style [

if [ -z "$WTHP" ]

Spaces required! (Arguments for the [ command.)
But assignments must be just = and no spaces!
(Otherwise the "variable" becomes a command.)

Hi newbie2100...

If you are not sure try experimenting longhand first, it aids me in understanding what is going on inside the shell...
(Note:- I usually try to stay as basic as possible until I do understand.)

Hope this helps:-

Last login: Sun Sep  1 09:32:06 on ttys000
AMIGA:barrywalker~> # A null character...
AMIGA:barrywalker~> WHTP=""
AMIGA:barrywalker~> echo "$WHTP"

AMIGA:barrywalker~> # A character zero...
AMIGA:barrywalker~> WHTP="0"
AMIGA:barrywalker~> echo "$WHTP"
0
AMIGA:barrywalker~> # This is an error with or without spaces...
AMIGA:barrywalker~> $WHTP=0
-bash: 0=0: command not found
AMIGA:barrywalker~> # It should be...
AMIGA:barrywalker~> WHTP=0
AMIGA:barrywalker~> echo "$WHTP"
0
AMIGA:barrywalker~> # Hope this helps...

Scrutinizer, MadeInGermany, and wisecracker touched on some problems in your script, but there are other problems here.

WTHP=`echo "$file" | cut -c158-162`

does not return the 158th through the 162nd byte from the 1st line in the file named by the expansion of the file shell variable; it returns that range of characters (not bytes) in the name (not the contents) of the file.

From your description it is not at all clear what you want to do with the above line. If you want the 158th through the 162nd bytes from the contents of the file, that would be something like:

WTHP=$(dd if="$file" bs=1 skip=157 count=5)

If you want the 158th through the 162nd bytes from the 1st line of the contents of the file, that would be something like:

WTHP=$(head -n 1 "$file"|cut -b158-162)

If you want the 158th through the 162nd bytes from every line of the contents of the file, that would be something like:

WTHP=$(cut -b158-162 "$file")

It isn't clear what you're trying to check with:

if [[ $WTHP = " " ]]

but it isn't likely to be what you want. You talk about 5 bytes being an empty string, but by definition an empty string only contains a single string terminating null character. If you want to verify that $WTHP expands to 5 space characters, that should be:

if [ "$WTHP" = "     " ]
      or
if [[ "$WTHP" == "     " ]]

And, the code:

WTHP=0
echo "$WTHP=" $WTHP >> $Log

will append the line:

0= 0

to the file named by the expansion of $Log . If you wanted to append:

$WTHP=0

to your log file, that would be something like:

echo '$'"WTHP=$WTHP" >> "$Log"
1 Like

Thanks all your help!! I got the fixed. :slight_smile: