Shell files (*.sh) to batch file (*.bat) converter?
I seriously doubt it. Precisely because it is so tedious to do, or more accurately because of the reason why it is tedious: the systems, let alone the software involved, are completely different.
It's not just a matter of adding or removing a #!
line at the top. Take this line from a shell script I've written:
STREAM_FROM=$($ZFS list -t snapshot ${BACKUP_TARGET_FS} -H -o name -r | grep '@backup_' | tail -n 1 | cut -d '@' -f 2)
(Yes, there's probably a more efficient way to do it than a handful of pipes, but that's beside the point; the script in question doesn't need top-notch performance.) This is variable assignment, subshell expression expansion, command invocation by environment variable expansion, environment variable expansion, piping, grep
, tail
and cut
all rolled into one statement. In order to accurately convert even this one statement, you need to know how all of that works and the exact semantics of the various options to the invoked standard utilities, and how to recreate that behavior on the target system.
Suppose you have the ZFS utilities installed on both systems (so we don't have to worry about that part). How are you going to, automatically, convert this to a Windows batch file statement or set of statements? I'd argue that unless you can do something akin to solving the halting problem, it isn't possible to write a generic converter such as the one you are clearly looking for. Unlike with natural language translation, with computer programs behavior "close" to that of the original just isn't anywhere near good enough: anything short of perfection is a bug.
Or take a scan job I did the other day (admittedly not a script, but it could just as easily have been; I just ended up typing it into a terminal because it was a one-off thing); I ended up with something not unlike the following:
for n in $(seq 10 66); do scanimage --button-controlled=yes -x 180 -y 200 --mode Gray --format=tiff > scan${n}.tmp && convert scan${n}.tmp scan${n}.png && \rm scan${n}.tmp; done
This is a complicated example because the --button-controlled option is provided by the backend, not by scanimage
(SANE) itself. So a tool to convert this one-liner would need to know about every option provided by every SANE backend and their counterparts on the target system; clearly not feasible, and perhaps not even possible.
Even a truly simple shell script that only iterates over a set of files is unlikely to be possible to convert automatically. And such simple scripts are few and far in between.
Also, "shell scripts" is not something homogenous: what's needed to convert a bash shell script is different from what's needed to convert a zsh shell script is different from what's needed to convert a shell script written in Perl. In many cases, there might not even exist direct counterparts to various syntaxes, further complicating translation.
Related videos on Youtube
stack
Updated on September 18, 2022Comments
-
stack over 1 year
It's quiet tedious to convert shell scripts into batch files. Is there a way to automatically and easily perform .sh to .bat conversion or vice versa?
-
stack about 10 yearsThanks a lot Michael Kjörling. Your answer is really most illuminating.
-
asmeurer over 9 yearsJust because you can't do something in full generality doesn't mean you can't do it at all.
-
stack over 9 years@asmeurer Agree with you on that, probably there's no available software or technology that solves the case yet.
-
user over 9 years@asmeurer The OP didn't ask about a specific case, so the only reasonable (to me) way to interpret the question was to mean the general case. If you feel you have a better answer, by all means do post it. That the question is old doesn't matter; in fact, that's what the Revival and Necromancer badges are all about.