Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wc-m似乎在bash中循环时停止_Bash_Shell_Unix_Wc - Fatal编程技术网

wc-m似乎在bash中循环时停止

wc-m似乎在bash中循环时停止,bash,shell,unix,wc,Bash,Shell,Unix,Wc,我正在学习一门关于UNIX的入门课程,其中一部分是bash脚本。我似乎已经理解了这些概念,但在这个特殊的问题上,我无法将我的头脑集中在这个问题上 我有一个txt文件,由1列随机用户名组成。 然后将该txt文件用作我的bash脚本的参数,理想情况下,该脚本使用用户名获取页面并计算该页面上的字符数。如果页面获取成功,则字符计数将与用户名一起保存在另一个txt文件中 下面是一个代码: #!/bin/bash filename=$1 while read username; do curl -

我正在学习一门关于UNIX的入门课程,其中一部分是bash脚本。我似乎已经理解了这些概念,但在这个特殊的问题上,我无法将我的头脑集中在这个问题上

我有一个txt文件,由1列随机用户名组成。 然后将该txt文件用作我的bash脚本的参数,理想情况下,该脚本使用用户名获取页面并计算该页面上的字符数。如果页面获取成功,则字符计数将与用户名一起保存在另一个txt文件中

下面是一个代码:

#!/bin/bash
filename=$1

while read username; do
    curl -fs "http://example.website.domain/$username/index.html"
    if [ $? -eq 0 ]
    then
        x=$(wc -m)
        echo "$username $x" > output.txt
    else
        echo "The page doesn't exist"
    fi
done < $filename
现在我在这里遇到的问题是,在一次成功的获取之后,它对字符进行计数,将它们输出到文件,然后完成循环并退出程序。如果我删除wc-m位,代码运行得非常好

问:这是应该发生的吗?我应该如何克服这一点来实现我的目标?或者我在其他地方犯了错误?

默认情况下,wc程序以及您在Linux上可以找到的许多其他实用程序都希望其输入通过标准输入提供给它,并将其输出提供给标准输出

在您的例子中,您希望wc对curl调用的结果进行操作。可以通过将curl的结果存储在变量中并将变量的内容传递给wc来实现这一点

或者,您可以将整个命令放在一个管道中,这可能更好,尽管您可能希望设置-o pipefail以捕获curl中的错误:


否则,正如@Dominique所说,您的wc将无限期地等待,直到它获得一些输入。

显示的代码与您在问题中的想法和主张不符

您的curl命令获取web并将其抛出到stdout:您没有保留这些信息以备将来使用。然后,您的wc没有任何参数,因此它开始从stdin读取。在stdin中,您有$filename中的用户名列表,因此计算的数字不是web的字符,而是文件的剩余字符。一旦计算完毕,stdin中就没有什么可读取的了,因此循环结束,因为它到达了文件的末尾

您正在寻找以下内容:

#!/bin/bash
filename="$1"

set -o pipefail
rm -f output.txt
while read username; do
    x=$(curl -fs "http://example.website.domain/$username/index.html" | wc -m)
    if [ $? -eq 0 ]
    then
        echo "$username $x" >> output.txt
    else
        echo "The page doesn't exist"
    fi
done < "$filename"
这通常是个坏主意。最好是:

if <cmd>...

正如其他人已经指出的,just wc将挂起,因为它希望您在stdin上提供输入

你好像在找类似的东西

#!/bin/bash
filename=$1
# Use read -r
while read -r username; do
    if page=$(curl -fs "http://example.website.domain/$username/index.html"); then
        # Feed the results from curl to wc
        x=$(wc -m <<<"$page")
        # Don't overwrite output file on every iteration
        echo "$username $x"
    else
        # Include parameter in error message; print to stderr
        echo "$0: The page for $username doesn't exist" >&2
    fi
# Note proper quoting
# Collect all output redirection here, too
done < "$filename" >output.txt

这将导致if检查wc的退出代码,该代码始终为真。无论如何,您希望避免显式检查$?@tripleee set-o pipefail保留curl的错误状态的反模式。保存得很好,但您仍然拥有反模式。@triplee您是对的,但我不想再修改他的代码。无论如何,使用pipefail选项,检查的退出代码应该是失败命令之一,而不是最后一个命令。。。或者我遗漏了什么?不,现在都搞定了。但您仍然在每次迭代时覆盖输出文件。
<cmd>
if [ $? -eq 0 ]...
if <cmd>...
if x=$(curl -fs "http://example.website.domain/$username/index.html" | wc -m); then
    echo...
#!/bin/bash
filename=$1
# Use read -r
while read -r username; do
    if page=$(curl -fs "http://example.website.domain/$username/index.html"); then
        # Feed the results from curl to wc
        x=$(wc -m <<<"$page")
        # Don't overwrite output file on every iteration
        echo "$username $x"
    else
        # Include parameter in error message; print to stderr
        echo "$0: The page for $username doesn't exist" >&2
    fi
# Note proper quoting
# Collect all output redirection here, too
done < "$filename" >output.txt