加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

当作为选项传递时,shell代码扩展不起作用

发布时间:2020-12-15 17:01:52 所属栏目:安全 来源:网络整理
导读:我发现波浪扩展不适用于以下情况: $set -x$./print_arg.pl destdir=~/test+ ./print_arg.pl 'destdir=/root/test'destdir=/root/test$./print_arg.pl dest-dir=~/test+ ./print_arg.pl 'dest-dir=~/test'dest-dir=~/test$./print_arg.pl -destdir=~/test+ .
我发现波浪扩展不适用于以下情况:
$set -x

$./print_arg.pl destdir=~/test
+ ./print_arg.pl 'destdir=/root/test'
destdir=/root/test

$./print_arg.pl dest-dir=~/test
+ ./print_arg.pl 'dest-dir=~/test'
dest-dir=~/test

$./print_arg.pl -destdir=~/test
+ ./print_arg.pl '-destdir=~/test'
dest-dir=~/test

print_arg.pl的内容是

#!/usr/bin/perl
print $ARGV[0],"n";

根据Order of Shell processing,壳牌将在“波浪扩张”之前分裂.而且我注意到单词分裂实际上是不同的.结果不同的原因是什么?

Tilde扩展也发生在shell变量赋值中,其中destdir =?/ test类似. dest-dir =?/ test和-destdir =?/ test不会,因为 – 不是变量名中的有效字符.虽然shell没有将destdir =?/ test作为变量赋值进行评估(如果使用set -k,它会出现),看起来解析器仍在处理它并在RHS上执行波浪扩展.

从http://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html#Tilde-Expansion开始:

Each variable assignment is checked for unquoted tilde-prefixes immediately following a ‘:’ or the first ‘=’. In these cases,tilde expansion is also performed. Consequently,one may use file names with tildes in assignments to PATH,MAILPATH,and CDPATH,and the shell assigns the expanded value.

请注意,设置-k选项后,将按原样处理正确的变量赋值,并从传递给print_arg.pl的参数列表中删除:

~ $set -kx
~ $./print_arg.pl destdir=~/bin foo
+ destdir=/Users/clint/bin
+ ./print_arg.pl foo
foo
~ $./print_arg.pl dest-dir=~/bin foo
+ ./print_arg.pl 'dest-dir=~/bin' foo
dest-dir=~/bin

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读