传递给子Shell的Shell变量值?
我正在用shell编写脚本,它类似于:传递给子Shell的Shell变量值?,shell,Shell,我正在用shell编写脚本,它类似于: temp=0 while true; do case "a" in a) temp=5; ;; esac break done | echo "$temp" (温度值是if在子shell中的条件,并且需要|(管道)作为标准输出重定向到标准输入) 我需要临时5,但我在echo中得到0。有什么方法可以在case中保存值(没有tempfile,正确无误)?您希望这样吗?打印5和0 #!/bin/sh temp=0 whi
temp=0
while true; do
case "a" in
a) temp=5; ;;
esac
break
done | echo "$temp"
(温度值是if在子shell中的条件,并且需要|(管道)作为标准输出重定向到标准输入)
我需要临时5,但我在echo中得到0。有什么方法可以在case中保存值(没有tempfile,正确无误)?您希望这样吗?打印5和0
#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat
echo "temp unchanged because of subshell $temp"
这会帮助你过滤掉你的状态吗?
我仍然不明白您为什么需要管道,以及您希望管道中的其他命令如何对状态做出反应。这不是它的工作方式。在脚本中计算条件并相应地调用命令
#!/bin/sh
filter(){
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%s\n' "$data"; printf '%s\n' "$line") ;;
esac
done
if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi
}
temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%s\n' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter
echo "temp unchanged because of subshell $temp"
在
中为true;在a)temp=5;;中不使用case“a”;;以撒;打破完成|回显“$temp”
,在执行while循环之前,将评估管道右侧的$temp
。请显示您在管道右侧实际尝试执行的操作。到echo
的管道没有意义,因为echo
不从标准数据读取。我们需要知道你真正在做什么,因为答案会根据你真正需要的不同而有所不同。不幸的是,我需要保持stdout和stdin不变。关键是将变量和标准输出传递给子shell标准输出和标准输出,保持不变?所以没有烟斗?因为在你的问题中,你说你想要管道?我需要管道stdout(30行)到stdin(30行)并传递那个变量,它决定了这些行可能会发生什么。遗憾的是,我的文件中没有这20行,也不允许我临时创建文件