Joe*_*ran 4 solaris shell-script variable bourne-shell
有人可以向我解释为什么我的 while 循环似乎有一个内部范围吗?我在网上看到了多种解释,但它们都与管道有关。我的代码没有。
编码:
#!/bin/sh
while read line
do
echo "File contents: $line"
echo
if [ 1=1 ]; then
test1=bob
fi
echo "While scope:"
echo " test1: $test1"
done < test.txt
if [ 1=1 ]; then
test2=test2;
fi
echo;
echo "Script scope: "
echo " test1: $test1"
echo " test2: $test2"
Run Code Online (Sandbox Code Playgroud)
输出:
File contents: In the file
While scope:
test1: bob
Script scope:
test1:
test2: test2
Run Code Online (Sandbox Code Playgroud)
在 Bourne shell 中,重定向复合命令(如while
循环)在子 shell 中运行该复合命令。
在 Solaris 10 和更早的1 中,您不想使用/bin/sh
它,因为它是 Bourne shell。使用/usr/xpg4/bin/sh
或/usr/bin/ksh
来获取 POSIX sh
。
如果由于某种原因您必须使用/bin/sh
,然后解决这个问题,而不是执行以下操作:
compound-command < file
Run Code Online (Sandbox Code Playgroud)
你可以做:
exec 3<&0 < file
compound-command
exec <&3 3<&-
Run Code Online (Sandbox Code Playgroud)
那是:
1 . 在 Solaris 11 及更高版本中,Oracle 最终(终于)制作/bin/sh
了一个 POSIX shell,因此它现在的行为类似于sh
大多数其他 Unices(它解释sh
POSIX 指定的语言,尽管它支持基于它的扩展ksh88
(如其他 Unices ,sh
现在一般基于 ksh88、pdksh、bash、yash 或增强的 ash))
归档时间: |
|
查看次数: |
2575 次 |
最近记录: |