diff --git a/bin/runtime.cmd b/bin/runtime.cmd index 6b1a49d3..3c226f7e 100644 --- a/bin/runtime.cmd +++ b/bin/runtime.cmd @@ -512,10 +512,10 @@ if {[::punk::multishell::is_main]} { # -- --- --- --- --- --- --- --- --- --- --- --- --- ---end Tcl Payload # end hide from unix shells \ HEREDOC1B_HIDE_FROM_BASH_AND_SH -# csh/tcsh/sh/bash use oldschool backticks and sed lowest common denominator \ -echo "script: `echo $0 | sed 's/^-//'`" -# csh/tcsh/sh/bash use oldschool backticks and sed lowest common denominator \ -echo "shell: " `ps -p $$ | awk '$1 != "PID" {print $(NF)}' | tr -d '()' | sed -E 's/^.*\/|^-//'` +#Be wary of any non-trivial sed/awk etc - can be brittle to maintain across linux,freebsd,macosx due to differing implementations +#echo "script: `echo $0 | sed 's/^-//'`" +# csh/tcsh/sh/bash use oldschool backticks and sed - lowest common denominator \ +#echo "shell: " `ps -p $$ | awk '$1 != "PID" {print $(NF)}' | tr -d '()' | sed -E 's/^.*\/|^-//'` #csh/tcsh diversion \ test "$argv[*]" != "[*]" && ( /usr/bin/env bash $argv[*]; exit ) #other non-bash diversion \ @@ -525,7 +525,7 @@ test `ps -p $$ | awk '$1 != "PID" {print $(NF)}' | tr -d '()' | sed -E 's/^.*\/| # sh/bash \ shift && set -- "${@:1:$#-1}" -#echo "shell:" `ps -o args= $$ | sed -E 's/^.*\/|^-//' | awk '{print $1}'` +echo "shell:" `ps -o args= $$ | sed -E 's/^.*\/|^-//' | awk '{print $1}'` #------------------------------------------------------ # -- This if block only needed if Tcl didn't exit or return above. if false==false # else { @@ -568,28 +568,49 @@ else #os="$OSTYPE" os="other" fi -echo ostype: $OSTYPE -shellconfigline=$( sed -n "/: <>/{:a;n;/: <>/q;p;ba}" "$0" | grep $os) -#echo $shellconfigline; -if [[ $shellconfigline == *"nextshelltype"* ]]; then - echo "found config for os $os" - split1="${shellconfigline#*=}" #remove everything through the first '=' - #echo "split1: $split1" - pathraw="${split1%%\"*}" #take everything before the quote - use %% to get longest match - pathraw="${pathraw//\"/}" #remove quote - nextshellpath="${pathraw/%_*/}" #remove trailing underscores (% = must match at end) - #echo "nextshellpath: $nextshellpath" - split2="${split1#*=}" - #echo "split2: $split2" - split2="${split2//\"/}" - nextshelltype="${split2/%_*/}" - echo "nextshelltype: $nextshelltype" -else - echo "unable to find config for os $os" - echo "shellconfigline: $shellconfigline" - nextshellpath="" - nextshelltype="" -fi +#echo ostype: $OSTYPE +## This is the sort of sed that will not work across implementations +## shellconfiglines=$( sed -n "/: <>/{:a;n;/: <>/q;p;ba}" "$0" | grep $os) +#awk tested on linux & freebsd +shellconfiglines=$( awk '/^:.*<>.*$/,/^:.*<>.*$/' "$0" | grep $os) +#echo $shellconfiglines; +readarray -t arr_oslines <<<"$shellconfiglines" +nextshellpath="" +nextshelltype="" +for ln in "${arr_oslines[@]}"; do + if [[ "$ln" == *"nextshellpath"* ]]; then + splitln="${ln#*=}" #remove everything through the first '=' + pathraw="${splitln%%\"*}" #take everything before the quote - use %% to get longest match + #remove trailing underscores (% means must match at end) + nextshellpath="${pathraw/%_*/}" + echo "nextshellpath: $nextshellpath" + elif [[ "$ln" == *"nextshelltype"* ]]; then + splitln="${ln#*=}" + typeraw="${splitln%%\"*}" + nextshelltype="${typeraw/%_*/}" + fi +done + +#if [[ $shellconfigline == *"nextshelltype"* ]]; then +# #echo "found config for os $os" +# split1="${shellconfigline#*=}" #remove everything through the first '=' +# #echo "split1: $split1" +# pathraw="${split1%%\"*}" #take everything before the quote - use %% to get longest match +# pathraw="${pathraw//\"/}" #remove quote +# nextshellpath="${pathraw/%_*/}" #remove trailing underscores (% = must match at end) +# #echo "nextshellpath: $nextshellpath" +# split2="${split1#*=}" +# #echo "split2: $split2" +# split2="${split2//\"/}" +# nextshelltype="${split2/%_*/}" +# echo "nextshelltype: $nextshelltype" +#else +# echo "unable to find config for os $os" +# echo "shellconfigline: $shellconfigline" +# nextshellpath="" +# nextshelltype="" +#fi + exitcode=0 #-- sh/bash launches nextscript here instead of shebang line at top if [[ "$nextshelltype" != "bash" && "$nextshelltype" != "none" ]]; then @@ -863,12 +884,14 @@ function GetDynamicParamDictionary { return $DynParamDictionary } } +# Example usage: # GetDynamicParamDictionary # - This can make it easier to share a single set of param definitions between functions # - sample usage #function ParameterDefinitions { # param( -# [Parameter(Mandatory)][string] $myargument +# [Parameter(Mandatory)][string] $myargument, +# [Parameter(ValueFromRemainingArguments)] $opts # ) #} #function psmain { @@ -879,10 +902,15 @@ function GetDynamicParamDictionary { # #called once with $PSBoundParameters dictionary # #can be used to validate arguments, or set a simpler variable name for access # switch ($PSBoundParameters.keys) { -# 'myargumentname' { +# 'myargument' { # Set-Variable -Name $_ -Value $PSBoundParameters."$_" # } -# #... +# 'opts' { +# write-warning "Unused parameters: $($PSBoundParameters.$_)" +# } +# Default { +# write-warning "Unhandled parameter -> [$($_)]" +# } # } # foreach ($boundparam in $PSBoundParameters.GetEnumerator()) { # #... @@ -890,7 +918,7 @@ function GetDynamicParamDictionary { # } # end { # #Main function logic -# Write-Host "myargumentname value is: $myargumentname" +# Write-Host "myargument value is: $myargument" # #myotherfunction @PSBoundParameters # } #}