shell和PowerShell中运行的脚本

shell和PowerShell中运行的脚本

本文介绍了是否可以编写一个可以在bash/shell和PowerShell中运行的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个集成脚本来设置一些环境变量,使用wget下载文件并运行它.

I need to create ONE integrated script that sets some environment variables, downloads a file using wget and runs it.

挑战在于,它必须是既可以在Windows PowerShell上运行,又可以在bash/shell上运行的SAME脚本.

The challenge is that it needs to be the SAME script that can run on both Windows PowerShell and also bash / shell.

这是shell脚本:

#!/bin/bash
# download a script
wget http://www.example.org/my.script -O my.script
# set a couple of environment variables
export script_source=http://www.example.org
export some_value=floob
# now execute the downloaded script
bash ./my.script

在PowerShell中这是同一件事:

This is the same thing in PowerShell:

wget http://www.example.org/my.script -O my.script.ps1
$env:script_source="http://www.example.org"
$env:some_value="floob"
PowerShell -File ./my.script.ps1

所以我想知道这两个脚本是否可以合并并在任何一个平台上成功运行?

So I wonder if somehow these two scripts can be merged and run successfully on either platform?

我一直在尝试找到一种将它们放在同一脚本中并使bash和PowerShell.exe忽略错误的方法,但是这样做没有成功.

I've been trying to find a way to put them in the same script and get bash and PowerShell.exe to ignore errors but have had no success doing so.

有什么猜想吗?

推荐答案

这是可能的;我不知道它的兼容性如何,但是PowerShell将字符串视为文本,然后将它们显示在屏幕上,Bash将它们视为命令并尝试运行它们,并且两者都支持相同的函数定义语法.因此,将函数名称放在引号中,只有Bash将运行它,将退出"放入引号中,只有Bash将退出.然后再编写PowerShell代码.

It is possible; I don't know how compatible this is, but PowerShell treats strings as text and they end up on screen, Bash treats them as commands and tries to run them, and both support the same function definition syntax. So, put a function name in quotes and only Bash will run it, put "exit" in quotes and only Bash will exit. Then write PowerShell code after.

NB.之所以可行,是因为两个Shell中的语法重叠,并且您的脚本很简单-运行命令并处理变量.如果您尝试对其中一种语言使用更高级的脚本(如果/然后,用于,切换,区分大小写等),则另一种可能会抱怨.

NB. this works because the syntax in both shells overlaps, and your script is simple - run commands and deal with variables. If you try to use more advanced script (if/then, for, switch, case, etc.) for either language, the other one will probably complain.

将其另存为dual.ps1,以便PowerShell对其满意; chmod +x dual.ps1,以便Bash可以运行它

Save this as dual.ps1 so PowerShell is happy with it, chmod +x dual.ps1 so Bash will run it

#!/bin/bash

function DoBashThings {
    wget http://www.example.org/my.script -O my.script
    # set a couple of environment variables
    export script_source=http://www.example.org
    export some_value=floob
    # now execute the downloaded script
    bash ./my.script
}

"DoBashThings"  # This runs the bash script, in PS it's just a string
"exit"          # This quits the bash version, in PS it's just a string


# PowerShell code here
# --------------------
Invoke-WebRequest "http://www.example.org/my.script.ps1" -OutFile my.script.ps1
$env:script_source="http://www.example.org"
$env:some_value="floob"
PowerShell -File ./my.script.ps1

然后

./dual.ps1

在任何一个系统上.

您可以通过以下方式添加更复杂的代码:用不同的前缀注释代码块,然后让每种语言过滤出自己的代码并eval(通常,安全性警告适用于eval),例如这种方法(结合哈里·约翰斯顿的建议):

You can include more complex code by commenting the code blocks with a distinct prefix, then having each language filter out its own code and eval it (usual security caveats apply with eval), e.g. with this approach (incorporating suggestion from Harry Johnston ):

#!/bin/bash

#posh $num = 200
#posh if (150 -lt $num) {
#posh   write-host "PowerShell here"
#posh }

#bash thing="xyz"
#bash if [ "$thing" = "xyz" ]
#bash then
#bash echo "Bash here"
#bash fi

function RunBashStuff {
    eval "$(grep '^#bash' $0 | sed -e 's/^#bash //')"
}

"RunBashStuff"
"exit"

((Get-Content $MyInvocation.MyCommand.Source) -match '^#posh' -replace '^#posh ') -join "`n" | Invoke-Expression

这篇关于是否可以编写一个可以在bash/shell和PowerShell中运行的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:15