Sometimes I use same foreach operation with different target files in csh.
If I can give foreach command in a single line, I could easily substitue the target file names to repeat the process.(I usually use `find . -name ...` for target files)
For the purpose of this question, let's assume I want to cat all the *.txt files.
$ foreach i (*.txt)
foreach? cat $i
foreach? end
I read https://unix.stackexchange.com/questions/32873/running-a-full-foreach-command and tried
alias disp 'echo "foreach i (*.txt)"; echo "cat "\$"i"; echo "end"; | /bin/csh'
when I type disp
it gives me Invalid null command
.
If I can do it in a single line, I could do !foreach:gs/\.c\>/.h/
(do the same replacing .c with .h).
How can I do it?
$? -The exit status of the last command executed. $0 -The filename of the current script. $# -The number of arguments supplied to a script.
You use the set or setenv commands to initialize a variable, where set is used for current shell and setenv for current and any subshells (i.e. it will automatically export variables to subshell). setenv should be used for PATH, HOME, and all other system related environmental settings.
This method works for me:
printf 'foreach f ( 1 2 3 )\n echo $f \n end' | tcsh
There is no easy way to do the foreach in one line under tcsh.
However, by using alias, you may get something very close to your question.
alias disp 'foreach i (*.txt)\
cat $i\
end'
You can call disp
in your terminal.
If you want to direct the result into a pipe, then echo `disp` | grep foobar
.
Sometimes, we need to save the result first, before further processing: set foo=`disp`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With