在 ubuntu linux 11.10 64 位 shell 中完成 bg 任务时设置变量
所以我有这样的代码:
uthreads=4
x=1
cd ./wxWidgets/wxWidgets/build-win
for upxs in $(ls ./lib/*.dll)
do
while [ true ] ; do
if [ $x -lt $uthreads ] ; then
x=$((x+1))
(upx -qqq --best --color $upxs; x=$((x-1))) &
break
fi
sleep 1
done
done
x=$((x=1))
问题在于括号中被修改的变量。当然,这不会按预期工作,因为变量永远不会发送回父 shell。所以我的问题是你如何做到这一点?在 upx 命令完成后,无论其退出状态如何,该变量都必须递增。所以一个简单的 &&或||不会在这里工作。并且您不能使用单个 &在 upx 之后;因为然后该进程在后台运行并立即更改变量...
所以我实际上陷入困境并且需要一些帮助...
so I have this code:
uthreads=4
x=1
cd ./wxWidgets/wxWidgets/build-win
for upxs in $(ls ./lib/*.dll)
do
while [ true ] ; do
if [ $x -lt $uthreads ] ; then
x=$((x+1))
(upx -qqq --best --color $upxs; x=$((x-1))) &
break
fi
sleep 1
done
done
x=$((x=1))
the problem lies in the variable being modified in parenthesis. Naturally this does not work as intended as the variable never gets sent back to the parent shell. So my question is how do you do this? The variable must be incremented after the upx command finishes regardless of it's exit status. so a simple && or || won't work here. and you can't use a single & here after upx; because then the process runs in the background and instantly changes the variable...
So I'm effectively stuck and could use some help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,这个:
while [ true ]
是错误的,true
是一个命令,[
也是一个命令,你在这里不需要两者都需要当 true 时执行; do (并在 bash 中使用[[
,另一个仅用于 POSIX-sh)。这个:
for upxs in $(ls ./lib/*.dll)
也是错误的,而且更严重;我将为您提供一些包含所有这些信息的链接,但现在只需执行:for upxs in ./lib/*.dll
另一个重要的错误是您没有引用变量,每个变量变量必须双引号,如果您从这篇文章中学到任何东西,请就这样。
现在,关于您的特定问题,整个方法是错误的,您无法做您想做的事情,因为您不知道哪个进程将首先结束,并且您等待特定的进程或等待所有进程。
另请注意,您正在使用同一个文件运行四个进程,我确信您的意图是并行处理 4 个不同的文件,并且您也不等待它们完成,很可能在您的脚本结束你的进程被杀死。
次优但简单的解决方案是让 4 个进程并行运行,并等待所有进程完成,然后再启动 4 个进程,这是通过以下方式完成的(类似于您当前的方法):
现在,这可能不足以满足你的要求:) 所以实现 4 个进程始终运行的方法就是这样(使用 PID):
现在这是我承诺的链接:
first of all, this:
while [ true ]
is wrong,true
is a command and so is[
you don't need both here just dowhile true; do
(and use[[
in bash, the other is just for POSIX-sh).this:
for upxs in $(ls ./lib/*.dll)
is also wrong, and it's more serious; I'll give you some links with information about all that but for now just do:for upxs in ./lib/*.dll
another important mistake is that you're not quoting your variables, EVERY SINGLE VARIABLE MUST BE DOUBLE QUOTED, if there's anything you learn from this post please let it be that.
now regarding your particular problem the whole approach is wrong, you can't do what you're trying to do because you don't know which process will end first and you wait for a particular one or wait for all of them.
also notice you're running four processes with the same file, I'm sure your intention was to process 4 different files in parallel and you don't wait for them to finish either, chances are after your script ends your processes get killed.
the suboptimal but easy solution would be to have 4 processes running in parallel and wait for all of them to finish before starting 4 more, that is accomplished by this (which is similar to your current approach):
now, this might not be enough to satisfy you :) so the way to implement 4 running processes at all times would be this (using PIDs):
now here's the links I promised:
假设您尝试通过控制后台运行的程序副本数量来“控制”程序的活动,请查看
xargs
命令。我的系统没有安装upx
,因此我可以推荐的最佳代码示例是使用
xargs --help
查看所有选项,man xargs
如果您幸运地获得有关使用的深入详细信息,请在网络和此处搜索更多使用示例。 维基百科文章是一个好的开始。我希望这有帮助
Assuming that you're trying to 'gate' the activity of a program by controlling how many copies of it are running in the background, check out the
xargs
command. My system doesn't haveupx
installed, so the best I can recommend as a code sample isuse
xargs --help
to see all the options,man xargs
if you're lucky for in-depth detail on usage, and search on web and here for more examples of usage. The wikipedia article is a good start.I hope this helps