0

I'm currently using the following line to find a number of scripts and execute them. The scripts are named as : my_script_0.sh, my_script_1.sh, etc.

find $MY_HOME/SHELL/my_script_[0-9].sh -type f -exec csh -c '"$1" >& "$logfile" &' {} \;

This works fine except that now I would like to create a unique $logfile for each of the executed scripts. I realize that using awk I could do something like this to grab the number of the file and then potentially use that in the logfile name.

find $MY_HOME/SHELL/my_script_[0-9].sh -type f | awk -F"\\.|_" '{print $4}'

The issue is that I don't believe I can use awk with the original statement since I need the positional parameter to be the full path/filename.

Ideally I would like to simply use another positional parameter with the find -exec command. Does it exist?

McArthey
  • 1,614
  • 30
  • 62

1 Answers1

1

The -exec option to find simply excutes the given arguments. Since you pass it csh -c ... it will start a new shell to which you can optionally pass some arguments (one being {}). These arguments are then set as positional arguments $1, $2, $3, ... ,$n in the new shell. In this case the find results are passed one by one and used as $1 in the shell.

I'll suggest an alternative to your find command which uses a loop instead:

foreach script ( $MY_HOME/SHELL/my_script_[0-9].sh )
    csh -c "$script >& $script:r.log"
end

The :r strips the extension of the variable it's attached to so we get the logfile: my_script_n.log for the script my_script_n.sh.

I checked out these references on C-shell syntax:

  • It appears that csh doesn't have the for loop? – McArthey Dec 10 '13 at 19:16
  • @McArthey Ok, this answer works in (ba)sh, running csh in a subshell. Sorry. –  Dec 10 '13 at 19:17
  • Great, thanks! It appears that simply using `foreach script ( $MY_HOME/SHELL/my_script_[0-9].sh )` takes care of it. – McArthey Dec 10 '13 at 19:24
  • Something still isn't exactly correct since an error results: `Missing }.` – McArthey Dec 10 '13 at 19:46
  • @McArthey Yes, as I said it will only work in (ba)sh, not csh. –  Dec 10 '13 at 19:56
  • @McArthey I just updated the for-loop to work in `csh`. Let me know how it works. –  Dec 10 '13 at 22:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42900/discussion-between-herman-torjussen-and-mcarthey) –  Dec 10 '13 at 22:56