0

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.

Kochise
  • 504
  • 5
  • 10
  • Right now this question is asked in such a way that only people who know _both_ bash and bat can answer it. – Charles Duffy Sep 14 '21 at 15:39
  • Can you write it in a way that doesn't assume readers will know what `%~f1` means? – Charles Duffy Sep 14 '21 at 15:40
  • (...if it's just making a relative path into an absolute one, that's what `readlink -f` or `realpath` is for). – Charles Duffy Sep 14 '21 at 15:40
  • @CharlesDuffy I've put the links to the **bat** and **sh** explanations, that should be enough. And sure, the problem is to know both worlds. – Kochise Sep 14 '21 at 15:45
  • But why would you ask a question that _requires_ someone to know both worlds, when there are lots of folks here who know the bash world, and _you yourself_ already know the batch world adequately? Asking people to follow links to understand your question is not generally considered reasonable. – Charles Duffy Sep 14 '21 at 15:52
  • It's possible or even likely that what you want is identical to `"$(readlink -m -- "$1")"`, but to know that _with certainty_ would require more details than are presently available. – Charles Duffy Sep 14 '21 at 15:54
  • (btw, you're tagged `bash` but talking about `sh` in the question body; bash and sh are two different shells -- they're not fully interchangeable; even when they're both provided by the bash codebase, it turns off some features when started under the `sh` name). – Charles Duffy Sep 14 '21 at 15:58
  • @CharlesDuffy I ask because I know more about `bat` and that you have plenty of `sh` is concerning. Anyway, I added some examples in the OP so people with knowledge can figure it out. – Kochise Sep 14 '21 at 16:02
  • That's still not very helpful. Your original question already said "fully qualified path name" -- dumping more documentation talking about additional things without clarifying corner cases in the original description doesn't answer the questions that one line _doesn't_ describe. For example: How does it handle files that don't exist? How does it handle symlink traversals? How does it handle arguments that can't be parsed as valid filenames at all? – Charles Duffy Sep 14 '21 at 16:05
  • @CharlesDuffy Currently if it does at least what **bat** does is enough for me. If the file doesn't exists, it is treated as a string and passed as-is. If simlink, I guess full path would link to the original file. – Kochise Sep 14 '21 at 16:07
  • 1
    The point is that people-who-aren't-you don't know what "at least what bat does" _is_, so we can't tell what you're asking for. It sounds like _you_ don't completely know "at least what bat does" yourself, given the guesses in the comment above. – Charles Duffy Sep 14 '21 at 16:14
  • 1
    AFAIK, the answer by @Socowi should address your question fully, but if it doesn't, maybe you could/should comment on it describing how and why it doesn't? – Charles Duffy Sep 14 '21 at 16:14

1 Answers1

2

parameter expansion [...] Is there something as convenient in sh

No, instead of a built-in expansion, you have to rely on commands in most cases. For instance, instead of %~XYfile you would write something like

$(cmd-that-does-X "$file"; cmd-that-does-Y "$file")

For a translation between %~X… and cmd-that-does-X, search for »How to do X in sh«. All of the X that make sense on a Unix system should already have plenty of good answers.

But keep in mind that a 100% equivalent translation is usually not reasonable (e.g. Windows users expect case-insensitve paths, where Unix users expect case-sensitve paths) or simply impossible (e.g. Unix does not have drive letters or short names).

Example for %~f1

In a bash script, you would replace %~f1 by

"$(realpath -m "$1")"

or

"$(readlink -m "$1")"

Both are not entirely equivalent to %~f1, but they do what a Unix user would expect. %~f1 internally uses FindFirstFileW; thanks to @Mofi for pointing this out. If you really, really want to emulate the behavior of that, you'd at least (!) have to

  • expand Windows (!) wildcards (note that Windows and Unix use different wildcards)
  • in a case-insensitve manner (very unusual for Unix)
  • without resolving symbolic links
  • but still normalize ./ and ../.

Replacement function

If you wanted, you could write a function that translates %~... for you. Instead of %~abc1 you then would write something like $(winexp abc "$1").

However, I'd advise against this.

  • You probably don't need all expansions in a single script, so including this function into all of your scripts is unnecessary bloat.
  • At least to Unix users, winexp f "$1" is more cryptic / less readable than realpath -m "$1".
  • For combined expansions (e.g. %~XYZ…) there might be more efficient single commands (e.g. cmd-XYZ … instead of cmd-X …; cmd-Y …; cmd-Z …).
Socowi
  • 25,550
  • 3
  • 32
  • 54
  • 1
    @Squashman Thank you, I read as much. What I meant with *"not entirely sure"* is all the details, for instance case-sensitivity, wildcards, and (missing) extensions. – Socowi Sep 14 '21 at 15:47