LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 06: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, 06:50 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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 06:52 PM.
 
Old 06-10-2008, 07: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



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 08:09 AM
compare $php variable to indexed distinct mysql columns secretlydead Programming 1 02-18-2008 10:48 PM
Compare a variable with line. say_hi_ravi Programming 2 01-29-2008 03: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 10:57 AM
How do you compare variable in bash programming? chynna_v Programming 6 09-08-2004 02:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:15 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
Open Source Consulting | Domain Registration