Pushover shell 腳本將命令列輸出結果傳送到 Pushover.app

Pushover shell 腳本將命令列輸出結果傳送到 Pushover.app

我正在嘗試建立/編輯一個可以從任何腳本或命令呼叫的 Pushover shell 腳本,並將腳本或命令的輸出傳送到我的 Pushover 帳戶。請按照此說明進行操作

我已放置以下 shell 腳本/usr/local/bin並新增我的應用程式令牌和使用者令牌。

使用此命令後,我沒有收到任何 Pushover 通知或錯誤:

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

shell 腳本的內容由 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
}

我假設衝突在第一行,可能在引用中,但在嘗試了一些不同的變體後沒有任何成功。

嘗試偵錯上述 shell 腳本後運行情況的範例。顯然這只是為了證明我的 Pushover 設定都是正確的。這將問題範圍縮小到腳本中的函數。

#!/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

答案1

您是對的,函數的第一行有問題。您需要用換行符號或分號分隔 shell 指令。如果您沒有重定向 stderr,您會看到類似的內容bash: local: `[[': not a valid identifier

嘗試這個:

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
}

儘管我會使用數組作為捲曲選項以方便維護。

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
}

演示錯誤訊息:

$ 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

相關內容