我试图让bash处理来自stdin的数据,但是没有运气.我的意思是以下工作:
echo "hello world" | test=($(< /dev/stdin)); echo test=$test
test=
echo "hello world" | read test; echo test=$test
test=
echo "hello world" | test=`cat`; echo test=$test
test=
Run Code Online (Sandbox Code Playgroud)
我想要输出的地方test=hello world.我试过把""引号括起来"$test"也不起作用.
我有一些脚本,用参数工作,他们工作得很好,但我想他们能够从标准输入读取,例如从一个管道,一个例子,假设这就是所谓的读:
#!/bin/bash
function read()
{
echo $*
}
read $*
Run Code Online (Sandbox Code Playgroud)
现在这适用read "foo" "bar",但我想用它作为:
echo "foo" | read
Run Code Online (Sandbox Code Playgroud)
我该如何做到这一点?
当没有传递参数(文件)时我想使用标准输入时脚本不起作用.有没有办法在这段代码中使用stdin而不是文件?
我试过这个:
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"
Run Code Online (Sandbox Code Playgroud) 我想echo这可能包含一个字符串参数相同的echo.如何在不修改字符串的情况下完成?
例如:
$ var="-e something"
$ echo $var
something
Run Code Online (Sandbox Code Playgroud)
......没打印 -e