What's new

How To Improve Shell Script Code Quality and Readability

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Xentrk

Part of the Furniture
If you use Asuswrt-Merlin, eventually you may have to write a Linux shell script. The User scripts Wiki covers basic information on how to create and troubleshoot scripts.

I recently started using the open source Atom editor on my Windows 10 device for the development of scripts used on Asuswrt-Merlin firmware. I recently learned of two packages that can assist users in the development of scripts and have written a guide on how to install and use the packages.

The guide will show you how to configure the Atom editor to improve shell script code quality and readability using the linter-shellcheck and format-shell packages in the Atom editor on a Windows 10 device. Because you are developing the code on your local machine, I also recommend that you install an SFTP client, such as WinSCP, on your Windows machine to easily transfer the code to the Linux operating system for testing.

Special thank you to @Adamm for pointing out the spellcheck package feature available in the Atom editor to me.
 
ShellCheck actually provide precompiled windows binaries now so you can skip the Linux subsystem requirement and bat file workaround, instead pointing directly to the binary.

https://shellcheck.storage.googleapis.com/index.html


There is also an additional step you need to take with a registry edit to be able to check files sourced from network drives, I’ll update this post with a link when I get home.
 
Symbols tree view is also nice for quick jumping between defined functions

I also love the Git integration so I can push commits to development branches without leaving the editor
 
ShellCheck actually provide precompiled windows binaries now so you can skip the Linux subsystem requirement and bat file workaround, instead pointing directly to the binary.

https://shellcheck.storage.googleapis.com/index.html


There is also an additional step you need to take with a registry edit to be able to check files sourced from network drives, I’ll update this post with a link when I get home.
Thanks @Adamm, I can update the guide after you send me the information.

The one feature that is missing in the Atom linter shellcheck package that is available in https://www.shellcheck.net/
is the ability to click on the error code and link to additional help on the error on the site
https://github.com/koalaman/shellcheck/wiki/SC2039

But the integration with Atom outweighs this minor issue for me.
 
Thanks @Adamm, I can update the guide after you send me the information.

Err, guess my memory wasn't as good as I hoped, can't remember the exact error I was getting prior with linter on network shares (or the relevant solution) :rolleyes:. I'll let you know if I stumble across it again.

The one feature that is missing in the Atom linter shellcheck package that is available in https://www.shellcheck.net/
is the ability to click on the error code and link to additional help on the error on the site
https://github.com/koalaman/shellcheck/wiki/SC2039

But the integration with Atom outweighs this minor issue for me.

You will find the website implementation has a major issues processing complex scripts and those over 1000 lines (Skynet for example just endlessly waits), so for larger projects a native implementation (atom/linux subsystem/travisci/codacy) is needed.

btw;

OgCGxJK.png
 
The guide has been updated to remove the Ubuntu and Windows batch file requirement. The new requirement is to download the precompiled binary file shellcheck binary file from https://shellcheck.storage.googleapis.com/index.html and enter the file name and path in the Shellcheck Executable Path field. The updated guide reduces many of the steps to setup and configure.
 
Since configuring linter-shellcheck, I get a pop up message in Atom stating

upload_2019-3-28_20-50-19.png


For now, I have been selecting the option to Disable Diagnostics. I will need to research more about the conflict.
 
Last edited:
Does Atom have an auto-save function when the app loses the focus?
I cannot code without that feature and NPP has it.
And who reads my code and cares as long as it works. I'm not saying that I have shoddy code or habits but I am reluctant to use a new editor after years and years of using NPP.
 
Trying to install linter-shellcheck but get error. Do I need Python and Visual Studio installed to ?

Code:
Installing “linter-shellcheck@1.6.0” failed.Hide output…
gyp info it worked if it ends with ok
gyp info using node-gyp@3.4.0
gyp info using node@8.9.3 | win32 | x64
gyp http GET https://atom.io/download/electron/v2.0.18/iojs-v2.0.18.tar.gz
gyp WARN install got an error, rolling back install
gyp ERR! install error
gyp ERR! stack Error: connect ETIMEDOUT 34.231.75.48:443
gyp ERR! stack     at Object._errnoException (util.js:1024:11)
gyp ERR! stack     at _exceptionWithHostPort (util.js:1046:20)
gyp ERR! stack     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "C:\\Users\\Johan\\AppData\\Local\\atom\\app-1.35.1\\resources\\app\\apm\\bin\\node.exe" "C:\\Users\\Johan\\AppData\\Local\\atom\\app-1.35.1\\resources\\app\\apm\\node_modules\\node-gyp\\bin\\node-gyp.js" "install" "--runtime=electron" "--target=2.0.18" "--dist-url=https://atom.io/download/electron" "--arch=x64" "--ensure"
gyp ERR! cwd C:\Users\Johan\.atom
gyp ERR! node -v v8.9.3
gyp ERR! node-gyp -v v3.4.0
gyp ERR! not ok

