How can I make bash wait for a process to finish b

2019-03-06 12:07发布

I have a script that uses sed to search and replace a simple .yaml file and output a .sh file which should then be pulled in with source. My problem is that I can't figure out how to wait until sed outputs the .sh before calling source.

sed -e 's/:[^:\/\/]/="/g;s/$/"/g;s/ *=/=/g' in.yaml > out.sh
source out.sh

EDIT
I get this error

script.sh: line 2: out.sh: No such file or directory

END EDIT

I've tried

sed -e 's/:[^:\/\/]/="/g;s/$/"/g;s/ *=/=/g' in.yaml > out.sh
wait $!
source out.sh

, but I believe that this requires the process to be in the bg. I tried throwing it in the bg, but I get the bg: no job control error message. I read that overriding this is bad practice, so I'm trying to avoid doing so.

How can I make source wait on sed?

2条回答
干净又极端
2楼-- · 2019-03-06 12:15

You don't have to do anything else than :

sed -e 's/:[^:\/\/]/="/g;s/$/"/g;s/ *=/=/g' in.yaml > /path/to/out.sh
source /path/to/out.sh

the shell by default run commands one by ones, so it will run source when sed will finish.

For your error, you should put the full path of out.sh in your source command.

查看更多
疯言疯语
3楼-- · 2019-03-06 12:21

The source commands looks for the sourced file on your PATH when its name contains no slashes (but it only has to be readable; it does not have to be executable). You probably don't have the current directory on your PATH.

Try:

source ./out.sh
查看更多
登录 后发表回答