Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C Shell Variable assignment 1

Status
Not open for further replies.

stu78

Programmer
May 29, 2002
121
GB
Hi,

I am new to the C Shell and am trying to assign variables. I am working on this line;

foo=`grep -i "does not" $log`

However am having problems, I have alsop tried

set foo=`grep -i "does not" $log`

However am getting a variable syntax problem, has anyone any ideas?


Stewart.
 
Is it
setenv foo=`grep -i "does not" $log`
or
setenv foo `grep -i "does not" $log`
???


Dickie Bird
db@dickiebird.freeserve.co.uk
 
#!/bin/sh
Hi Stewart

foo=`grep -i "does not" $log 2>/dev/null`

echo "$foo"

This should do the trick
HTH

Nogs
[ponder]
 
Stewart
I do apologise that should be;

foo=`grep -i does not $log 2>/dev/null`

Sorry for any confusion - I confused myself there somehow!!!
Nogs
[ponder]
 
Hi,
the title of the request is C Shell assignment so the /bin/sh examples aren't very helpful.

For CSH you use set....

your set example is fine....

set foo=`grep -i "does not" $log`

provided you are returning one word. Since the grep is for

"does not"

we can be pretty sure the grep will return at least 2 words

"does" and "not"

surrounded by whatever other words are on the line and on the next line with "does not" and the next line with "does not" and the .....

You must be carefull since GREP will return every line that contains "does not" $foo could be one VERY LONG String or a very long list of words. Here is why.....


Here is my $log file to use with this example.....

this command does not work
output
output
this command does not work
output
output
this command does not work


You can do one of 2 things depending on how you want to reference the result.....

1. Surround it by quotes.



set foo="`grep -i 'does not' $log`"

Because you are using a $log inside the expression you must use Double quotes outside and therefore the grep needs to change to use single quotes.

This will return one long string with ever line concatenated together.

echo $foo
this command does not work this command does not work this command does not work


2. You could use Parenthises.

set foo= ( `grep -i "does not" $log` )

This will return each word as a separate subscript of $foo

echo "$foo[0] $foo[1]"
this command

@ x = 0
while ($x < $#foo )
echo $foo[$x]
@ x ++
end

this
command
does
not
work
.
.
.


So what exactly are you trying to get out of this file and we can see if there is a way to do this effectively in
CSH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top