LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to compare a variable with a number? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-compare-a-variable-with-a-number-648392/)

pdklinux79 06-10-2008 06:41 PM

how to compare a variable with a number?
 
the code is as below:


if [ $i -le $tnum ] ;
then
if [ $t -ne 0 ];
then
{array[$tnum]}=$t
tnum=$((tnum+1))
fi
else
{array[$tnum]}=$t
tnum=$((tnum+1))
fi



bash :
error :./omrprac1.sh: line 14: [: 0: unary operator expected

./omrprac1.sh: line 14: [: 0: unary operator expected

./omrprac1.sh: line 14: [: 0: unary operator expected

./omrprac1.sh: line 14: [: 0: unary operator expected



how can i compare a a variable t with number zero?

chrism01 06-10-2008 06:50 PM

Tricky, there's only 11 lines there .... ;)

Try it this way (inc indentations; use code tags)
Code:

#!/bin/bash
# this next line turns on comprehensive debugging
set -xv

# need to init vars
i=1
tnum=2

# Note: do not use ';' if the 'then' statement is on a separate line
if [[ $i -le $tnum ]]
then
    if [[ $t -ne 0 ]]
    then
        {array[$tnum]}=$t
        tnum=$((tnum+1))
    fi
else
    {array[$tnum]}=$t
    tnum=$((tnum+1))
fi

http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

HTH

pdklinux79 06-10-2008 07:02 PM

Hey The array is not set the the value i want:
 
the below is the code:
#!/bin/bash

i=1
tnum=1

while read line
do
t=$(echo "$line" | awk '{print $3}')
if [ $i -ne $tnum ]
then
if [ $t -ne 0 ]
then
{array[$tnum]}=$t
echo ${array[$tnum]} #not showing the value
tnum=$((tnum+1))
fi
else
{array[$tnum]}=$t
echo ${array[$tnum]} #not showing the value
tnum=$((tnum+1))
fi
echo ${array1[$tnum]} #not showing the v


done < <(grep '^ID' /home/admin/cdat.txt)


please let me know why the array is not echoed when i run the program.. it is not taking at all...


All times are GMT -5. The time now is 07:44 AM.