请问shell脚本中指定哪个shell的指令(如#!/bin/bash)有没有办法根据不同的OS取得不同的shell?

24 kali dilihat
Langsung ke pesan pertama yang belum dibaca

xis...@gmail.com

belum dibaca,
6 Mei 2015, 11.39.3806/05/15
kepadash...@googlegroups.com
最近在修改一段老脚本,这段脚本需要同时在AIX和suse linux上运行,之前都使用ksh没有什么问题。但suse linux升级到版本12后,发现其ksh下的local指令含义发生了变化,which local无果。但用bash可行,所以在思考shell脚本中有没有办法通过#!解释指令,根据uname的结果来确定其具体的shell,但尝试几次后并没有结果。

目前的解决方案是使用#!/bin/sh,其在suse linux下为bash的符号链接,AIX下为默认sh,运行暂时没有问题。但这个肯定是个麻烦,请问诸位有能什么好的方法吗?

DaboD

belum dibaca,
6 Mei 2015, 20.43.4406/05/15
kepadash...@googlegroups.com
local 是指用在 function 中宣告 local variable 的那個 local 嗎? 如果是的話, 應該是 BASH builtin
$ type local
local is a shell builtin

所以 which 不到的.

另外, 如果要讓一段 script 能給所有的 shell 執行, 如上所說的 ksh, sh, bash, 那看起來似乎只能使用 sh 的語法和 POSIX 兼容的部份, 感覺似乎也不會減少太多麻煩, 想達成 write once run everywhere?

另一個解法是在各機器上裝上同樣的 shell, 例如 bash, shebang 後這麼寫
#!/bin/bash

或是
#!/bin/env bash

Chaos Eternal

belum dibaca,
6 Mei 2015, 21.57.3006/05/15
kepadash...@googlegroups.com
使用#!/bin/sh是posix规定的标准行为。如果你的脚本希望在多个posix兼容的环境上运行,用/bin/sh是正确的。

即使是在linux上/bin/sh指向/bin/bash,当你用/bin/sh的时候,其行为是符合posix的,而不是跟Bash完全一样。
> --
> -- You received this message because you are subscribed to the Google Groups
> Shanghai Linux User Group group. To post to this group, send email to
> sh...@googlegroups.com. To unsubscribe from this group, send email to
> shlug+un...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/shlug?hl=zh-CN
> ---
> 您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
> 要查看更多选项,请访问https://groups.google.com/d/optout

Suse Shi

belum dibaca,
6 Mei 2015, 23.24.1606/05/15
kepadash...@googlegroups.com
我不认为ksh和bash的脚本应该混用。

我习惯更明确地表示
比如
#!/usr/bin/env bash


对于不同的os平台,用case选项 分离一些步骤就好。



On Wednesday, May 6, 2015 at 11:39:38 PM UTC+8, xis...@gmail.com wrote:

依云

belum dibaca,
7 Mei 2015, 01.20.4307/05/15
kepadash...@googlegroups.com
On Wed, May 06, 2015 at 05:43:44PM -0700, DaboD wrote:
> local 是指用在 function 中宣告 local variable 的那個 local 嗎? 如果是的話, 應該是 BASH builtin
> $ type local
> local is a shell builtin
>
> 所以 which 不到的.

zsh 里 which 可以查到的:

>>> which local
local: shell built-in command

因为 which 也被 zsh builtin 了。不知道他的 ksh 是什么情况。

> 另外, 如果要讓一段 script 能給所有的 shell 執行, 如上所說的 ksh, sh, bash, 那看起來似乎只能使用 sh 的語法和
> POSIX 兼容的部份, 感覺似乎也不會減少太多麻煩, 想達成 write once run everywhere?
>
> 另一個解法是在各機器上裝上同樣的 shell, 例如 bash, shebang 後這麼寫
> #!/bin/bash
>
> 或是
> #!/bin/env bash
>
> On Wednesday, May 6, 2015 at 11:39:38 PM UTC+8, xis...@gmail.com wrote:
> >
> > 最近在修改一段老脚本,这段脚本需要同时在AIX和suse linux上运行,之前都使用ksh没有什么问题。但suse
> > linux升级到版本12后,发现其ksh下的local指令含义发生了变化,which
> > local无果。但用bash可行,所以在思考shell脚本中有没有办法通过#!解释指令,根据uname的结果来确定其具体的shell,但尝试几次后并没有结果。
> >
> > 目前的解决方案是使用#!/bin/sh,其在suse
> > linux下为bash的符号链接,AIX下为默认sh,运行暂时没有问题。但这个肯定是个麻烦,请问诸位有能什么好的方法吗?
> >

如果你严格按 POSIX 标准来的话,这个肯定不是麻烦而是方便。/bin/sh 一定是
POSIX 兼容 shell,而不是会 csh fish 之类的语法迥异的东西。

如果你想不同系统使用不同的解释器,可以先写段 POSIX shell 脚本判断并 exec
你想要的 shell。

--
Best regards,
lilydjwg

Linux Vim Python 我的博客:
http://lilydjwg.is-programmer.com/
--
A: Because it obfuscates the reading.
Q: Why is top posting so bad?
Balas ke semua
Balas ke penulis
Teruskan
0 pesan baru