I am writing a bash script to mount DFS Windows shares via cifs
. I have the main part working, but I am having trouble for when I need the user to enter the DFS path as a variable and convert the backslashes to forward slashes.
#!/bin/bash
FILE='\\edi3\welshch\test'
FILEPATH="$("$FILE" | sed -e 's/\\/\//gp')"
echo $FILEPATH
I had another script that used a command to find a filepath for AD home directories then piped to sed as per the part | sed -e 's/\\/\//gp
However this script above gives me;
./test.sh: line 10: \\edi3\welshch\test: command not found
Best Answer
Inside the command substitution you have
"$FILE" | sed -e 's/\\/\//gp'
, which the shell expands to (the equivalent of)'\\edi3\welshch\test' | sed -e 's/\\/\//gp'
. Since it's a command, the shell goes looking for a file called\\edi3\welshch\test
to run.You probably meant to use
echo "$FILE" | sed ...
to pass the contents ofFILE
tosed
via the pipe.Note that even that's not right, some versions of
echo
will process the backslashes as escape characters, messing things up. You'll needprintf "%s\n" "$FILE" | sed ...
for it to work in all shells. See: Why is printf better than echo?Also, note that the default behaviour of
sed
is to print the line after whatever operations it does. When you use/p
on thes///
command, it causes an additional print, so you get the result twice in the output.That said, since you're using Bash, you could just use the string replacement expansion:
gives the output
//edi3/welshch/test