当我想在没有传递参数(文件)的情况下使用标准输入时,脚本不起作用。有什么方法可以在此代码中使用标准输入而不是文件吗?
我试过这个:
if [ ! -n $1 ] # check if argument exists
then
$1=$(</dev/stdin) # if not use stdin as an argument
fi
var="$1"
while read line
do
... # find the longest line
done <"$var"
最佳答案
对于希望在缺少参数时从 stdin 读取值的一般情况,这会起作用。
$ echo param | script.sh
$ script.sh param
脚本.sh
#!/bin/bash
set -- "${1:-$(</dev/stdin)}" "${@:2}"
echo $1
关于bash - 没有传递参数时如何读取标准输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619490/