trying to convert an old bat (batch) file of mine into sh (bash) yet there are a few things that I still have trouble finding the sh equivalent (for delims tokens
being one).
Most notably though, the pretty nifty parameter expansion found in bat :
https://ss64.com/nt/syntax-args.html
http://cplusplus.bordoon.com/cmd_exe_variables.html
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:1 - searches the directories listed in the PATH
environment variable and expands %1 to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string.
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:1 - searches the directories listed in the PATH
environment variable for %1 and expands to the
drive letter and path of the first one found
(but this would work only in called functions and
only for numbered variables)
%~ftzaI - expands %I to a DIR like output line
Hence for C:\Users\dkoch\Downloads\test.bat
you get :
%~0 : test.bat
%~f0 : C:\Users\dkoch\Downloads\test.bat
%~d0 : C:
%~p0 : \Users\dkoch\Downloads\
%~n0 : test
%~x0 : .bat
%~s0 : C:\Users\dkoch\DOWNLO~1\test.bat
%~a0 : --a--------
%~t0 : 14/09/2021 17:58
%~z0 : 351
%~$PATH:1 :
%~dp0 : C:\Users\dkoch\Downloads\
%~nx0 : test.bat
%~fs0 : C:\Users\dkoch\DOWNLO~1\test.bat
%~dp$PATH:1 :
%~ftza0 : --a-------- 14/09/2021 17:58 351 C:\Users\dkoch\Downloads\test.bat
I often use something like %~f1
to try expanding the 1st parameter into a full path if it can. Otherwise the parameter is left untouched and passed as-is.
Is there something as convenient in sh, like $~f1
?
All what I've found so far is variable substring extraction/substitution :
http://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Expansions
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
https://linuxhint.com/bash_parameter_expansion/
https://wiki.bash-hackers.org/syntax/pe
Not really that helpful for what I'm trying to do. Your pick ?
Regards.