You are not logged in.
Pages: 1
Hello,
I am creating a Bash parser. I have posted it previously.
https://bbs.archlinux.org/viewtopic.php?id=243077
I am interested in constructive criticism.
https://github.com/jclifton44/shell-scr … rseJSON.sh
... You are using line-oriented tools like grep and sed, that will just never prove reliable on JSON...
... You do not need to pipe grep to grep, just write a proper regex in the first place. You do not need to pipe sed to sed, just learn how sed commands actually work...
For example, in line 158:
key=$(echo $key | sed "s/^\s*//" | sed "s/\s*$//")
Output from echo is piped to sed to remove leading whitespace. Then output from sed is piped to sed to remove trailing whitespace. The output from sed is assigned to the variable 'key.'
How can one use sed to remove leading and trailing whitespace with one sed command.
I'm making revisions now. I'm going to comb through the thread I originally posted and make changes. If you have any other suggestions they are welcome.
"... a high signal to noise ratio..."
Offline
I'll start:
There are no comments.
"... a high signal to noise ratio..."
Offline
I'll start:
There are no comments.
So after your previous thread was dustbinned for refusing to accept the critiques you asked for, you will open a new thread with the same content?
And your big realization is that your script just needs comments?
Managing AUR repos The Right Way -- aurpublish (now a standalone tool)
Offline
The line-orient tools will not work on real json. But your tool is limited to json-like content all entered on a single line (on the command line no less). So that critique does not apply given the constraint of your tool to not work on multiline input.
http://www.grymoire.com/Unix/Sed.html
Sed commands can be strung together, e.g. `'s/^\s*//;s/\s*$//'` or you can use an anchor for the content, `s/^\s\([^\s]*\).*/\1/'`. But really in this case sed serves no purpose at all:
key=$(echo $key)
Done, e.g:
$ key=" one two "
$ echo "[$key]"
[ one two ]
$ key=$(echo $key)
$ echo "[$key]"
[one two]
Of if you are using bash there are better ways too, but you continue to refuse to specify a shell.
EDIT: there is a "gotcha" with the above echo method. However given the numerous assumptions you are already making that the input is pure and perfect conforming JSON (but all on one line) there's really no harm in the above. EDIT 2: wait, I'll be ****, and asterisk is a valid JSON key name. So the above echo method would not be wise unless you are using bash where you can use the relevant `shopt` command to protect yourself ... and if you are using bash, there are - again - easier methods to remove all whitespace from a variable.
Last edited by Trilby (2019-01-06 18:09:43)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I'd probably split things up in smaller bits with functions. But i'm more of a Go programmer than shell programmer
https://ugjka.net
"It is easier to fool people, than to convince them that they've been fooled" ~ Dr. Andrea Love
Offline
I'll start:
There are no comments.
That's not how this place works. You ask for feedback, and then you incorporate it when it is provided. You don't bump the thread with empty posts like it is failbook.
I'll leave this open for now but the next ill-advised post will see it dustbinned.
Offline
Jclifton44 wrote:I'll start:
There are no comments.
So after your previous thread was dustbinned for refusing to accept the critiques you asked for, you will open a new thread with the same content?
And your big realization is that your script just needs comments?
Criticism is welcome. I'm not refusing to accept critique.
"... a high signal to noise ratio..."
Offline
Of if you are using bash there are better ways too, but you continue to refuse to specify a shell.
I'm using /bin/bash.
Also, it's been updated to parser numbers and boolean values for objects only. Key mistmatch errors and array out of bounds errors exit with status code 1 and a message.
Last edited by Jclifton44 (2019-01-06 19:31:02)
"... a high signal to noise ratio..."
Offline
I'm using /bin/bash.
You may use bash as the default shell on your system, but your script still is not.
But if you want to specify bash, then you can use bash's string manipulation to easily trim whitespace from the start and end of the string.
Last edited by Trilby (2019-01-06 19:39:49)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
EDIT: there is a "gotcha" with the above echo method. However given the numerous assumptions you are already making that the input is pure and perfect conforming JSON (but all on one line) there's really no harm in the above. EDIT 2: wait, I'll be ****, and asterisk is a valid JSON key name. So the above echo method would not be wise unless you are using bash where you can use the relevant `shopt` command to protect yourself ... and if you are using bash, there are - again - easier methods to remove all whitespace from a variable.
it's far, far worse. The script already uses numerous bashisms that cause syntax errors if /bin/sh is not a symlink to bash, and on top of that, when piping `echo $key | sed ...` the glob evaluation is already being performed in the script right now.
The sed is redundant because it will never be utilized in the first place, unless the script is changed to protect itself from variable re-expansion by quoting everything.
Managing AUR repos The Right Way -- aurpublish (now a standalone tool)
Offline
Pages: 1