1

In msys bash in Windows, I'd like to cd to the directory a (Windows native) .lnk links to. These are the standard Windows shortcuts. I want to be able to do this:

~ $ cdlnk programs.lnk
/c/Program\ Files/ $

I've come this far:

strings "$lnk" | grep -A 1 DATA | tail -n 1

gives me the path the shortcut links to. However, now I'm stuck. I can either

  1. Make an alias in .bashrc - but then I cannot use pipes or pass the parameter to a script, i.e. I cannot run above code.
  2. Make a script cdlnk.sh - but then I can only change its cd and not that of the calling shell.
3
  • 2
    You can stick your command into a function in a file that gets sourced, for example somwhere in .bashrc or a file that gets sourced in there. Commented Dec 19, 2018 at 14:58
  • Great! That works! Thank you. Commented Dec 20, 2018 at 8:01
  • Care to put your comment in an answer? Then I can select it and mark the question answered. Commented Jul 1, 2020 at 8:23

1 Answer 1

1

To both be able to process parameters and modify the current shell, you can use a shell function in a file that has to be sourced, for example .bashrc.

For the command you have, the function might look like this:

cdlnk() {
    strings "$1" | grep -A 1 DATA | tail -n 1
}

You could make this a little shorter by using sed instead of grep and tail:

cdlnk() {
    strings "$1" | sed -n '/DATA/{n;p;q;}'
}

where -n suppresses output, and on a line matching DATA, the commands are n (get next line into pattern buffer), p (print the line), and q (exit – no need to look at the rest of the file).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.