Skip to content

Commit

Permalink
launch-nw: Fix so it can be used as a symlink target
Browse files Browse the repository at this point in the history
This was its intended use--did I even test this??
  • Loading branch information
caldwell committed Apr 4, 2023
1 parent fce108f commit d806d0c
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion launch-nw
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh
${0%/*}/Emacs -nw "$@"
orig=`readlink $0`
: ${orig:=$0} # Otherwise you can't run it directly
${orig%/*}/Emacs -nw "$@"

This comment has been minimized.

Copy link
@marc-hb

marc-hb Feb 9, 2024

@caldwell this script is not whitespace-compatible. Easily found and fixed with shellcheck. Please make all scripts shellcheck-clean, shellcheck is really a no-brainer.

bash -x /Applications/Emacs\ 29.2.app/Contents/MacOS/emacs-nw
++ readlink /Applications/Emacs 29.2.app/Contents/MacOS/emacs-nw
+ orig=
+ : /Applications/Emacs 29.2.app/Contents/MacOS/emacs-nw
+ /Applications/Emacs 29.2.app/Contents/MacOS/Emacs -nw
/Applications/Emacs 29.2.app/Contents/MacOS/emacs-nw: line 4: /Applications/Emacs: No such file or directory

PS: thanks for Emacs on mac!

shellcheck /Applications/Emacs\ 29.2.app/Contents/MacOS/emacs-nw

In /Applications/Emacs 29.2.app/Contents/MacOS/emacs-nw line 2:
orig=`readlink $0`
     ^-----------^ SC2006 (style): Use $(...) notation instead of legacy backticks `...`.
               ^-- SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: 
orig=$(readlink "$0")


In /Applications/Emacs 29.2.app/Contents/MacOS/emacs-nw line 3:
: ${orig:=$0} # Otherwise you can't run it directly
  ^---------^ SC2223 (info): This default assignment may cause DoS due to globbing. Quote it.


In /Applications/Emacs 29.2.app/Contents/MacOS/emacs-nw line 4:
${orig%/*}/Emacs -nw "$@"
^--------^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: 
"${orig%/*}"/Emacs -nw "$@"

For more information:
  https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
  https://www.shellcheck.net/wiki/SC2223 -- This default assignment may cause...
  https://www.shellcheck.net/wiki/SC2006 -- Use $(...) notation instead of le...

This comment has been minimized.

Copy link
@caldwell

caldwell Feb 10, 2024

Author Owner

I've never heard of shellcheck!
OMG it hates every line of my script 🤣.
You're right I forgot the quotes on stuff. Not sure the middle line thing matters. The best "flaw" I could come up with is:

$ mkdir '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * '
$ cp launch-nw '* *'*
$ bash -x '* *'*/launch-nw 2>&1 | wc
       5   22784  416707

That's pretty contrived though. I suppose it doesn't hurt to put quotes around it.

This comment has been minimized.

Copy link
@marc-hb

marc-hb Feb 10, 2024

OMG it hates every line of my script 🤣.

Thanks for trying it! You don't have to fix every warning immediately! But please do run it every time you're changing any shell script code, it's an absolute no brainer.

Shellcheck literally saved me hours and hours of coaching with these magic words "please run shellcheck" and even more time after I added it to CI. As linters and other static analyzers go, the rate of false positives is unbelievably low and you can silence (at your own risk!) these extremely rare cases with a one-line # shellcheck disable=SC1234 comment.

Another good thing to know: shellcheck --format=gcc myscript.sh will help most editors and IDEs parse the output and go straight to the corresponding line(s).

I suppose it doesn't hurt to put quotes around it.

That's right: "in doubt, just quote". Much much faster than thinking about it and trying to guess if/when it's actually necessary.

https://mywiki.wooledge.org/Quotes#I.27m_Too_Lazy_to_Read.2C_Just_Tell_Me_What_to_Do

Another "good practice" is set -e / errexit:
thesofproject/sof-test#312

But don't add set -e to any existing script without shellcheck analysis, otherwise it could exit in unexpected places.

For complex scripts (NOT like this one) you also want to combine set -e with trap exit_handler_func EXIT


# Use this file as a symlink target to easily create a "terminal only" emacs:
# ln -s /Applications/Emacs.app/Contents/MacOS/emacs-nw /usr/local/bin/emacs

0 comments on commit d806d0c

Please sign in to comment.