Skip to content

Instantly share code, notes, and snippets.

@AtnNn
Last active December 17, 2019 13:44
Show Gist options
  • Save AtnNn/253689f31fcaf47a16a6d3fa4dad3336 to your computer and use it in GitHub Desktop.
Save AtnNn/253689f31fcaf47a16a6d3fa4dad3336 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eou pipefail
if [[ $# != 1 ]]; then
echo "Generates a shell script that reproduces the environment variables created by a batch file"
echo "Usage: $0 <path-to-batch-file>"
exit 1
fi
d=/tmp/bat-to-winbat-env-$$$RANDOM
# set -x
mkdir $d
cmd //c env > $d/before
cmd //c "$1 && echo CUT HERE && env" > $d/output
sed -n '/^CUT HERE/,$p' $d/output > $d/after
! diff -u $d/before $d/after > $d/diff
perl -ne '
if (m/^-PATH=(.*)/) { }
elsif (m/^\+(.*?)=([^\r\n]*)/) { print "export $1='"'"'$2'"'"'\n" }
elsif (m/^-[^-!]/) { print "error: unsupported removed variable: $_"; exit 1 }
' $d/diff || { echo Failed >&2; exit 1; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment