当前位置:网站首页>编写一个在bash / shell 和 PowerShell中均可运行的脚本
编写一个在bash / shell 和 PowerShell中均可运行的脚本
2022-07-26 09:37:00 【Dust_Evc】
任务:创建一个集成脚本来设置一些环境变量, 使用 wget 下载文件并运行它.
挑战在于它需要是可以在 Windows PowerShell 和 bash / shell 上运行的 SAME 脚本.
这是 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 中的情况相同:
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
所以我想知道这两个脚本是否可以合并并在任一平台上成功运行?
我一直在试图找到一种方法将它们放在同一个脚本中, 并让 bash 和 PowerShell.exe 忽略错误, 但没有成功.
我不知道这是多么兼容, 但 PowerShell 将字符串视为文本并最终显示在屏幕上, Bash 将它们视为命令并尝试运行它们, 并且两者都支持相同的函数定义语法. 因此, 将函数名称放在引号中, 只有 Bash 会运行它, 将 “exit” 放在引号中, 只有 Bash 才会退出. 然后编写 PowerShell 代码.
NB. 这是有效的, 因为两个 shell 中的语法重叠, 并且您的脚本很简单 – 运行命令并处理变量. 如果您尝试使用更高级的脚本 (if / then,for,switch,case 等) 用于任何一种语言, 另一种可能会抱怨.
将其保存为 dual.ps1, 以便 PowerShell 对它感到满意, chmod x dual.ps1 因此 Bash 将运行它
#!/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), 例如:采用这种方法 (纳入 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
边栏推荐
- Modern medicine in the era of "Internet +"
- el-table实现增加/删除行,某参数跟着变
- 系统安装Serv-U后IIS出错提示:HRESULT:0x80070020
- 青少年软件编程等级考试标准解读_二级
- Fiddler packet capturing tool for mobile packet capturing
- Custom password input box, no rounded corners
- 小程序纪录
- ie7设置overflow属性失效解决方法
- 2019 ICPC Asia Yinchuan regional (water problem solution)
- mysql5.7.25主从复制(单向)
猜你喜欢
keepalived 实现mysql自动故障切换
Fiddler抓包工具之移动端抓包
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
面试突击68:为什么 TCP 需要 3 次握手?
Node 内存溢出及V8垃圾回收机制
高斯消元求解矩阵的逆(gauss)
CSV data file settings of JMeter configuration components
小白搞一波深拷贝 浅拷贝
[MySQL] understand the important architecture of MySQL (I)
随机推荐
E. Two Small Strings
keepalived 实现mysql自动故障切换
The problem of accessing certsrv after configuring ADCs
Basic use of ArcGIS 4
Gauss elimination for solving XOR linear equations
Audio and video knowledge
图解用户登录验证流程,写得太好了!
Malloc failed to allocate space and did not return null
Mo team learning notes (I)
Redis sentinel mode setup under Windows
matlab simulink实现模糊pid对中央空调时延温度控制系统控制
微信小程序图片无法显示时显示默认图片
The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
MFC handy notes
添加dll
EOJ 2020 January race E-number transformation
高斯消元
matlab中的AR模型短时预测交通流
阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
Windows下Redis哨兵模式搭建