Format your scripts with shfmt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format your scripts with shfmt
# 8  
Old 09-09-2016
Quote:
Line Length and Long Strings
▶ Maximum line length is 80 characters.
How will your parser cope with this line and others in that script you have to test with?:-
Code:
printf "%b" "\x52\x49\x46\x46\x24\x00\x01\x00\x57\x41\x56\x45\x66\x6d\x74\x20\x10\x00\x00\x00\x01\x00\x01\x00\x40\x1f\x00\x00\x40\x1f\x00\x00\x01\x00\x08\x00\x64\x61\x74\x61\x00\x00\x01\x00" > /tmp/binary.bin

This is also in octal format too in another script!
The binary is 44 bytes in size.
# 9  
Old 09-09-2016
wisecracker, you can split long lines like that with a backslash:

Code:
$ echo "asdf\
> qwertyuiop\
> qe7158905189"
asdfqwertyuiopqe7158905189

$

This does not actually add newlines to the resulting string, as you see.

I hope I don't have code formatting practices imposed on my scripts... in my experience enforced formatting practices usually make things less readable and harder to edit.
This User Gave Thanks to Corona688 For This Post:
# 10  
Old 09-09-2016
> How will your parser cope with this line and others in that script you have to test with?

The parser of course does not have problem with long lines.

The formatter does not do anything about long lines, as there is no good solution to split lines. This should be done by humans, if they wish to.

> in my experience enforced formatting practices usually make things less readable and harder to edit.

Less readable is subjective, and harder to edit is until you get used to it. In my opinion, the tradeoff is worth it once there are multiple people maintaining a software. Otherwise, you can easily get to a point where pieces of the software are written in wildly different formats.

Diffs become bigger than they need to be, merge conflicts on the rise, readability decreases as noone is happy, etc. Those are simply irrelevant for scripts you develop on your own.
This User Gave Thanks to mvdan For This Post:
# 11  
Old 09-09-2016
Quote:
Originally Posted by Corona688
wisecracker, you can split long lines like that with a backslash:

Code:
$ echo "asdf\
> qwertyuiop\
> qe7158905189"
asdfqwertyuiopqe7158905189

$

This does not actually add newlines to the resulting string, as you see.

I hope I don't have code formatting practices imposed on my scripts... in my experience enforced formatting practices usually make things less readable and harder to edit.
I am actually well aware of that except I was wondering if the parser would chop at exactly 80 characters of which case the could corrupt the data if in mid flight inside a 4 character '\x??' string.

I treid it and it does corrupt.
Code:
printf "%b" "\x52\x49\x46\x46\x24\x00\x01\x00\x57\x41\x56\x45\x66\x6d\x74\x20\\
x10\x00\x00\x00\x01\x00\x01\x00\x40\x1f\x00\x00\x40\x1f\x00\x00\x01\x00\x08\x0\
0\x64\x61\x74\x61\x00\x00\x01\x00" > /tmp/binary

Results:-
Code:
Last login: Fri Sep  9 19:22:55 on ttys000
AMIGA:barrywalker~> cd Desktop/code/Shell
AMIGA:barrywalker~/Desktop/code/Shell> chmod 755 80charperline.sh
AMIGA:barrywalker~/Desktop/code/Shell> ./80charperline.sh
AMIGA:barrywalker~/Desktop/code/Shell> ls -l /tmp/binary
-rw-r--r--  1 barrywalker  wheel  48  9 Sep 19:29 /tmp/binary
AMIGA:barrywalker~/Desktop/code/Shell> hexdump -C /tmp/binary
00000000  52 49 46 46 24 00 01 00  57 41 56 45 66 6d 74 20  |RIFF$...WAVEfmt |
00000010  5c 0a 78 31 30 00 00 00  01 00 01 00 40 1f 00 00  |\.x10.......@...|
00000020  40 1f 00 00 01 00 08 00  64 61 74 61 00 00 01 00  |@.......data....|
00000030
AMIGA:barrywalker~/Desktop/code/Shell> _

As it is mvdan has adequately explained his position.
# 12  
Old 09-09-2016
It does not. Not even when I abuse it by chopping in the middle of a hex code.

Code:
#!/bin/sh

printf "%b" "\x52\x49\x46\x46\x24\x00\x01\x00\x57\x41\x56\x45\x66\x6d\x74\x20\x10\x00\x00\x00\x01\x00\x01\x00\x40\x1f\x00\x00\x40\x1f\x00\x00\x01\x00\x08\x00\x64\x61\x74\x61\x00\x00\x01\x00" > bin1

printf "%b" "\x52\x49\x46\x46\x24\x00\x01\x00\x57\x41\x56\x45\x66\x6d\x74\
\x20\x10\x00\x00\x00\x01\x00\x01\x00\x40\x1f\x00\x00\x40\x1f\x00\x00\x01\
\x00\x08\x00\x64\x61\x74\x61\x00\x00\x01\x00" > bin2

printf "%b" "\x52\x49\x46\x46\x24\x00\x01\x00\x57\x41\x56\x45\x66\x6d\x7\
4\x20\x10\x00\x00\x00\x01\x00\x01\x00\x40\x1f\x00\x00\x40\x1f\x00\x00\x01\
\x00\x08\x00\x64\x61\x74\x61\x00\x00\x01\x00" > bin3

diff bin1 bin2 || echo "bin1 and bin2 differ"
diff bin2 bin3 || echo "bin2 and bin3 differ"
diff bin1 bin3 || echo "bin1 and bin3 differ"

You used two backslashes, and in doing so, inserted a literal backslash, followed by a literal newline.

You're correct, though, how we feel about coding standards has nothing to do with his script. I apologize.
This User Gave Thanks to Corona688 For This Post:
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