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.

0

1 Answer 1

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!"
5
  • thank you, I understand for the $$ and ; question. Can you help me write the script , is that possible to put the content of all the -d {} in a json file ?
    – Louis
    Oct 11, 2018 at 19:16
  • 4
    @Louis we can help you make something, but we won't do it for you. Go ahead and ask about -d {} in a new question. You'll probably have more luck on Stack Overflow about that, but only ask about specific issues. Oct 11, 2018 at 19:26
  • stackoverflow.com/questions/52767726/… :)
    – Louis
    Oct 11, 2018 at 19:40
  • 2
    @Louis Before you post new questions, don't forget to upvote and accept the existing answers if they helped you... ;-)
    – Fabby
    Oct 12, 2018 at 7:50
  • 1
    @TheWander It'd be perfectly fine here as well. It's not specifically a programming question, just a scripting one Oct 13, 2018 at 14:19

Not the answer you're looking for? Browse other questions tagged .