Tcl

DOSH - DOS or SH
Login

The same script to be run from either MS DOS or UNIX SHELL

https://stackoverflow.com/questions/17510688/single-script-to-run-in-both-windows-batch-and-linux-bash

Launch script from DOS or sh:

DOS CMD

  1. No slash -> DOS

    c:\fossil\nest\tcl>dosh5
        usage: dosh5 <firstWindowsArg> <secondWindowsArg>
    
  2. Backslash -> DOS

    c:\fossil\nest\tcl>.\dosh5
        usage: dosh5 <firstWindowsArg> <secondWindowsArg>
    
  3. Foreslash -> Error

    c:\fossil\nest\tcl>./dosh5
        '.' is not recognized as an internal or external command,
        operable program or batch file.
    

PowerShell

  1. Foreslash -> DOS

    PS C:\fossil\nest\tcl> ./dosh5
    usage: C:\fossil\nest\tcl\dosh5.bat <firstWindowsArg> <secondWindowsArg>
    
  2. Backslash -> DOS

    PS C:\fossil\nest\tcl> .\dosh5
        usage: C:\fossil\nest\tcl\dosh5.bat <firstWindowsArg> <secondWindowsArg>
    
  3. sh -> UNIX shell

    PS C:\fossil\nest\tcl> sh dosh5
        usage: dosh5 <firstUnixArg> <secondUnixArg>
    
  4. No prefix -> Error

    PS C:\fossil\nest\tcl> dosh5
    dosh5 : The term 'dosh5' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
    path is correct and try again.
    At line:1 char:1
    + dosh5
    + ~~~~~
        + CategoryInfo          : ObjectNotFound: (dosh5:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    Suggestion [3,General]: The command dosh5 was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type: ".\dosh5". See "get-help about_Command_Precedence" for more details.
    

Explorer (double click)

File extension must be .cmd. dosh5 does not work, dosh5.cmd does.

MSYS2

dosh5 is only detected as UNIX shell.
sh dosh5.cmd is detected as UNIX shell, while ./dosh5.cmd is detected as DOS CMD.

Distinguish between OS types

DOS:

Outputs one of:

echo %PROCESSOR_ARCHITECTURE% 
    AMD64  # 64-bit
    x86    # 32-bit

PowerShell:

echo $env:PROCESSOR_ARCHITECTURE
    AMD64  # 64-bit
    x86    # 32-bit

PowerShell/MSYS2/MINGW64/Cygwin/Linux/MacOs/BSD:

uname -a, uname -m

Detect a failing command

%%% TODO

Detect shell type

%%% TODO

Delete a file, ignore errors

DOS:

del "juvlist.json" >nul 2>&1

PowerShell:

rm .\juvlist.json -ErrorAction SilentlyContinue

MSYS2/MINGW64/Cygwin/Linux/BSD:

rm -f juvlist.json

Download a file

DOS/PowerShell:

bitsadmin /rawreturn /transfer /download /priority normal https://www.fossil-scm.org/home/juvlist c:\\fossil\\nest\tcl\juvlist.json

PowerShell:

$Response = Invoke-WebRequest -URI https://www.fossil-scm.org/home/juvlist -OutFile juvlist.json

PowerShell/MSYS2/MINGW64/Cygwin/Linux:

wget https://www.fossil-scm.org/home/juvlist -O juvlist.json
curl https://www.fossil-scm.org/home/juvlist -o juvlist.json

MacOS:

fetch https://www.fossil-scm.org/home/juvlist -o juvlist.json   # ALWAYS available
wget https://www.fossil-scm.org/home/juvlist -O juvlist.json    # MAY BE available
curl https://www.fossil-scm.org/home/juvlist -o juvlist.json    # MAY BE available

BSD:

fetch https://www.fossil-scm.org/home/juvlist -o juvlist.json   # ALWAYS available
wget https://www.fossil-scm.org/home/juvlist -O juvlist.json    # MAY BE available
curl https://www.fossil-scm.org/home/juvlist -o juvlist.json    # MAY BE available