Reputation: 1300
I have a script that usually works in linux, but I'm trying to make it work on windows as well.
The script basically calls some other programs, and then moves lots of files.
The problem is that all the pathing is done using normal slashes (/
), but when I try to run the script using cmder
, like sh path/to/script/script.sh param
it's failing to find stuff because it ends up mixing slashes: Unable to access jarfile C:\full\path\to\file/here/are/normal/slashes/javafile.jar
and so on.
Say, when I edited the java files to work with both OS pathing, I changed the concating of slashes to the use of java.nio.file.Paths.get
to get a correct path.
Is there a way to do something like that on bash?
f.e. if i have jarpath=../path/to/jar
, to something like jarpath=correct_os_path(../path/to/jar)
Upvotes: 1
Views: 1493
Reputation: 113834
To change the slashes to correct type for the OS, try:
fixpath() {
case "$OSTYPE" in
*(linux|darwin|bsd)*) echo "${1//\\/\/}" ;;
*win*) echo "${1//\//\\}" ;;
esac
}
The above a bash function called fixpath
. The function uses bash's OSTYPE
variable to determine the OS and then uses bash's pattern substitution to change slashes to backslashes or vice versa.
For example, when run on Linux:
$ fixpath '\a\b\c/d/e'
/a/b/c/d/e
In your use case:
jarpath="$(fixpath "../path/to/jar")"
On Unix, it is possible for a file or directory name to contain a literal \
. For the Unix-Windows conversion to work reliably, try to avoid such names.
Upvotes: 1