Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-27-2012
Registered User
 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Script with variable and condition

Hello

newbies question...

I just need a script able to launch a command when a condition is matched :

Code:
#!/bin/ksh

SIZ = 'cat /nurp/control.lst|wc -l' 
if test "$SIZ" -gt 0
then
echo 1
else 
echo 2
fi

but I receive errors messages

Code:
./t2[5]: SIZ:  not found
2

whats wrong ?
thanks for help

Last edited by Franklin52; 12-28-2012 at 08:09 AM.. Reason: Please use code tags for data end code samples
Sponsored Links
    #2  
Old 12-27-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,485
Thanks: 56
Thanked 425 Times in 421 Posts
try sth like this..


Code:
SIZ=$(cat /nurp/control.lst|wc -l)
if [[ "$SIZ" -gt 0 ]]
then
echo 1
else
echo 2
fi

Sponsored Links
    #3  
Old 12-27-2012
Jotne's Avatar
Registered User
 
Join Date: Dec 2010
Posts: 521
Thanks: 45
Thanked 98 Times in 91 Posts
A more simple version

Code:
SIZ=$(cat /nurp/control.lst|wc -l)
[[ "$SIZ" -gt 0 ]] && echo 1 || echo 2

    #4  
Old 12-27-2012
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,268
Thanks: 150
Thanked 721 Times in 694 Posts
No need to use cat and variable SIZ:

Code:
[[ $( wc -l < infile ) -gt 0 ]] && echo 1 || echo 2

Sponsored Links
    #5  
Old 12-27-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Location: Aachen, Germany
Posts: 1,881
Thanks: 25
Thanked 434 Times in 420 Posts
Pls use code tags as advised.
All proposals above are fine and work, but I think your ./t2[5]: SIZ: not found error comes from the two spaces around the equals-sign when defining SIZ. Remove them, test, and come back with result.
Sponsored Links
    #6  
Old 12-27-2012
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,268
Thanks: 150
Thanked 721 Times in 694 Posts
What RudiC mentioned is indeed an issue.

On top of that I see you are using single quote around the command:

Code:
'cat /nurp/control.lst|wc -l'

Replace that with:

Code:
SIZ=$( cat /nurp/control.lst|wc -l )

OR

Code:
SIZ=$( wc -l < /nurp/control.lst )

Sponsored Links
Reply

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
Compare the two variable with if condition aroragaurav.84 Shell Programming and Scripting 5 06-28-2012 10:38 AM
Problem using shell variable in awk if condition vishwas.s Shell Programming and Scripting 4 05-30-2012 05:19 PM
Bash: if condition as variable curlee2002 Shell Programming and Scripting 2 02-20-2010 01:24 PM
csh if loop variable condition check peroblem vasanth.vadalur Shell Programming and Scripting 0 11-06-2009 07:43 AM
if condition for variable existing in file eltinator Shell Programming and Scripting 3 02-15-2009 11:35 PM



All times are GMT -4. The time now is 06:33 AM.