我有一个简单的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
当i remove gdb -args 前缀程序效果很好(也就是说,它像应该像零一样除以零(这是程序:
#include <stdio.h>
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
(将共享/home/oren/Downloads/users.txt
stdin ,因此您的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 <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
加载到数组中并通过它:
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