【问题标题】:How can I prevent unintentional passing of arg in c-shell script that sources other c-shell script (linux)如何防止意外传递来自其他 c-shell 脚本(linux)的 c-shell 脚本中的 arg
【发布时间】:2018-02-04 15:52:27
【问题描述】:

在 linux 上,c-shell 是默认解释器...

我获取了一个我将 args 传递给的 c-shell 脚本。在该脚本中,我获取了另一个没有 args 的 c-shell 脚本。但是我传递给父脚本的参数会传递给孩子。

问:为什么会这样?

问:如何防止将参数传递给子脚本

简单的例子...

猫父母.csh

source ./child.csh
exit

cat child.csh

echo $arvv[*]
exit

会发生什么...

source ./parent.csh abc def
abc def

我不希望将“abc”和“def”传递给孩子。

【问题讨论】:

  • 请参阅superuser.com/a/618442/292141,了解为什么您会在 child 中看到这些论点以及可能的解决方案。顺便说一句,我花了 5 秒钟才找到答案……
  • 看起来你提供的指针是在谈论获取脚本和在它自己的 shell 中运行它之间的区别。就我而言,我正在采购两者。

标签: csh


【解决方案1】:

我想我找到了自己的答案。 (如有错误请指正)

嵌套的“采购”(上例中的父采购子)的行为就像用 child.csh 的内容替换“源 ./child.csh”(几乎像“包含”)所以如果你的示例代码是。 ..

echo "This is the parents code"
source ./child.csh
echo "Parent exiting"
exit

还有……

echo "This is the child code"
echo $argv[*]
echo "Child exiting"
exit

它会“真的”看起来像......

echo "This is the parents code"
echo "This is the child code"
echo $argv[*]
echo "Child exiting"
exit    
echo "Parent exiting"
exit

传递给父级的参数会暴露给子级,因为它位于同一范围内。这个解释有一个问题......为什么它在子代码中的替换中点击“退出”时它不退出?

不管怎样,如果我这样称呼孩子的话……

source child.csh ""

这似乎解决了问题。

【讨论】:

  • 采购几乎就像包括但不完全一样。我的采购心理模式是“将当前脚本推入堆栈,现在启动采购脚本”之一。因此,shell 知道当它遇到 exit 时,它是在源 shell 中,并将原始脚本从堆栈中弹出 + 继续执行。
猜你喜欢
  • 2015-08-18
  • 2015-07-22
  • 1970-01-01
  • 2016-05-27
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
相关资源
最近更新 更多