1

I would like to generate some commands accoring to data stored in a field of mysql table.

To get the data and store them in a variable i do the following

modules=$(/usr/bin/mysql -u $MyUSER -p$MyPASS $DB_NAME -e "SELECT modules FROM myapp WHERE ID=31 " -B -N) 

In the database the data is stored like in json format :

["module-name-1","module-2","https:\/\/domain.com\/module\/packages\/mailmdule.zip"] 

I want to iterate each element of this list and produce a line with a command that would look like

install module-name-1 -yes --no-prompt install module-2 -yes --no-prompt install https://domain.com/module/packages/mailmdule.zip -yes --no-prompt 

Please note that the URL in the last command is formatted differently than it in the json

I was looking for a solution through jq but did not succeed, I have tried to get a clean output :

$((/usr/bin/mysql -u $MyUSER -p$MyPASS $DB_NAME -e "SELECT modules FROM myapp WHERE ID=31 " -B -N)| jq '.[]')) 

But i only get

"module-name-1" "module-2" "https:\/\/domain.com\/module\/packages\/mailmdule.zip" 

Would appreciate any suggestion

1

1 Answer 1

1
jq -r '.[] | @sh "install \(.) -yes --no-prompt"' 

This would take your array and insert the elements, properly quoted for the shell, into a string that is then outputted.

Given the array that you show, this would produce

install 'module-name-1' -yes --no-prompt install 'module-2' -yes --no-prompt install 'https://domain.com/module/packages/mailmdule.zip' -yes --no-prompt 

An array element containing single quotes and spaces etc. would be handled correctly.

To run these commands in the shell, pipe them to sh -s:

jq -r '.[] | @sh "install \(.) -yes --no-prompt"' | sh -s 

Or, you could just do a shell loop reading the data from the array and using that in the install command line rather than trying to construct a valid command in jq:

jq -r '.[]' | while IFS= read module; do install "$module" -yes --no-prompt done 

This obviously won't work if any array element contains an embedded newline.

5
  • I have tried this and it seems to be pretty close to what i am looking for, except that for the output. It is all in one single line. Also the URL is not formatted correctly. I still have this : https:\/\/domain.com\/module\/packages\/mailmdule.zip
    – yazzou
    CommentedFeb 16, 2021 at 15:22
  • @yazzou Ah, you're trying to run it with a command substitution. No, that won't work, obviously. I'm updating the answer, hold on.
    – Kusalananda
    CommentedFeb 16, 2021 at 15:25
  • Thanks ! just to be complete, and to get rid of the "\/\/" in the module url, i have replaced this install "$module" -yes --no-prompt by this : install "${module//\\/}" -yes --no-prompt
    – yazzou
    CommentedFeb 16, 2021 at 15:56
  • @yazzou I never had to do that with the data that I copied from your question. Are you using -r with jq?
    – Kusalananda
    CommentedFeb 16, 2021 at 16:25
  • Yes i am using it but my output was not as expected
    – yazzou
    CommentedFeb 16, 2021 at 16:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.