Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Script with variable and condition

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 12-27-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 27 May 2015, 11:47 AM EDT
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 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 Unix and Linux 12-27-2012
Jotne's Unix or Linux Image
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
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 Unix and Linux 12-27-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 4 July 2015, 1:12 AM EDT
Location: Galactic Empire
Posts: 3,409
Thanks: 240
Thanked 1,217 Times in 1,143 Posts
No need to use cat and variable SIZ:

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

Sponsored Links
    #5  
Old Unix and Linux 12-27-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 3 July 2015, 9:53 AM EDT
Location: Aachen, Germany
Posts: 6,243
Thanks: 109
Thanked 1,701 Times in 1,593 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 Unix and Linux 12-27-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 4 July 2015, 1:12 AM EDT
Location: Galactic Empire
Posts: 3,409
Thanks: 240
Thanked 1,217 Times in 1,143 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 09:31 AM.