Last active
September 6, 2024 18:45
-
-
Save chrisfcarroll/636458b42793cd453a68b529ff53b469 to your computer and use it in GitHub Desktop.
Hold both bash and PowerShell code in a single script file and/or make a PowerShell.ps1 script executable on all platforms. For Windows, *nix, linux and macOs. See Comments below
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
` # \ | |
# PowerShell Param statement : every line must end in #\ except the last line must with <#\ | |
# And, you can't use backticks in this section #\ | |
param( [ValidateSet('A','B')]$tabCompletionWorksHere, #\ | |
[switch]$andHere #\ | |
) <#\ | |
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ` | |
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv | |
# Bash Start ------------------------------------------------------------ | |
scriptdir="`dirname "${BASH_SOURCE[0]}"`"; | |
echo BASH. Script is running from $scriptdir | |
# Bash End -------------------------------------------------------------- | |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
echo > /dev/null <<"out-null" ### | |
'@ | out-null | |
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv | |
# Powershell Start ----------------------------------------------------#> | |
$scriptdir=$PSScriptRoot | |
Write-host "Powershell. This script is running from $scriptdir." | |
# Powershell End ------------------------------------------------------- | |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
out-null | |
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv | |
# Both Bash and Powershell run the rest but with limited capabilities | |
echo "Some lines work in both bash and powershell." | |
echo "Powershell picked up the named parameter '$'tabCompletionWorksHere=$tabCompletionWorksHere '$'andHere=$andHere" | |
echo "Whilst bash got positional parameters \$1=$1 and \$2=$2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file has a bash section followed by a powershell section, | |
# as well as shared sections. | |
echo @' | |
' > /dev/null | |
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv | |
# Bash Start ----------------------------------------------------------- | |
scriptdir="`dirname "${BASH_SOURCE[0]}"`"; | |
echo BASH. Script is running from $scriptdir | |
# Bash End ------------------------------------------------------------- | |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
echo > /dev/null <<"out-null" ### | |
'@ | out-null | |
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv | |
# Powershell Start ----------------------------------------------------- | |
$scriptdir=$PSScriptRoot | |
"powershell. Script is running from $scriptdir" | |
# Powershell End ------------------------------------------------------- | |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
out-null | |
echo "Some lines work in both bash and powershell. Calculating scriptdir=$scriptdir, requires separate sections." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/pwsh | |
# A single Powershell file can be recognised as executable on all platforms: Windows, linux & macOs | |
@" | |
This file is PowerShell only, but it can run unmodified on Windows, linux & macOs | |
Windows recognises it as executable because of the the ps1 suffix. | |
linux and macOs recognises the #! header and so will pass the script to powershell to run. | |
linux and macOs do still require the execute bit set: chmod a+x *.ps1 | |
"@ | |
[System.Environment]::OSVersion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file has a powershell section first followed by the bash section, | |
# as well as shared sections. But it is more complicated. Putting the | |
# the bash section first, followed by powershell, is simpler. | |
# ---------------------------------------------------------------------- | |
echo @" | |
" > /dev/null ; echo > /dev/null <<"out-null" ### | |
"@ | out-null | |
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv | |
# Powershell Start ----------------------------------------------------- | |
$scriptdir=$PSScriptRoot | |
"powershell. Script is running from $scriptdir" | |
# Powershell End ------------------------------------------------------- | |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
out-null | |
echo @' | |
' > /dev/null | |
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv | |
# Bash Start ----------------------------------------------------------- | |
scriptdir="`dirname "${BASH_SOURCE[0]}"`"; | |
echo BASH. Script is running from $scriptdir | |
# Bash End ------------------------------------------------------------- | |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
echo > /dev/null <<out-null | |
'@ | out-null | |
out-null | |
# ------------------------------------------------------ | |
echo "Some lines work in both bash and powershell. Calculating scriptdir=$scriptdir, requires separate sections." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
chmod
—e.g.chmod a+x script.ps1
—to set the executable flag.to do this:
Bash-or-Powershell.ps1
orBash-or-PowerShell-with-Param.ps1
and write the duplicated environment setup for the two shells. The same dot-source syntax,. scriptthatsetsenvironmentvariables.ps1
then works in both shells.Powershell-executable-on-win-and-nix.ps1
template.bash
is an option of course. ButPowerShell
is 30 years newer thanbash
. It shows.