Linux中gdb退出循环的命令,从bash循环执行gdb立即退出(示例代码)

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

#!/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/Downloads/users.txt文件:

cat /home/oren/Downloads/users.txt

答案是:

OrenIshShalom

当我删除gdb --args前缀时,程序运行正常(也就是说,它像它应该的那样除以零)这是程序:

#include

int main(int argc, char **argv)

{

int i=0;

if (argc > 1)

{

i = (i+argc)/(argc-3);

}

}

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

...

(gdb) quit

这里发生了什么?谢谢!

编辑:

当我删除外部循环gdb工作正常...但我非常希望保持这个循环,因为脚本中的所有内容都是基于它构建的

答案

整个while循环(包括read和gdb)将共享stdin,这是/home/oren/Downloads/users.txt所以你的gdb也会消耗来自/home/oren/Downloads/users.txt的数据。 gdb立即退出,因为它很快消耗所有数据并看到EOF。

请参阅以下示例:

[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

[...]

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加载到一个数组并完成它:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值