views:

44

answers:

3

Hi, when I use bash to run the following code, it will assign the value 5 in to the var1.

var1=$(awk '$1>$3{ print "5"}' newfile2)
echo $var1

But when I use this same code in banana or something, it gives me error. Can some one please tell me if there is some other way I can write this code, so I can run it using the C or Korn shell as well.

+1  A: 

Use backticks and the set command for csh.

set var1=`awk '$1>$3{ print "5"}' newfile2`
echo $var1
idealmachine
Thanks. when I use this code, it did not assign any values for the var1.
+3  A: 

For C shell, use

set var=`....`

For bash/ksh

var1=$(awk '$1>$3{ print "5"}' newfile2)
ghostdog74
The question is, why would anyone want to write code for csh rather than bash? Clearly, csh's age combined with its annoying and limited ad-hoc parser is the reason shells like bash became popular, isn't it?
idealmachine
you shell tell that to OP. Put your comments in his question. AFAIK, his teacher said csh/ksh as well as bash.
ghostdog74
Thanks ghostdog for the new code. I try it with both bash and the other one. now it doesn't work with none of them. it simply doesn't assign any value to var1 nor compare the condition.
+1  A: 

Please note that there are no spaces before and after the variable name. so,

var1=`ls`

is what you need. But, if you have

var = `ls` 

you will get errors.

So, your code should be:

var1=`awk '$1>$3{ print "5"}' newfile2`
echo $var1

Make sure you are in BASH shell, not C or TCSH.

shivashis