0

I am trying to write a npm script (bash) to run the command jsonlint -q over every .json file in a directory that contains files of other extensions.

This is what I have so far, but it only works for the first json file in the directory. Different things I've tried end up running it over every file regardless of extension.

ls folder/subfolder/ *.json | xargs jsonlint -q

  • 2
    `for x in *.json; do jsonlint -q "$x"; done`. See [Why you shouldn't parse the output of `ls`](http://mywiki.wooledge.org/ParsingLs), and [BashPitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29). Use `set -x` beforehand to log commands actually run. – Charles Duffy May 07 '18 at 15:42
  • Is your application ever going to be developed on Windows? If so, you might want to consider a JavaScript solution instead of bash. – Hunter McMillen May 07 '18 at 15:44
  • @CharlesDuffy I tried this, this has no mention of the specific 'folder/subfolder', also I'm putting this as an npm-script - it says that `Error: ENOENT: no such file or directory, open '$x'` ... what is the $x supposed to be? – Username123 May 07 '18 at 15:48
  • 1
    If you write the `for` loop exactly as @CharlesDuffy wrote in the first comment then the `$x` is the name of each file in the list. Note that `'` is not the same as `"`. – JawguyChooser May 07 '18 at 15:56
  • 1
    Regarding the directory path: just add it to the for loop invocation: `for x in folder/subfolder/*json; do` – JawguyChooser May 07 '18 at 15:58

0 Answers0