Skip to content

Instantly share code, notes, and snippets.

@wbazant
Last active September 29, 2017 12:01
Show Gist options
  • Save wbazant/dd7d9aec7fe31c6128354989e1751d5f to your computer and use it in GitHub Desktop.
Save wbazant/dd7d9aec7fe31c6128354989e1751d5f to your computer and use it in GitHub Desktop.
  1. Can you not do exit at the bottom, and put echo Finished!? We will find it easier to know that the program works correctly. Add -S or --show-error so that we see what went wrong. Make curl's response include the query (so that when it fails, we know what it failed for, imagine you got just a HTTP failure code in the logs - not helpful to work with).

  2. Instead of tr -d '"' do jq -r , “raw” - it does what you want :)

  3. https://www.shellcheck.net/ tells you things about the program -and that it's mostly fine, no warnings and merely notes- could you apply at least the one about $() instead of ``?

  4. I've been collecting useful JQ programs on https://www.ebi.ac.uk/seqdb/confluence/display/GXA/JQ+tips I've just put exactly the one you want (get all baseline experiments) as:

curl http://www.ebi.ac.uk/gxa/json/experiments | jq -r '.aaData | map(select(.experimentType=="RNASEQ_MRNA_BASELINE")) | map(.experimentAccession)[]'
  1. instead of a for loop you can
curl http://www.ebi.ac.uk/gxa/json/experiments | jq -r '.aaData | map(select(.experimentType=="RNASEQ_MRNA_BASELINE")) | map(.experimentAccession)[]' | while read -r experimentAccession {
	// the program goes here
	echo "$experimentAccession"
}

Feel free to not apply the last two comments, the solution with iterating through indices is unusual for bash but it should work. At the very least apply 0) the rest would've helped you before you started, now that you have a working program it's mostly so you can get productive with shell scripting / jq if you want to.

Well done for solving the problem! I think it will work. I like that the script has:

  • a clear usage message
  • clear usage of variables
  • the program's reporting on its progress, so when it fails we will know
  • good usage of curl's options (e.g. -o to /dev/null because we don't need it)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment