AIX (UNIX)/Linux 外壳。为什么我不能调用我导出的变量

发布于 01-15 20:07 字数 1259 浏览 3 评论 0原文

我的问题会在这里变得简洁
。看看这个(将其设置为“通用” - 坚持尽可能不讨论特定的代码审查 - 堆栈指南)

# some 'echos' &  'exports'  might sound redundant agreed. That was to make sure I did not miss out on anything 
g=rf1.`date '+%Y%m%d-%H%M'`.log ;export pat x ; 
for d in  `find <path> -type d -maxdepth 1`  ; 
do r=`basename "$d"` ;echo "\$r basename is $r"; 
# there can be multiple matches . pat is an array that can capture this 
 ( [[  -n `ls  /<path>/*file*.ext 2>/dev/null` ]] 
&& 
 ( pat=( $(ls  /<path>/*file*.ext ) ) ; 
# confirming that val is captured  by x  
# I am grabbing the value of pat for testing 
echo "\$pat is $pat"; export pat ; export 
x=${pat[0]}; 
echo "x is $x " ;  ) )  
|| ( echo "did not find ya da ya "   ) ; 
echo "Repeat -\$x is $x"  ;  
< some more  code that tests the value of x and 
proceeds > ; 
done |  tee /<path>  2>&1 

我将 array pat 的值抓取到var x 但尽管我进行了所有调整,我仍无法将其输出到我想要使用它的后半部分中捕获的值。又名 echo "\$pat is $pat";出口帕特;导出x=${pat[0]}; echo "$\x is $x 会给出值,但在 echo "Repeat -\$x is $x" 时它什么也没有给出。为什么会这样
顺便说一句,我找到了几种解决方法所以我不是在寻找代码修复,而是想知道为什么 $x 在第二部分中被遗忘
PS 这是我在命令行上运行的快速操作。小的时候没啥感觉。将其放入脚本中

Will be v terse w/ my Q here
. Take a look at this ( made it as 'generic' as poss - adhering to not discuss a specific code review much as a can be - stack guideline )

# some 'echos' &  'exports'  might sound redundant agreed. That was to make sure I did not miss out on anything 
g=rf1.`date '+%Y%m%d-%H%M'`.log ;export pat x ; 
for d in  `find <path> -type d -maxdepth 1`  ; 
do r=`basename "$d"` ;echo "\$r basename is $r"; 
# there can be multiple matches . pat is an array that can capture this 
 ( [[  -n `ls  /<path>/*file*.ext 2>/dev/null` ]] 
&& 
 ( pat=( $(ls  /<path>/*file*.ext ) ) ; 
# confirming that val is captured  by x  
# I am grabbing the value of pat for testing 
echo "\$pat is $pat"; export pat ; export 
x=${pat[0]}; 
echo "x is $x " ;  ) )  
|| ( echo "did not find ya da ya "   ) ; 
echo "Repeat -\$x is $x"  ;  
< some more  code that tests the value of x and 
proceeds > ; 
done |  tee /<path>  2>&1 

I grabbed the value of array pat into var x but not withstanding all my tweaks I could not get it to out put the value captured in the latter part where I wanted to use it. aka echo "\$pat is $pat"; export pat ; export x=${pat[0]}; echo "$\x is $x will give values but at echo "Repeat -\$x is $x" it gives nothing. Why so
BTW I have found several workarounds. So I am not looking for code fixes but wanna know why $x gets forgotten in the 2nd part .
PS This is a quickie I ran on the command line. for the small time didnt feel like putting it in a script

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文