macosbashterminalencodeuricomponentpbcopy

Bash shells script 101 - variable definition, pbcopy, etc


I am trying to create a (my first) bash script, but I need a little help. I have the following:

#!/bin/bash

echo "Write a LaTeX equation:" 
read -e TeXFormula

URIEncoded = node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')"

curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif | pbcopy

I want it to:

  1. Require user input (LaTeX equation)
  2. URIEncode user input (the top result in Google was using node.js, but anything will do..)
  3. Perform a cURL call to this website converting equation to a GIF image
  4. Copy the image to the placeholder, so I can paste it to a note taking app like OneNote, Word, etc.

My script is malfunctioning in the following:


Solution

  • URIEncoded is undefined, so there is something wrong with my variable definition.

    The line should read

    URIEncoded=$(node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')")
    

    without spaces around the = sign, and using the $() construct to actually perform the command, otherwise, the text of the command would be assigned to the variable.

    When I copy using pbcopy the encrypted text content of the image is copied, and not the actual image. Is there a workaround for this? Otherwise, the script could automatically open the image and I could manually Cmd + C the content.

    pbcopy takes input from stdin but you are telling curl to write the output to a file rather than stdout. Try simply

    curl http://latex.codecogs.com/gif.latex?$URIEncoded | pbcopy
    

    or, for the second option you describe

    curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif && open $_