LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-10-2008, 07:41 PM   #1
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Rep: Reputation: 15
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?
 
Old 06-10-2008, 07:50 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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

Last edited by chrism01; 06-10-2008 at 07:52 PM.
 
Old 06-10-2008, 08:02 PM   #3
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Original Poster
Rep: Reputation: 15
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compare floating number in a shell script? abefroman Programming 34 10-12-2016 09:09 AM
compare $php variable to indexed distinct mysql columns secretlydead Programming 1 02-18-2008 11:48 PM
Compare a variable with line. say_hi_ravi Programming 2 01-29-2008 04:03 AM
Why do I get a broken connection when I try to compare SID with a variable (PHP 4) vharishankar Programming 6 07-09-2005 11:57 AM
How do you compare variable in bash programming? chynna_v Programming 6 09-08-2004 03:17 AM


All times are GMT -5. The time now is 06:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration