How can I make bash wait for a process to finish before continuing on with a script? [closed]

这一生的挚爱 提交于 2019-12-02 16:48:27

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/13188542/how-can-i-make-bash-wait-for-a-process-to-finish-before-continuing-on-with-a-scr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!