Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have the following relevant lines in a csh script:

#!/bin/csh -xv
set dvar = `date +"^%m\/%d\/%y"`
set filedate = `date +%b%d%Y`
set grepstring = "grep "\"$dvar\"" </home/user/log/logfile"
set outstring = ">> serverlog_$filedate.txt"
grep "$dvar" <logfile >serverlog_$filedate.txt
ssh SERVER1 \"\"$grepstring\"\" $outstring

The first grep on the local log file works great, but the SSH grep writes its output to the remote directory.

The line is coming out of the batch file as:

ssh SERVER1 ""grep "^01\/05\/10" <home/user/log/logfile"" >> serverlog_Jan052010.txt

When I type it in (with one less set of quotes of course) it works fine, I just can't get this to work from a csh script.

How would you get this command to output to the specified file on the local machine?

EDIT: Here is the final result that worked thanks to womble:

#!/bin/csh
set dvar = `date +"%m\/%d\/%y"`
set filedate = `date +%b%d%Y`

grep "^$dvar" logfile > serverlog_$filedate.txt
ssh SERVER1 grep "^$dvar" /home/user/log/logfile >> serverlog_$filedate.txt
share|improve this question
    
Switch to /bin/sh for scripting right now. See faqs.org/faqs/unix-faq/shell/csh-whynot –  reinierpost Jan 6 '10 at 21:04
add comment

2 Answers 2

up vote 5 down vote accepted

That script makes my eye bleed. Get rid of all of the variables storing fragments of the command (and the unnecessary input redirect on the grep), and see where that gets you. The quoting is almost certainly screwed up in ways you don't even want to imagine. In bourne shell, I'd write something like this:

today="$(date +"%m\/%d\/%y")"
filetoday="$(date +%b%d%Y)"

grep "^$today" logfile > serverlog_${filetoday}.txt
ssh SERVER1 grep "^$today" /home/user/log/logfile >> serverlog_${filetoday}.txt
share|improve this answer
    
I believe you about the quoting, it took me four days to get it to work right. I'll give this a shot. –  Lance Roberts Jan 6 '10 at 16:44
    
It worked great. I can't believe all the crappy documentation out there insisting I had to quote the command given to SSH to work remotely, and unquote the local file. Thanks. –  Lance Roberts Jan 6 '10 at 18:14
add comment

If it's at all possible:

  1. do not use csh
  2. do what womble suggests
  3. don't use a variable for redirection since it won't work

If you need to be able to change the redirection, at least in Bash and sh, you can do:

file="/some/file"
if [ somecondition ]
then
    exec 3>&1            # send output of file descriptor 3 to stdout
elif [ condition2 ]
    exec 3>>"$file"      # append it to the file
else
    exec 3>"$file"       # overwrite the file        
fi
echo "text" >&3          # output to chosen location
share|improve this answer
    
No choice on csh. –  Lance Roberts Jan 6 '10 at 16:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.