1

I need to write a script that would take two filenames as arguments, then remove all duplicate slashes from those and pass them into some other app, let's say vim.

I started with something like that but of course it doesn't work

#!/bin/bash
/usr/local/bin/vim $($1 | sed s#//*#/#g)
4
  • Remove duplicate slashes from file names or files? Commented Dec 16, 2014 at 1:23
  • ...and why would you care about removing duplicate slashes from the name? A filename starting with // may be given special treatment if the local OS or filesystem chooses to do so, but anywhere other than in the leading position extra slashes are harmless on any POSIX-compliant system. Commented Dec 16, 2014 at 1:25
  • 1
    Also, this code creates bugs when handed filenames with spaces. Commented Dec 16, 2014 at 1:26
  • the original problem is that SourceTree doesn't pass $LOCAL file when configured to use vim as differ, I noticed that for whatever reason it has double slash in the end, but removing slashes didn't solve my problem :( So SourceTree sends filepath that looks like that /var/folders/lk/p5gtdm514773pg4ldm5whf1w0000gn/T//lVM4Zx_build-server-task.coffee any other editor can open that, but vim just refuses Commented Dec 16, 2014 at 1:44

2 Answers 2

2

The pipe operator will take the output of one command and pass it to another command. In your case, $1 is a string value that you don't want to run as a command. Instead you can pass that value into sed with the string redirection operator <<<, as follows:

$(sed s#//*#/#g <<< $1)
Sign up to request clarification or add additional context in comments.

3 Comments

Is the <<< specific to Bash or is it available in a POSIX shell too?
@dreamlax, it's a bashism.
...the POSIX equivalent would be <<EOF, followed by a newline, then $1, then another newline, then EOF. Obviously, using <<< is shorter.
1

Give readlink a try:

/usr/local/bin/vim "$(readlink -m "$1")"

3 Comments

@Agzam, "doesn't work" is not enough information to actually understand or diagnose a problem. If you want to provide something helpful, use #!/bin/bash -x as the shebang for your script so you can show the command it actually runs, and how this differs from what you expect or desire.
@Agzam, ...the most likely reason this wouldn't work, by the way, would be being on an operating system with a non-GNU implementation of readlink. Did you specify your OS in the question? [Edit: Ahh, yes, you did -- Apple, which indeed uses a BSD readlink rather than a GNU one].
@Agzam: For files that already exist, this will work on OS X if you simply take out the -m; for files that don't exist, you'll need to find a different solution. Of course, you could also install GNU readlink in your PATH before the system one, and that would fix it too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.