Format your scripts with shfmt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format your scripts with shfmt
# 1  
Old 09-08-2016
Power Format your scripts with shfmt

I've been working on a tool to format (style) shell programs, much like gofmt for Go. After much testing and personal use, I'm throwing it out there to see if it's useful to anyone else.

GitHub - mvdan/sh: A shell parser and formatter in Go

You'll need golang to build and install it. If this is an issue for anyone, I can upload cross compiled binaries somewhere.

The style itself is influenced both by what's most used in the wild and Google's shell style:

https://google.github.io/styleguide/...xml#Formatting

Feedback appreciated, and of course feel free to report any issues.

Last edited by mvdan; 09-08-2016 at 11:08 AM.. Reason: links
# 2  
Old 09-08-2016
I will try to give you program a test run but lacking go it will take me a few days.

Quote:
Originally Posted by mvdan
The style itself is influenced both by what's most used in the wild and Google's shell style:

https://google.github.io/styleguide/...xml#Formatting
To be honest i haven't noticed this styleguide at all until today. Several things in there are IMHO rather questionable and my suggestion is that you shouldn't rely on that at all. Here are my main objections:
  • Bash is the only shell scripting language permitted for executables.
    Nonsense! Many applications prescribe a certain shell (SAP for instance enforces csh for system users) and to maintain a supported system you have to respect that, even if (conceivably) unhappy about it. Other systems (like the AIX i work on) use ksh, which IMHO is even better suited to shell programming than bash. But that is my opinion and to say "use this shell" or "use that shell" is like saying "only walk with your left foot" - ridiculous.
  • Shell should only be used for small utilities or simple wrapper scripts.
    ROFLMAO! Some of the scripts i maintain are several thousand lines long and contain calls to a shell library of functions which also is several thousand lines of code worth.
  • [[ ... ]] is preferred over [, test and /usr/bin/[.
    It is exactly the other way round: use "[ ... ]" instead of "[[ ... ]]" if you want to write portable scripts.
  • Indent 2 spaces. No tabs.
    I can understand the "no tabs", but 2 spaces is uncomfortable for some. I use 5 spaces, because it makes indentation standing out clearly (IMHO), but again: this is personal preference. A reformatter should be configurable in this regard. The same goes for:
  • Put ; do and ; then on the same line as the while, for or if.
    I like that style better than putting do/then in the next line, but that also is personal preference! It should be configurable in a reformatter.
  • Use process substitution or for loops in preference to piping to while.
    This takes the biscuit! Consider for foo in $(command) ; do: if command produces too many output words your foor-loop will crash with a "too many arguments" while the while-loop with the pipeline has no such limitations.
  • Naming Conventions
    This is just one more way to do things in a somewhat consistent way. Personally i am a fan of Hungarian-style Notation, which i use in a variation suited to shell programming: "ch" as prefix for strings (char), "i" for integers, "f" for file-/directory names, "p" (procedure) for local functions, "f_" for library functions, etc. Any system will do, as long as you consistently use it. Personally i also find Mixed case to be easier to read and write than the underscore-separated words. If pReadAttributeFromItem() or read_attribute_from_item() is better is everybodies guess, but i think it is personal preference.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 09-08-2016
Note that nearly all of the items you're commenting on do not concern formatting at all - like limiting to bash, what scripts should be used for or [[ over [.

Some comments:

* shfmt (the parser) works with bash, which should work just as fine for POSIX shell scripts
* shfmt supports indentation with tabs or any number of spaces (see README)
* It will not replace [ with [[. It does not modify any code - it just formats. Same goes for process substitution in for loops. And for naming conventions.

Regarding spacings and newlines being configurable - I'm trying to keep the number of options to a minimum. The way it keeps "do" and "then" on the same line is because that's what is most common and also found in the most popular style guides.
# 4  
Old 09-08-2016
Hi bakunin...
Quote:
Shell should only be used for small utilities or simple wrapper scripts.

ROFLMAO! Some of the scripts i maintain are several thousand lines long and contain calls to a shell library of functions which also is several thousand lines of code worth.
Ha ha, I agree.
There is at least one large script on here, URL below. <wink>

Hi mvdan...

You might like to test your formatter with this:-

The Start Of A Simple Audio Scope Shell Script...

And have some fun...
This User Gave Thanks to wisecracker For This Post:
# 5  
Old 09-09-2016
wisecracker, that fails right now because I wasn't aware of $[. Apparently, it's a very old and deprecated form of $((:

https://unix.stackexchange.com/questions/209833/what-does-a-dollar-sign-followed-by-a-square-bracket-mean-in-bash

In any case, will add support for it now.
# 6  
Old 09-09-2016
Quote:
Originally Posted by mvdan
wisecracker, that fails right now because I wasn't aware of $[. Apparently, it's a very old and deprecated form of $((:

arithmetic - What does a dollar sign followed by a square bracket mean in bash? - Unix & Linux Stack Exchange

In any case, will add support for it now.
First bug? <wink>

Inside the code are nothing but tabs for indentation.
Why? - Because my tab is equivalent of 8 whitespaces and just imagine the size of the file with all of those tabs replaced by useless whitespaces.
Also it contains for - do - done on seperate lines.
Also it contains while - do - done and others on seperate lines.
Also it contains if - then - else - fi and derivatives on seperate lines.
Also it contains somefunction() - { - } on seperate lines.
Also it contains ` somecommand ` , also deprecated.
Although items might be deprecated does not mean they are not in use - be aware.

EDIT:-
I forgot to add that this script works on current bash versions on various platforms along with CygWin too.
So replace the word 'deprecated' with 'hidden' until it is completely removed from the bash shell which I suspect will not be for a long while yet...

Last edited by wisecracker; 09-09-2016 at 08:12 AM.. Reason: See above.
# 7  
Old 09-09-2016
Quote:
Originally Posted by wisecracker
Inside the code are nothing but tabs for indentation.
Why? - Because my tab is equivalent of 8 whitespaces and just imagine the size of the file with all of those tabs replaced by useless whitespaces.
Also it contains for - do - done on seperate lines.
Also it contains while - do - done and others on seperate lines.
Also it contains if - then - else - fi and derivatives on seperate lines.
Also it contains somefunction() - { - } on seperate lines.
None of this matters. The original format is of little significance, since the program is written to change it.

Quote:
Originally Posted by wisecracker
Also it contains ` somecommand ` , also deprecated.
Although items might be deprecated does not mean they are not in use - be aware.

EDIT:-
I forgot to add that this script works on current bash versions on various platforms along with CygWin too.
So replace the word 'deprecated' with 'hidden' until it is completely removed from the bash shell which I suspect will not be for a long while yet...
Deprecated means "don't use, but still supported". I doubt they will ever remove support for these, as that would mean a breaking change.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling multiple scripts from another scripts

Dear all, I am working on script which call other shell scripts in a loop but problem is from second script am not able to come out. Here is the snippet:- #!/bin/bash HSFILE=/root/Test/Components.txt LOGFile=/opt/domain/AdminDomain/application/logs... (3 Replies)
Discussion started by: sharsour
3 Replies

2. UNIX for Dummies Questions & Answers

converting scripts from dos 2 unix format

Hi, I am new to shell scripting and exploring it , I have developed few sample shell script but I have developed them on windows xp notepad and then saving them on folder and then testing them on cywgin and running perfectly...but these scripts are in dos format and I want to convert them in unix... (1 Reply)
Discussion started by: rahul125
1 Replies

3. Shell Programming and Scripting

KSH - How to call different scripts from master scripts based on a column in an Oracle table

Dear Members, I have a table REQUESTS in Oracle which has an attribute REQUEST_ACTION. The entries in REQUEST_ACTION are like, ME, MD, ND, NE etc. I would like to create a script which will will call other scripts based on the request action. Can we directly read from the REQUEST_ACTION... (2 Replies)
Discussion started by: Yoodit
2 Replies

4. UNIX for Dummies Questions & Answers

Shell Scripts - shows today’s date and time in a better format than ‘date’ (Uses positional paramete

Hello, I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Discussion started by: citizencro
3 Replies

5. Shell Programming and Scripting

Shell scripts for record Re format

I have few files from windows which are tab delimited or ‘|' delimited files. I need to convert these files without any delimiter ( so in a way it would become variable length with no delimiter ) Can someone help me with the command in bourne shell scripts., ( I am trying with awk ) Thanks In... (6 Replies)
Discussion started by: Shanks
6 Replies

6. Shell Programming and Scripting

Changing the Bash Scripts to Bourne Scripts:URGENT

Hi, I have to write a program to compute the checksums of files ./script.sh I wrote the program using bash and it took me forever since I am a beginner but it works very well. I'm getting so close to the deadline and I realised today that actually I have to use normal Bourne shell... (3 Replies)
Discussion started by: pgarg1989
3 Replies

7. Shell Programming and Scripting

Help with Script using rsh and scripts within scripts

Hi, I've written a script that runs on a Database server. It has to shutdown the Application server, do an Oracle Dump and then restart the Application server. Its been a long time since I wrote any shells scripts. Can you tell me if the scripts that I execute within my script will be executed... (3 Replies)
Discussion started by: brockwile1
3 Replies

8. UNIX for Dummies Questions & Answers

Profile scripts versus rc scripts....

what is the difference between login and profile scripts versus the rc scripts? (1 Reply)
Discussion started by: rookie22
1 Replies

9. Shell Programming and Scripting

Want to Convert Scripts in Linux format

I have scripts which I want to convert in Linux format. Note these scripts are in txt format.But I want to convert them in Linux, as DBA's will be using this script. Any command or utility which converts tht files in proper Linux format. Thanks in Adavce. Kunal (1 Reply)
Discussion started by: niceboykunal123
1 Replies
Login or Register to Ask a Question