2

I feel like this should be simple but quoting and the like always confuse me in bash.. I am trying to run a command over an ssh connection and I want to use timeout with that command. All well and good for simple commands, but now I want to run something like this (just a mock):

while read id; do
    find . -name $id -exec echo '{}' \;
    find . -name ${id}-other -exec echo '{}' \;
done < <(grep foo foo.txt | awk '{print $1}')

so I try this: timeout 60 while read id; do find . -name $id -exec echo '{}' \;; find . -name ${id}-other -exec echo '{}' \;; done < <(grep foo foo.txt | awk '{print $1}') which of course doesn't work. So I try to wrap it in a bash -c, but I really can't figure out how to properly escape it...

Any help would be appreciated.. I know this might be a duplicate of something, but I have actually looked around and I can't quite fit the examples I've found to my case here.

user3591723
  • 1,224
  • 1
  • 10
  • 22
  • The same logic applies as in [Why can't I use Unix `nohup` with Bash `for` loop?](http://stackoverflow.com/questions/3099092/why-cant-i-use-unix-nohup-with-bash-for-loop/3099107?s=1|3.6339#3099107) – Jonathan Leffler Aug 21 '15 at 05:23

1 Answers1

5

The argument to timeout is a command. It is not a shell expression. That is, it is the filename of a utility followed by the arguments to that utility. (You can use redirections, but the redirections are applied to the timeout command; they are not interpreted by timeout. Since timeout executes the specified command in the same execution environment, the redirections will be inherited by the command.)

As you say, you can use the bash command to run any bash expression, but quoting is awkward. Quoting can be simplified using here-docs:

timeout 60 bash <<"EOF"
while read id; do
    find . -name $id -exec echo '{}' \;
    find . -name ${id}-other -exec echo '{}' \;
done < <(grep foo foo.txt | awk '{print $1}')
EOF
rici
  • 234,347
  • 28
  • 237
  • 341