csh (tcsh) ambiguous output redirect when assigning output of piped commands w/stderr redirect to variable in script
2>&1
isn't valid csh
syntax. This only works for Bourne shells.
To pipe both stdout and stderr, you can use the |&
operator.
Also note that ping
will run forever by default, you probably want to add a -c
option to limit the number of times it runs.
set VAR="`ping localhost -c 5 |& grep 'unknown' | cut -b 7-13`"
If that works interactively, your interactive shell is not csh... which is probably a good thing. Stderr redirection in csh uses just >&
but only redirects stderr and stdout together into a file.
See also http://www.faqs.org/faqs/unix-faq/faq/part2/section-9.html
If the intent is to figure out whether $1
resolves, ping
is the wrong tool anyway. Try
set VAR=`dig +short "$1" || echo unknown`