Compiler tools not found
Packages that depend on modules that contain C/C++ code will fail to install.
Read here for instructions on installing Python and Visual Studio.
Run apm install --check after installing to test compiling a native module.
 
Trying to install linter-shellcheck but get error. Do I need Python and Visual Studio installed to ?

Code:
Installing “linter-shellcheck@1.6.0” failed.Hide output…
gyp info it worked if it ends with ok
gyp info using node-gyp@3.4.0
gyp info using node@8.9.3 | win32 | x64
gyp http GET https://atom.io/download/electron/v2.0.18/iojs-v2.0.18.tar.gz
gyp WARN install got an error, rolling back install
gyp ERR! install error
gyp ERR! stack Error: connect ETIMEDOUT 34.231.75.48:443
gyp ERR! stack     at Object._errnoException (util.js:1024:11)
gyp ERR! stack     at _exceptionWithHostPort (util.js:1046:20)
gyp ERR! stack     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "C:\\Users\\Johan\\AppData\\Local\\atom\\app-1.35.1\\resources\\app\\apm\\bin\\node.exe" "C:\\Users\\Johan\\AppData\\Local\\atom\\app-1.35.1\\resources\\app\\apm\\node_modules\\node-gyp\\bin\\node-gyp.js" "install" "--runtime=electron" "--target=2.0.18" "--dist-url=https://atom.io/download/electron" "--arch=x64" "--ensure"
gyp ERR! cwd C:\Users\Johan\.atom
gyp ERR! node -v v8.9.3
gyp ERR! node-gyp -v v3.4.0
gyp ERR! not ok

Compiler tools not found
Packages that depend on modules that contain C/C++ code will fail to install.
Read here for instructions on installing Python and Visual Studio.
Run apm install --check after installing to test compiling a native module.
Ok found problem, have to turn off vpn to get package update/downloading.
 
Does Atom have an auto-save function when the app loses the focus?
I cannot code without that feature and NPP has it.
And who reads my code and cares as long as it works. I'm not saying that I have shoddy code or habits but I am reluctant to use a new editor after years and years of using NPP.
Good question. Looks like Atom does have the feature in the package autosave: https://atom.io/packages/autosave.

I just checked my installation and the package was already installed but the option to enable autosave was not checked.
  • From the menu, Packages -> Settings View -> Manage Packages.
  • Then, do a search for "autosave".
  • Click on the Settings button so the Settings screen will appear.
  • In the Settings screen, scroll down to the Settings and check the "Enabled" box.


upload_2019-3-29_7-13-53.png
 
Last edited:
I'm not saying that I have shoddy code or habits but I am reluctant to use a new editor after years and years of using NPP.

I was reluctant at first to swap, but once I got things the way I like them (most N++ functionality can be replicated via plugins) I feel like it became a much more productive editor. Simple things like a project list makes life much easier.

i6thGrJ.png


Some other useful plugins;

file icons
file watcher
find-and-replace-with-prev
force-line-endings
split-diff
symbols-tree-view
 
Last edited:
Atom does seem to be more extensible than np++, but it's a lot closer to a full ide than a quick and dirty text editor which is where np++ shines. It's also pretty easy to set up shellcheck in np++, there's two plugins available in the stock plugin manager.

Using the linter plugin:
If you installed with scoop, shellcheck will be in PATH.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<NotepadPlus>
  <linter extension=".sh" command="shellcheck -f checkstyle"/>
</NotepadPlus>
0uJCQ9e.png

Or the exec plugin:
My settings with "No internal messages" checked and gcc style output.

Execute...
Code:
nppexec:text_saveto "$(SYS.TEMP)$(NAME_PART).npp.tmp":ANSI
shellcheck -f gcc "$(SYS.TEMP)$(NAME_PART).npp.tmp"
cmd /c del "$(SYS.TEMP)$(NAME_PART).npp.tmp"
Console output filter - replace (might as well check the match case box)
Code:
$(SYS.TEMP)$(NAME_PART).npp.tmp
$(FULL_CURRENT_PATH)
Console output filter - highlight (pick whatever colours you like, I went with 27AE60, 8E44AD, E67E22, E74C3C)
Code:
%ABSFILE%:%LINE%:%CHAR%: style: * [SC????]
%ABSFILE%:%LINE%:%CHAR%: note: * [SC????]
%ABSFILE%:%LINE%:%CHAR%: warning: * [SC????]
%ABSFILE%:%LINE%:%CHAR%: error: * [SC????]
KoQqzII.png
 
Last edited:

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top