如何以编程方式验证 CYGWIN 应用程序是否安装在 SH 脚本中?

发布于 2024-07-29 16:08:08 字数 257 浏览 7 评论 0原文

情况如下:

我正在编写一个 SH 部署脚本,该脚本将在 CYGWIN 中使用 RSYNC 命令部署网站。 安装完成后,我想向开发团队发送一封电子邮件,说明已经进行了部署并提供了一些详细信息。 我将使用“exim”从 CYGWIN 发送邮件。

问题是,exim 仅在安装 CYGWIN 时是可选的,如果未安装,我想退出我的 SH 脚本。 如果应用程序安装在脚本中,是否有想法检查 UNIX(并在 CYGWIN 中工作)?

提前致谢 !

Here's the situation :

I am writing a SH deployment script that will deploy a website with an RSYNC command in CYGWIN. After the installation, I want to send an e-mail to the development team to say that a deployment has been made with some details. I will use "exim" to send the mail from CYGWIN.

The thing is that, exim is only optional when installing CYGWIN and I would like to quit my SH script if it's not installed. Any idea to check in UNIX (and working in CYGWIN) if an app is installed in a script ?

Thanks in advance !

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拔了角的鹿 2024-08-05 16:08:08
# bash script (should also work with sh)
# someprog must be on the path for type to find it
if type -p "someprog"
then
    echo "it's there"
else
    echo "it isn't"
fi
# bash script (should also work with sh)
# someprog must be on the path for type to find it
if type -p "someprog"
then
    echo "it's there"
else
    echo "it isn't"
fi
半透明的墙 2024-08-05 16:08:08

具体来说,要检查“exim”,请

exim -bV

在我的 Cygwin 上执行,它会给出类似的输出,

Exim version 4.69 #1 built 28-Jan-2008 21:59:08
Copyright (c) University of Cambridge 2006
Probably GDBM (native mode)
Support for: crypteq iconv() PAM OpenSSL Content_Scanning
Lookups: lsearch wildlsearch nwildlsearch iplsearch dbm dbmnz dnsdb dsearch ldap
    ldapdn ldapm passwd
Authenticators: cram_md5 plaintext spa
Routers: accept dnslookup ipliteral manualroute queryprogram redirect
Transports: appendfile/maildir/mailstore/mbx autoreply pipe smtp
Size of off_t: 8
Configuration file is /etc/exim.conf

来自“exim”手册中的“exim -bV”,

该选项使 Exim 写入当前版本号,编译
标准输出的 exim 二进制文件的编号和编译日期。
它还列出了正在使用的 DBM 库、可选模块
(例如特定的查找类型),包含在
二进制文件,以及正在使用的运行时配置文件的名称。

作为其操作的一部分,-bV 使 Exim 读取并检查其语法
配置文件。 然而,这只是静态检查。 它无法检查
有待扩展的价值观。 例如,虽然 ACL 动词拼写错误
被检测到,但动词参数中没有错误。 你不能依赖
-bV 单独发现(例如)配置中的所有拼写错误;
需要一些实际的测试。 -bh 和 -N 选项提供更多
动态测试设施。


typewhich 检查进行比较。

type 是 shell 内置命令。 它指示如果用作命令,shell 将如何解释名称。 -p 选项使其返回发出命令时将执行的磁盘文件的名称。

只需使用相同的 bash 算法在搜索 PATH 中按给定名称搜索可执行文件。

如果有人在路径中放置同名的可执行文件,则这两个选项都将返回 true。 如果考虑安全性,您应该更加小心地从脚本调用命令。

了解您正在运行的内容可能是谨慎的做法。
当然,我还可以实现一个名为 exim 的 shell 脚本,它将在 -bV 上返回上述输入; 以及背景中的混乱事物——你的偏执程度是多少?

Specifically to check for 'exim', execute

exim -bV

On my Cygwin it gives an output like,

Exim version 4.69 #1 built 28-Jan-2008 21:59:08
Copyright (c) University of Cambridge 2006
Probably GDBM (native mode)
Support for: crypteq iconv() PAM OpenSSL Content_Scanning
Lookups: lsearch wildlsearch nwildlsearch iplsearch dbm dbmnz dnsdb dsearch ldap
    ldapdn ldapm passwd
Authenticators: cram_md5 plaintext spa
Routers: accept dnslookup ipliteral manualroute queryprogram redirect
Transports: appendfile/maildir/mailstore/mbx autoreply pipe smtp
Size of off_t: 8
Configuration file is /etc/exim.conf

From the 'exim' manual on 'exim -bV',

This option causes Exim to write the current version number, compilation
number, and compilation date of the exim binary to the standard output.
It also lists the DBM library this is being used, the optional modules
(such as specific lookup types), the drivers that are included in the
binary, and the name of the run time configuration file that is in use.

As part of its operation, -bV causes Exim to read and syntax check its
configuration file. However, this is a static check only. It cannot check
values that are to be expanded. For example, although a misspelt ACL verb
is detected, an error in the verb's arguments is not. You cannot rely on
-bV alone to discover (for example) all the typos in the configuration;
some realistic testing is needed. The -bh and -N options provide more
dynamic testing facilities.


Comparing with the type and which checks.

type is a shell builtin command. It indicates how a name will be interpreted by the shell if used as a command. The -p option causes it to return the name of the disk file that would be executed if the command was issued.

which simply searches for an executable by the given name in the search PATH using the same bash algorithm.

Either of these will return true if someone puts an executable file called by the same name in the path. You should be more careful about invoking commands from a script, if security is a concern.

It may be prudent to know what you are running.
Of course, I can also implement a shell script called exim that will return the above input on -bV; and mess-up things in the background -- what is your level of paranoia?

静水深流 2024-08-05 16:08:08

您可以使用“which”命令。
它会返回程序的位置,如果不存在则什么也不返回。

You can use the 'which' command.
It will return you the location of the program or nothing if not present.

浪推晚风 2024-08-05 16:08:08

为了减轻 nik 带来的安全问题,您可以根据预期的位置列表检查可执行文件的位置:

case $(type -p someprog) in

    /usr/bin/someprog \
    | /bin/someprog \
    | /usr/local/someprog)

        echo "Valid location";; 
    *)
        echo "Invalid location";;
esac

To alleviate the security concerns that nik brought up you can check the location of the executable against an expected list of locations:

case $(type -p someprog) in

    /usr/bin/someprog \
    | /bin/someprog \
    | /usr/local/someprog)

        echo "Valid location";; 
    *)
        echo "Invalid location";;
esac
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文