• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

确定shell脚本是否执行"采购和QUOT;它

用户头像
it1352
帮助1

问题说明

是否有可能为一个shell脚本来测试它是否是通过执行源?即,例如

Is it possible for a shell script to test whether it was executed through source? That is, for example,

$ source myscript.sh
$ ./myscript.sh

灿的 myscript.sh 的这些不同的shell环境区分?

Can myscript.sh distinguish from these different shell environments?

正确答案

#1

我想,有什么山姆想要做的可能是不可能的。

I think, what Sam wants to do may be not possible.

要什么程度半baken解决方法是可行的,取决于...

To what degree a half-baken workaround is possible, depends on...

    • ...用户的默认外壳,和
    • ...它替代贝壳他们被允许使用。

如果我理解正确,萨姆的要求,他希望有一个脚本,
的MyScript ,那就是...

If I understand Sam's requirement correctly, he wants to have a 'script', myscript, that is...

    1. ...不是的直接的通过它的名字调用它的可执行文件的MyScript
      (即有文件模式A-X );
    1. ...不是的间接的通过调用可执行文件,为用户上海的MyScript
      调用庆典的MyScript
    1. ... 运行其包含的功能和命令,如果通过调用
      采购是:。的MyScript
  1. ...not directly executable via invoking it by its name myscript (i.e. that has chmod a-x);
  2. ...not indirectly executable for users by invoking sh myscript or invoking bash myscript
  3. ...only running its contained functions and commands if invoked by sourcing it: . myscript

首先要考虑的事情是这些

