grep in Cshell


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep in Cshell
# 1  
Old 12-02-2008
grep in Cshell

Hi Everyone,
I'm facing a problem using grep in Cshell.
This is what i'm trying to do:
grep "abc" somefile
VAR="$?"
echo $VAR

somefile contains:
abc
def
ghi

Now, should'nt my output be 0 (Zero)
I'm getting 1 (One)

Can you please help me out.


Thanks in advance Smilie
G1
# 2  
Old 12-03-2008
instead of:
Code:
VAR="$?"

try:
Code:
set VAR=$?

# 3  
Old 12-03-2008
Hi, jeevan_fimare.

In general, Bourne shell (sh, ksh, bash, etc) syntax cannot be used in csh. There are versions of csh, namely tcsh, that may be more lenient for some features, but basically the families are different. Here's an example showing syntax as well as the system variable status:
Code:
#!/usr/bin/env csh

# @(#) s1       Demonstrate assignment, system variable, csh.

echo
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version "=o" csh tcsh

echo

echo " 1. With system variable status:"
grep abc data1
echo " Status = $status"

echo
echo " 2. With correct syntax for assignment:"
grep abc data1
set var = $status
echo " Status = $var"

echo
echo " 3. With acceptable syntax for assignment:"
grep abc data1
set var=$status
echo " Status = $var"

echo
echo " 4. With variable ? - may depend on csh / tcsh version:"
grep abc data1
set var = $?
echo " Status = $var"

echo
echo " 5. With incorrect assignment, expect failure:"
grep abc data1
var="$?"
echo " Status = $var"

exit 0

Producing:
Code:
$  ./s1

(Versions displayed with local utility version)
SunOS 5.10
csh Aug 8 2006 (SunOS 5.10)
tcsh 6.12.00

 1. With system variable status:
abc
 Status = 0

 2. With correct syntax for assignment:
abc
 Status = 0

 3. With acceptable syntax for assignment:
abc
 Status = 0

 4. With variable ? - may depend on csh / tcsh version:
abc
Variable syntax

I suggest you look over man csh as well as the tutorial at Csh , the section on setting variables, for example is Csh

The shell csh is considered by many to be not well-suited for scripting (on the other hand, tcsh is often chosen for interactive use) ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cshell if clause indentation

I would like to know if indentation is relevant for Cshell scripts. I wrote my code like this: if ((-e file1) && (-e file2)) then cat file1 > file10 cat file2 > file20 endifUsually I write my if clauses like this: if ((-e file1) && (-e file2)) then cat file1 > file10 ... (1 Reply)
Discussion started by: maya3
1 Replies

2. Shell Programming and Scripting

Cshell setenv: Too many arguments.

i have in c-shell set value_str ="one three" set line_seprator = "," set value_and_sperator = "$value_str$line_seprator" setenv STRING_CONCAT $STRING_CONCAT$value_and_sperator and im getting error: setenv: Too many arguments. this is... (1 Reply)
Discussion started by: umen
1 Replies

3. Shell Programming and Scripting

Cshell if statement not working

Hi .I am trying to check the first arguments =-s and the third =-d,but it doesnt work ,any idea why It gives me if: Missing file name Thanks #case -s and files if( $1 == "-s" && $3 != "-d" ) then echo "case s" endif (1 Reply)
Discussion started by: lio123
1 Replies

4. Shell Programming and Scripting

cshell script problem

Hi ,I have this simple code,why is not working thanks #!/bin/csh set res=find ./ -name "*.bash" | wc -l $res > result.txt (16 Replies)
Discussion started by: lio123
16 Replies

5. Shell Programming and Scripting

Script cShell - help!!

Hello everyone! i'm new in this forum and I'm here because I have a huge problem with a csh script. Here the problem: I have to write a script that check the system status, more precisely I have to check if there are processes with TIME > 3 hours and if such processes exists I must send a mail... (3 Replies)
Discussion started by: TheBeefEater
3 Replies

6. Shell Programming and Scripting

CShell if statement

If I want to compare two string variables in csh how do I correctly implement it. For example I'm checking if on cmdln the $1 == -r do something. if($1 == -r) then code.... However when I run it I just get an error message "if: Missing file name". Any suggestions? (1 Reply)
Discussion started by: ROFL
1 Replies

7. Shell Programming and Scripting

CShell Syntax Problem

Hi guys, Basically I'm trying to write a CShell script that calls an awk script on a given directory (given in command-line). I keep getting a syntax error with my code though: #!/bin/csh set dir = $ARGV foreach file ( $dir/* ) set output = 'awk -f /Desktop/aal $file' echo... (3 Replies)
Discussion started by: ROFL
3 Replies

8. UNIX for Dummies Questions & Answers

Regarding Decimals in Cshell

Hello... I am new to unix and I am wondering if in a C-shell script , Are we supposed to use only whole numbers........ for example..if a program needs to calculate the average of some numbers........ @ avg = (($1 +$2 + $3)/3)) is returning a whole number.........How can a decimal be achieved... (1 Reply)
Discussion started by: ravindra22
1 Replies

9. Shell Programming and Scripting

GUI for cshell scripts

Hi, I want to know whether it is possible to use GUI for cshell scripts? Thanks Sarbjit (0 Replies)
Discussion started by: sarbjit
0 Replies

10. Shell Programming and Scripting

Ignoring several lines at once in cshell

Hi We use # sign to ignore any line (i.e. comment ). But is it possible to ignore group of line at once or i have to use # in front of each line. Thanks Sarbjit (3 Replies)
Discussion started by: sarbjit
3 Replies
Login or Register to Ask a Question