首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >立即从bash循环退出执行gdb。

立即从bash循环退出执行gdb。
EN

Stack Overflow用户
提问于 2018-02-07 09:33:46
回答 1查看 317关注 0票数 0

我有一个简单的bash脚本,它在一个循环中运行一些程序3次(/home/oren/下载/users.txt文件有一行)

代码语言:javascript
复制
#!/bin/bash
#######################
# Loop over all users #
#######################
while IFS='' read -r username
do
    for answer in {1..3};
    do
        ##############################################
        # Only perform check if both files exist ... #
        ##############################################
        if [ -f /home/oren/Downloads/someFile.txt ] && [ -f /home/oren/Downloads/anotherFile.txt ];
        then
            gdb --args /home/oren/Downloads/MMM/example PPP DDD 
        fi
    done
done < /home/oren/Downloads/users.txt

这里是/home/oren/下载/users.txt文件:

代码语言:javascript
复制
cat /home/oren/Downloads/users.txt

答案是:

代码语言:javascript
复制
OrenIshShalom

当我remove gdb -args前缀时,程序运行良好的(也就是说,它除以零,就像它应该的那样),下面是程序:

代码语言:javascript
复制
#include <stdio.h>

int main(int argc, char **argv)
{
    int i=0;
    if (argc > 1)
    {
        i = (i+argc)/(argc-3);
    }
}

但是,当我添加gdb -args时,gdb 立即退出

代码语言:javascript
复制
...
(gdb) quit

这里发生了什么事?谢谢!

编辑:

当我移除外循环gdb工作很好的 ..。但是我非常希望保持这个循环,因为脚本中的所有东西都是在它之上构建的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-07 13:32:58

整个while循环(包括readgdb)将共享stdin,即/home/oren/Downloads/users.txt,因此gdb也将使用/home/oren/Downloads/users.txt中的数据。gdb会立即退出,因为它会很快地消耗掉所有的数据并看到EOF。

见以下示例:

代码语言:javascript
复制
[STEP 109] # cat file
line 1
line 2
line 3
[STEP 110] # cat foo.sh
while read line; do
    gdb /bin/ls
done < file
[STEP 111] # bash foo.sh
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[...]
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /bin/ls...(no debugging symbols found)...done.
(gdb) Undefined command: "line".  Try "help".
(gdb) Undefined command: "line".  Try "help".
(gdb) quit
[STEP 112] #

对于您的情况,您可以将文件/home/oren/Downloads/users.txt加载到一个数组中,然后遍历它:

代码语言:javascript
复制
usernames=()
nusers=0
while IFS='' read -r username; do
    usernames[nusers++]=$username
done < /home/oren/Downloads/users.txt

for username in "${usernames[@]}"; do
    ...

    gdb ...

    ...
done
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48660274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档