Pushover.app에 명령줄 출력 결과를 보내는 푸시오버 셸 스크립트

Pushover.app에 명령줄 출력 결과를 보내는 푸시오버 셸 스크립트

모든 스크립트나 명령에서 호출할 수 있는 푸시오버 셸 스크립트를 생성/편집하고 스크립트나 명령의 출력을 내 푸시오버 계정으로 보내려고 합니다. 이에 대한 지침에 따라페이지.

다음 셸 스크립트를 배치하고 /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
}

더 쉬운 유지 관리를 위해 컬 옵션에 배열을 사용하겠습니다.

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

관련 정보