当前位置:网站首页>Write a script that can run in Bash / shell and PowerShell
Write a script that can run in Bash / shell and PowerShell
2022-07-26 09:40:00 【Dust_ Evc】
Mission : Create an integration script to set some environment variables , Use wget Download the file and run it .
The challenge is that it needs to be able to Windows PowerShell and bash / shell Running on SAME Script .
This is a shell Script :
#!/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
This is related to PowerShell The situation is the same in :
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 want to know whether these two scripts can be merged and run successfully on either platform ?
I've been trying to find a way to put them in the same script , And let bash and PowerShell.exe Ignore mistakes , But it didn't work .
I don't know how compatible this is , but PowerShell Treat the string as text and finally display it on the screen , Bash Think of them as commands and try to run them , And both support the same function definition syntax . therefore , Put the function name in quotation marks , Only Bash Will run it , take “exit” Put it in quotation marks , Only Bash You're going to quit . Then write PowerShell Code .
NB. It works , Because the two one. shell Syntax overlap in , And your script is simple – Run commands and process variables . If you try to use more advanced scripts (if / then,for,switch,case etc. ) For any language , Another possibility is to complain .
Save it as dual.ps1, In order to PowerShell Be satisfied with it , chmod x dual.ps1 therefore 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
then
./dual.ps1
Edit on any system : You can include more complex code by annotating code blocks with different prefixes , Then let each language filter out its own code and evaluate it ( The usual safety warnings apply to eval), for example : In this way ( Included in the Harry Johnston The advice of ):
#!/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
边栏推荐
- Great reward for interview questions
- Interview shock 68: why does TCP need three handshakes?
- 高斯消元求解矩阵的逆(gauss)
- Does volatile rely on the MESI protocol to solve the visibility problem? (top)
- Login module use case writing
- 音视频知识
- IIS website configuration
- Mo team learning notes (I)
- Fiddler packet capturing tool for mobile packet capturing
- [untitled]
猜你喜欢
Registration module use case writing
[MySQL] understand the important architecture of MySQL (I)
在Blazor 中自定义权限验证
正则表达式
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
asp. Net using redis cache
V-permission add permission
解决npm -v突然失效 无反应
Neural network and deep learning-6-support vector machine 1-pytorch
v-premission添加权限
随机推荐
The provincial government held a teleconference on safety precautions against high temperature weather across the province
添加dll
matlab simulink实现模糊pid对中央空调时延温度控制系统控制
Jmeter配置元件之CSV数据文件设置
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
Neural network and deep learning-6-support vector machine 1-pytorch
MySQL的逻辑架构
Login module use case writing
Interpretation of the standard of software programming level examination for teenagers_ second level
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
注册模块用例编写
面试题目大赏
QT随手笔记(六)——更新界面、截图、文件对话框
Double authentication of server and client
Does volatile rely on the MESI protocol to solve the visibility problem? (top)
简单行人重识别代码到88%准确率 郑哲东 准备工作
POJ 1012 Joseph
POJ 1012 Joseph
2022年中科磐云——服务器内部信息获取 解析flag
Logical architecture of MySQL