当前位置:网站首页>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
边栏推荐
猜你喜欢
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
v-premission添加权限
A new paradigm of distributed deep learning programming: Global tensor
AR model in MATLAB for short-term traffic flow prediction
【荧光字效果】
Registration module use case writing
Xiaobai makes a wave of deep copy and shallow copy
小白搞一波深拷贝 浅拷贝
Neural network and deep learning-6-support vector machine 1-pytorch
Windows下Redis哨兵模式搭建
随机推荐
opencv 类的使用
(2) Hand eye calibration of face scanner and manipulator (eye out of hand: nine point calibration)
Process32First返回false,错误x信息24
The diagram of user login verification process is well written!
挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破
Drawing shadow error diagram with MATLAB
解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.
[Online deadlock analysis] by index_ Deadlock event caused by merge
The provincial government held a teleconference on safety precautions against high temperature weather across the province
Mo team learning summary (II)
阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
JS judge the data types object.prototype.tostring.call and typeof
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
Gauss elimination
Neural network and deep learning-6-support vector machine 1-pytorch
高斯消元求解异或线性方程组
解决npm -v突然失效 无反应
uni-app学习总结
Fiddler download and installation
2019 ICPC Asia Yinchuan Regional(水题题解)