Linux shell Script not null checking IF condition | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Linux shell Script not null checking IF condition

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-01-2008
kaushys kaushys is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 3 March 2009, 2:47 PM EST
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
CPU & Memory Linux shell Script not null checking IF condition

Hi,
I am parsing a file and getting a line number and reading it into a variable

cat abd.tx | grep -n "Stopped" | cut -d ":" -f1 | read linenum

Bascially that will give me the line num where the word "stopped" occured.

Now I want to find what is that line number or the word never occured. I am trying to write an IF condition after this

If the linenum has a value
then
echo process stopped
else
echo process has not stopped

but when I try to write the usuall syntax

if [ -n $linenum ]


Then in either case, that is, whether the word occured or not it still evaluates it as not a null value. Under no circumstance it takes it as a null value.

So I changed the if to

if [ $linenum -gt 0 ]

or

if [ $linenum -ne 0 ]


In both cases it says ARGUMENT expected. Either I don't know what it reads when the word doesnt occur or I dont know how to compare the string. Anything else but -n and -z gives me the argument error.
Sponsored Links
    #2  
Old 07-01-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 October 2014, 11:15 AM EDT
Location: NM
Posts: 10,247
Thanks: 281
Thanked 803 Times in 747 Posts
I don't get why you need a line number at all.

Code:
grep 'Stopped' abd.txt |\
while read whole_rec
do
      # play around with the whole record here
done

Sponsored Links
    #3  
Old 07-01-2008
kaushys kaushys is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 3 March 2009, 2:47 PM EST
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Firstly the word can occur has many times as possible. Also I am not interested in the line. I want the linenum. Is the word occured then all information after that line to the end of the file. If it didnt occur then then entire file.

Thanks for your quick response though

Last edited by kaushys; 07-01-2008 at 05:39 PM..
    #4  
Old 07-02-2008
spirtle spirtle is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 23 March 2010, 12:11 PM EDT
Location: Scotland
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
I think you want

Code:
if [ -z $linenum ]

to test whether nothing has been written to the variable.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
NULL checking Ananthdoss Shell Programming and Scripting 6 10-11-2011 10:06 AM
Checking an array for NULL in C++ mind@work Programming 3 08-02-2011 03:33 PM
Checking for null value ganapathi.t Shell Programming and Scripting 4 04-21-2009 09:37 AM
How to compare null and space using single if condition jayakumarrt UNIX for Dummies Questions & Answers 3 05-16-2008 05:13 AM
How can find Null value in If condition koti_rama Shell Programming and Scripting 2 07-17-2007 05:20 AM



All times are GMT -4. The time now is 03:57 AM.