1

Im trying to send a slack message if there is a file available thats newer than a day.

Full command

find . -mtime -1 -exec curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/FooBar {} \;

the command doest work but throws an error.

output: okcurl: (6) Could not resolve host: okcurl: (6) Could not resolve host: okcurl: (6) Could not resolve host:

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • 1
    You need a space after the closing single quote. Voting to close as typo. – tripleee Nov 15 '18 at 11:42
  • thanks for the quick reply @tripleee ! sadly it doesn't work – Kasper van Rotterdam Nov 15 '18 at 11:51
  • I edited my question to clarify some things – Kasper van Rotterdam Nov 15 '18 at 11:54
  • 3
    You *still* need a space after the quote. If you're actually doing that, edit your question to reflect it. Also, you're passing the filenames found by the `find` command to `curl`, which will try to interpret them as hostnames. It's likely that is the source of your error because it is unlikely that those filenames are valid hostnames. – larsks Nov 15 '18 at 11:56
  • I'm not passing any variables found by find to the Curl command I also added the space in my command – Kasper van Rotterdam Nov 15 '18 at 11:58
  • You probably have DOS line endings in the script file. Duplicate of https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings – tripleee Nov 15 '18 at 12:17
  • 2
    Yes, you do. `find` will replace `{}` by the found files. I assume that the current directory `.` is the first file found. – Socowi Nov 15 '18 at 12:26
  • This is not "a simple typographical error" as alleged by some commentators here, but a problem with the correct syntax of the bash command. See my answer for the solution. Voting to reopen. – Erik Kalkoken Nov 15 '18 at 15:31

1 Answers1

0

As already mentioned by @larsks the filenames inserted via {} into the curl are apparently interpreted as host names and therefore generating that error.

It works if you put the {} into the text property of the message, which will then include the found filename in the message to the Slack webhook. I guess it would also make sense to not only send a message, but also include the name of that file in that message.

find . -mtime -1 -exec curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World! {}"}' https://hooks.slack.com/services/FooBar \;

Or if you don't want to show the found filenames on Slack you can just omit the {} altogether:

find . -mtime -1 -exec curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/FooBar \;

Finally, I would assume you do not want to send a message for the "." directory, so you need to exclude it: (from here)

find . -mtime -1 -not -path "." -exec curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/Foobar \;
Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114