Script with variable and condition | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Script with variable and condition

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-27-2012
vdurieu vdurieu is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 27 December 2012, 9:13 AM EST
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
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 14 April 2014, 6:10 AM EDT
Posts: 1,640
Thanks: 58
Thanked 476 Times in 472 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
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 8 April 2014, 11:00 AM EDT
Posts: 1,036
Thanks: 61
Thanked 216 Times in 204 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
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 16 April 2014, 6:03 PM EDT
Location: Galactic Empire
Posts: 3,279
Thanks: 227
Thanked 1,156 Times in 1,093 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
Last Activity: 16 April 2014, 3:52 PM EDT
Location: Aachen, Germany
Posts: 3,513
Thanks: 50
Thanked 818 Times in 781 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
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 16 April 2014, 6:03 PM EDT
Location: Galactic Empire
Posts: 3,279
Thanks: 227
Thanked 1,156 Times in 1,093 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
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
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 11:50 PM.