コマンドライン出力の結果を Pushover.app に送信する Pushover シェル スクリプト

コマンドライン出力の結果を Pushover.app に送信する Pushover シェル スクリプト

私は、任意のスクリプトやコマンドから呼び出すことができるプッシュオーバーシェルスクリプトを作成/編集し、スクリプトやコマンドの出力を自分のプッシュオーバーアカウントに送信しようとしています。このページ

次のシェル スクリプトを配置し/usr/local/bin、アプリ トークンとユーザー トークンを追加しました。

このコマンドを使用した後、プッシュオーバー通知やエラーは表示されません。

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

シェルスクリプトの内容は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
}

競合は最初の行、おそらく引用にあると想定していますが、いくつかの異なるバリエーションを試してみましたが、成功しませんでした。

上記のシェル スクリプトをデバッグしようとした後に機能するものの例。明らかに、これは私のプッシュオーバー設定がすべて適切であることを証明するためだけのものです。これにより、問題はスクリプト内の関数に絞り込まれます。

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

関数の最初の行に問題があるのは正しいです。シェルコマンドを改行またはセミコロンで区切る必要があります。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
}

ただし、メンテナンスを容易にするために、curl オプションには配列を使用します。

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

関連情報