Duke Dougal Duke Dougal - 2 months ago 40
Bash Question

Is it possible to write one script that runs in bash/shell and PowerShell?

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

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

This is the 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 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?

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.

Any guesses?

Answer

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. 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.

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

then

./dual.ps1

on either system.


Edit: If you're willing to put up with an error ('eval : The term 'eval' is not recognized as the name of a cmdlet') in PowerShell, and using 'eval', you can include more complex code with this approach:

#!/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

eval '$(grep "^#bash" $0 | sed "s/^#bash //")'
"exit"
((Get-Content $MyInvocation.MyCommand.Source) -match '^#posh' -replace '^#posh ') -join "`n" | Invoke-Expression