Bash 脚本 - 使用 cmd 而不是 cat

Bash Script - using cmd instead of cat

我写了一个脚本,包括这个循环:

#!/bin/bash

cat "" | while read -r line; do
  echo "$line"; sleep 2;
done

A shellcheck 运行 发出以下消息:

SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.

我将脚本更改为:

#!/bin/bash

cmd < "" | while read -r line; do
  echo "$line"; sleep 2;
done

但现在 bash 退出:

cmd: command not found

我做错了什么?

删除|并将结束行设置为:

 done < ""

您的 cmd 是整个 while cond; do ... done 复合语句,在这种情况下,重定向需要放在最后:

while read -r line; do
  echo "$line"; sleep 0.2
done < ""