0

I am trying to create/edit a pushover shell script that can be called from any script or command, and send the output of the script or command to my Pushover account. Following the instructions on this page.

I have placed the following shell script in /usr/local/bin and added my app token and user tokens.

I am not getting any pushover notifications, or errors after using this command:

john$ ls | pushover.sh 2>&1 | tee file /Users/john/Desktop/results.txt

Contents of the shell script EDITED per Glenn

#!/usr/bin/env bash
         
# TOKEN INFORMATION 
_token='APPTOKEN'
_user='USERTOKEN'
         
# Bash function to push notification to registered device
push_to_mobile() {
  local t="${1:cli-app}"
  local m="$2"
  [[ -n "$m" ]] && curl -s \
    --form-string "token=${_token}" \
    --form-string "user=${_user}" \
    --form-string "title=$t" \
    --form-string "message=$m" \
    https://api.pushover.net/1/messages.json
}

I am assuming the conflict is in the first line, possibly in the quoting, but after trying a few different variations haven't had any success.

An example of what is working after trying to debug the above shell script. Obviously this is to just prove that my pushover settings are all in order. This narrows the issue down to the function in the script.

#!/usr/bin/env bash

# TOKEN INFORMATION
_token='xxxx'
_user='yyyy'
_message='test'

# Bash function to push notification to registered device
curl -s \
  --form-string "token=${_token}" \
  --form-string "user=${_user}" \
  --form-string "message=${_message}" \
 https://api.pushover.net/1/messages.json
3
  • Indeed. You need to separate the shell commands with newlines or semicolons. If you were not redirecting stderr, you'd see something like bash: local: `[[': not a valid identifier Nov 30, 2020 at 20:22
  • Thanks. Actually before I redirected to stderr, to troubleshoot, I didn't see something like that. Could you rewrite a portion or the entire thing as an answer below?
    – John
    Nov 30, 2020 at 20:30
  • Hi, I see the confusion. It's just how I formatted it on this site. Not sure why it has been double indented. I will fix it so it represents my shell script.
    – John
    Nov 30, 2020 at 20:37

1 Answer 1

1

You are correct that the first line of the function is problematic. You need to separate the shell commands with newlines or semicolons. If you were not redirecting stderr, you'd see something like bash: local: `[[': not a valid identifier

Try this:

push_to_mobile() {
  local t="${1:cli-app}"
  local m="$2"
  [[ -n "$m" ]] && curl -s \
    --form-string "token=${_token}" \
    --form-string "user=${_user}" \
    --form-string "title=$t" \
    --form-string "message=$m" \
    https://api.pushover.net/1/messages.json
}

Although I'd use an array for the curl options for easier maintenance.

push_to_mobile() {
  [[ -z "$2" ]] && return
  local curl_opts=(
    --silent
    --form-string "title=${1:-cli-app}"
    --form-string "message=$2"
    --form-string "token=${_token}"
    --form-string "user=${_user}"
  )
  curl "${curl_opts[@]}" https://api.pushover.net/1/messages.json
}

Demonstrating the error message:

$ f() { local a=b local c=d [[ x == x ]] && echo hello; }
$ f
bash: local: `[[': not a valid identifier
bash: local: `==': not a valid identifier
bash: local: `]]': not a valid identifier
0

You must log in to answer this question.

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