The first things to consider are these

    1. 将它的名字(的MyScript )直接调用的脚本需要在第一线
      脚本像#!/斌/庆典或相似。这将直接决定哪些
      bash的可执行文件(或符号连接)的安装实例将被调用运行
      脚本的内容。这将是一个新的shell进程。它需要
      脚本文件本身具有可执行标志设置。
    1. 通过调用的脚本(路径 )外壳二进制运行脚本名称
      一个参数(上海的MyScript ),是一样的1。 - 除了
      可执行标志并不需要设置,并且所述第一线与
      不需要任何hashbang
      。唯一需要的就是调用
      用户需求的阅读的访问脚本文件。
    1. 通过获取其文件名调用脚本(的MyScript )非常多,
      一样的1。 - exept,它不是被调用一个新的外壳。一切
      脚本的命令在当前shell执行
      后,使用它的环境
      (也污染的环境与任何(新)变量可能设置或
      更改。 (通常这是一件非常危险的事情:但在这里,它可能是
      用于执行退出$返回值在一定条件下....)
  1. Invoking a script directly by its name (myscript) requires a first line in the script like #!/bin/bash or similar. This will directly determine which installed instance of the bash executable (or symlink) will be invoked to run the script's content. This will be a new shell process. It requires the scriptfile itself to have the executable flag set.
  2. Running a script by invoking a shell binary with the script's (path )name as an argument (sh myscript), is the same as '1.' -- except that the executable flag does not need to be set, and said first line with the hashbang isn't required either. The only thing needed is that the invoking user needs read access to the scriptfile.
  3. Invoking a script by sourcing its filename (. myscript) is very much the same as '1.' -- exept that it isn't a new shell that is invoked. All the script's commands are executed in the current shell, using its environment (and also "polluting" its environment with any (new) variables it may set or change. (Usually this is a very dangerous thing to do: but here it could be used to execute exit $RETURNVALUE under certain conditions....)

为'1':结果
容易实现:文件模式A-X的MyScript 将prevent 的MyScript
直接执行。但是,这不会fullfill要求'2'和'3'。

For '1.':
Easy to achieve: chmod a-x myscript will prevent myscript from being directly executable. But this will not fullfill requirements '2.' and '3.'.

对于'2'和'3':结果
更难实现。通过上海的MyScript Invokations需要的阅读
该文件的权限。所以出一种明显的方式似乎文件模式一-R
的MyScript
。不过,这也将DIS-允许3:你将不能够
每个源的脚本。

For '2.' and '3.':
Much harder to achieve. Invokations by sh myscript require reading privileges for the file. So an obvious way out would seem to chmod a-r myscript. However, this will also dis-allow '3.': you will not be able to source the script either.

那么,关于书面方式脚本的方式,使用的 Bashism 的?一个Bashism是
特定的方式做一些其他的壳不明白:使用
具体的变量,命令等,这可以在脚本中被用来
发现这种情况与有所作为它(如显示warning.txt
电子邮件地址管理员等)。但有没有办法在地狱,这将prevent SH
庆典或读书,并努力执行所有的下列任何其他的shell
命令/线路写进剧本,除非你通过调用杀壳
退出

So what about writting the script in a way that uses a Bashism? A Bashism is a specific way to do something which other shells do not understand: using specific variables, commands etc. This could be used inside the script to discover this condition and "do something" about it (like "display warning.txt", "mailto admin" etc.). But there is no way in hell that this will prevent sh or bash or any other shell from reading and trying to execute all the following commands/lines written into the script unless you kill the shell by invoking exit.

例如:的Bash中,由脚本看到的环境 $ BASH 的人都知道,
$ BASH_ARGV $ BASH_COMMAND $ BASH_SUBSHELL BASH_EXECUTION_STRING ...。如果
通过 SH 调用(也如的来源的内部 SH ),在执行shell会看到
所有这些 $ BASH _ * 为空的环境变量。再次,这可以使用
在脚本中发现这种情况,并做一些事情...的但不
prevent以下命令从被调用!

Examples: in Bash, the environment seen by the script knows of $BASH, $BASH_ARGV, $BASH_COMMAND, $BASH_SUBSHELL, BASH_EXECUTION_STRING... . If invoked by sh (also if sourced inside a sh), the executing shell will see all these $BASH_* as empty environment variables. Again, this could be used inside the script to discover this condition and "do something"... but not prevent the following commands from being invoked!

我现在假定...

    1. ...脚本中使用#!/斌/庆典作为它的第一行,
    1. ...用户设置bash作为其外壳和被调用的命令
      从下表中的Bash,这是他们的登录shell
    1. ... SH 可用,它是一个符号链接庆典破折号
  1. ...the script is using #!/bin/bash as its first line,
  2. ...users have set Bash as their shell and are invoking commands in the following table from Bash and it is their login shell,
  3. ...sh is available and it is a symlink to bash or dash.

这将意味着以下invokations是可能的,与列出的值
环境变量

This will mean the following invokations are possible, with the listed values for environment variables


vars invok's   | ./scriptname | sh scriptname | bash scriptname | . scriptname
--------------- -------------- --------------- ----------------- -------------
$0             | ./scriptname | ./scriptname  | ./scriptname    | -bash
$SHLVL         | 2            | 1             | 2               | 1
$SHELLOPTS     | braceexpand: | (empty)       | braceexpand:..  | braceexpand:
$BASH          | /bin/bash    | (empty)       | /bin/bash       | /bin/bash
$BASH_ARGV     | (empty)      | (empty)       | (empty)         | scriptname
$BASH_SUBSHELL | 0            | (empty)       | 0               | 0
$SHELL         | /bin/bash    | /bin/bash     | /bin/bash       | /bin/bash
$OPTARG        | (empty)      | (empty)       | (emtpy)         | (emtpy)

现在你可以把一个逻辑到文本脚本:

Now you could put a logic into your text script:

    • 如果 $ 1,0 不等于 -bash ,然后做一个退出$ SOMERETURNVALUE
  • If $0 is not equal to -bash, then do an exit $SOMERETURNVALUE.

在情况下,脚本通过上海的MyScript 庆典的MyScript ,那么它将被称为
退出调用外壳。如果它是在当前shell中运行,它会
继续运行。 (警告:如果脚本有任何其他退出语句,
您当前的shell将'杀死'...)

In case the script was called via sh myscript or bash myscript, then it will exit the calling shell. In case it was run in the current shell, it will continue to run. (Warning: in case the script has any other exit statements, your current shell will be 'killed'...)

所以,把你的非执行 myscript.txt 接近其开始像
这可能会做一些接近你的目标:

So put into your non-executable myscript.txt near its beginning something like this may do something close to your goal:

echo BASH=$BASH
test x${BASH} = x/bin/bash && echo "$? :    FINE.... You're using 'bash ...'"
test x${BASH} = x/bin/bash || echo "$? :    RATS !!! -- You're not using BASH and I will kick you out!"
test x${BASH} = x/bin/bash || exit 42
test x"${0}" = x"-bash"    && echo "$? :    FINE.... You've sourced me, and I'm your login shell."
test x"${0}" = x"-bash"    || echo "$? :    RATS !!! -- You've not sourced me (or I'm not your bash login shell) and I will kick you out!"
test x"${0}" = x"-bash"    || exit 33

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfggcc
系列文章
更多 icon
同类精品
更多 icon
继续加载