0

I need help figuring out the best practices to execute those commands in bash. It's a command that sends a text, and I need to repeat this command for several phone numbers and for several dates... about 250 times in total. The command returns something like this {"creditsUsed":1.00,"snapshotId":107416458} if it went well. An error code otherwise.

curl -X POST \
-H "X-Primotexto-ApiKey: 784155c6a3c9ceed9d0a4d1ffdb67466" \
-H "Content-Type: application/json" \
-d '{"number":"+317877592979" , "message":"test\nother line" , "sender":"myCompany" , "date": 1539272220000}' \
https://api.primotexto.com/v2/notification/messages/send;

curl -X POST \
-H "X-Primotexto-ApiKey: 784155c6a3c9ceed9d0a4d1ffdb67466" \
-H "Content-Type: application/json" \
-d '{"number":"+317877574678" , "message":"test2\nother line" , "sender":"myCompany" , "date": 1539272220001}' \
https://api.primotexto.com/v2/notification/messages/send;

curl -X POST \
-H "X-Primotexto-ApiKey: 784155c6a3c9ceed9d0a4d1ffdb67466" \
-H "Content-Type: application/json" \
-d '{"number":"+317877574676" , "message":"test3\nother line" , "sender":"myCompany" , "date": 1539272220002}' \
https://api.primotexto.com/v2/notification/messages/send;

...

Should I end it with ; or && ? Should I write a script ? Can I do something better with json ?

EDIT maybe I could build a json file like this :

{
    "number":"+336770002979",
    "message":"La plupart\ntest",
    "sender":"BEinstitute",
    "date": 1539286620000
},
{
    "number":"+336600000780",
    "message":"La plupart\ntest",
    "sender":"BEinstitute",
    "date": 1539286620000
}

and the run through each object with curl. Any idea how to do this ?

$ curl -X POST -H "X-Primotexto-H "Content-Type: application/json" -d @json.json https://api.primotexto.com/v2/notification/messages/send;

only reads the first object.

Louis
  • 263

1 Answers1

3

It all depends on your requirements. But writing a script is rarely a bad idea, especially for repetitive tasks.

But between ; and &&, it is a completely different result.

  • ; is a command separator
  • && is a conditional operator (so is ||)

For example:

If you try the following command:

touch /var/no-permission; echo "It works!"

The first part of the line (before the semicolumn) will fail since you won't have access to create files in /var (as long as you aren't logged in as root of course). But you will see the line "t works! printed in your console because the semicolumn is just a separator and doesn't care if the previous command works or not.

However, if you try the following command:

touch /var/no-permission && echo "It works!"

This won't do anything so you won't see It works! in your console. Since the first command will fail, the && operator will not try to run the next command. (You can also use || instead of && to do its opposite. So the second command only runs when the first fails)

So if one of the commands fails, all the others should stop, then use &&. If they should continue, use ;.

Not: If you can write each command on a new line, it's as if you separate them by a semicolumn. For example,

touch /var/no-permission; echo "It works!"

Is the same as

touch /var/no-permission
echo "It works!"
Dan
  • 14,180