📄 varenv.sh
字号:
## varenv.sh## Test the behavior of the shell with respect to variable and environment# assignments#expect(){ echo expect "$@"}a=1b=2c=3d=4e=5f=6 g=7 h=8a=3 b=4 $CHMOD $MODE $FN# This should echo "3 4" according to Posix.2expect "3 4"echo $a $bset -k# Assignment statements made when no words are left affect the shell's# environmenta=5 b=6 $CHMOD c=7 $MODE d=8 $FN e=9expect "5 6 7 8 9"echo $a $b $c $d $e$CHMOD f=7 $MODE g=8 $FN h=9expect "7 8 9"echo $f $g $hset +k# The temporary environment does not affect variable expansion, only the# environment given to the commandexport HOME=/usr/chetexpect $HOMEecho $HOMEexpect $HOMEHOME=/a/b/c /bin/echo $HOMEexpect $HOMEecho $HOME# This should echo /a/b/cexpect /a/b/cHOME=/a/b/c printenv HOMEset -k# This should echo $HOME 9, NOT /a/b/c 9expect "$HOME"HOME=/a/b/c /bin/echo $HOME c=9expect "$HOME 7"echo $HOME $c# I claim the next two echo calls should give identical output.# ksh agrees, the System V.3 sh does notexpect "/a/b/c 9 /a/b/c"HOME=/a/b/c $ECHO a=$HOME c=9echo $HOME $c $aexpect "/a/b/c 9 /a/b/c"HOME=/a/b/c a=$HOME c=9echo $HOME $c $aset +k# How do assignment statements affect subsequent assignments on the same# line?expect "/a/b/c /a/b/c"HOME=/a/b/c a=$HOMEecho $HOME $a# The system V.3 sh does this wrong; the last echo should output "1 1",# but the system V.3 sh has it output "2 2". Posix.2 says the assignment# statements are processed left-to-right. bash and ksh output the right# thingc=1d=2expect "1 2"echo $c $dd=$c c=$dexpect "1 1"echo $c $d# just for completenessunset d cexpect unsetecho ${d-unset}# no outputexport aa=bcdeexport a/bin/true 2>/dev/nullfunc(){ local YYZ YYZ="song by rush" echo $YYZ echo $A}YYZ="toronto airport"A="AVAR"echo $YYZecho $AA=BVAR funcecho $YYZecho $Aexport A# Make sure expansion doesn't use assignment statements preceding a builtinA=ZVAR echo $AXPATH=/bin:/usr/bin:/usr/local/bin:.func2(){ local z=yy local -a avar=( ${XPATH//: } ) echo ${avar[@]} local}avar=42echo $avarfunc2echo $avar# try to set an attribute for an unset variable; make sure it persists# when the variable is assigned a valuedeclare -i ivarivar=10declare -p ivarunset ivar# export an unset variable, make sure it is not suddenly set, but make# sure the export attribute persists when the variable is assigned a# valueexport ivarecho ${ivar-unset}ivar=42declare -p ivar# make sure set [-+]o ignoreeof and $IGNOREEOF are reflectedunset IGNOREEOFset +o ignoreeofset -o ignoreeofif [ "$IGNOREEOF" -ne 10 ]; then echo "./varenv.sh: set -o ignoreeof is not reflected in IGNOREEOF" >&2fiunset IGNOREEOFset +o ignoreeof# older versions of bash used to not reset RANDOM in subshells correctly[[ $RANDOM -eq $(echo $RANDOM) ]] && echo "RANDOM: problem with subshells"# make sure that shopt -o is reflected in $SHELLOPTS# first, get rid of things that might be set automatically via shell# variablesset +o posixset +o ignoreeofset +o monitorecho $-echo ${SHELLOPTS}shopt -so physicalecho $-echo ${SHELLOPTS}# and make sure it is readonlyreadonly -p | grep SHELLOPTS# This was an error in bash versions prior to bash-2.04. The `set -a'# should cause the assignment statement that's an argument to typeset# to create an exported variableunset FOOFOOFOOFOO=barset -atypeset FOOFOO=abcdeprintenv FOOFOO# test out export behavior of variable assignments preceding builtins and# functions$THIS_SH ./varenv1.sub# more tests; bugs in bash up to version 2.05a$THIS_SH ./varenv2.sub# make sure variable scoping is done righttt() { typeset a=b;echo a=$a; };a=z;echo a=$a;tt;echo a=$a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -