예약된 명령을 실행하지 않을 때 - 문제 해결

예약된 명령을 실행하지 않을 때 - 문제 해결

저는 날짜, 시간, 전화번호가 포함된 파일을 읽고 SMS 공급자 API를 사용하여 SMS 알림을 보내는 이 bash 스크립트를 작성 중입니다.

#!/bin/bash

while read date time phone
do

user=user
pass=pass
senderid=senderid
message=Your%20appointment%20is%20at%20$date%20$time.%20For%20cancellations%20call%2096989898.%20Thank%20you.
api="https://sms.service.com/Websms/sendsms.aspx?User=$user&passwd=$pass&mobilenumber=357$phone&message=$message&senderid=$senderid&type=0"

curl -k $api

done < ~/sms_reminders/events/events_$(date +%d-%m-%y)

이렇게 실행하면 바로 SMS가 옵니다. 그런데 특정 시간에 나가도록 알림을 예약하고 싶습니다. 그래서 스크립트를 이것으로 변경합니다.

#!/bin/bash

while read date time phone
do

user=user
pass=pass
senderid=senderid
message=Your%20appointment%20is%20at%20$date%20$time.%20For%20cancellations%20call%2096989898.%20Thank%20you.
api="https://sms.service.com/Websms/sendsms.aspx?User=$user&passwd=$pass&mobilenumber=357$phone&message=$message&senderid=$senderid&type=0"

echo curl -k $api | at $time

done < ~/sms_reminders/events/events_$(date +%d-%m-%y)

그리고 다음과 같은 메시지를 받았습니다.

warning: commands will be executed using /bin/sh
job 22 at Fri Jun  6 21:46:00 2019

어느 것이 좋니.

하지만 SMS를 받은 적이 없습니다.

내 생각엔 문제가 sh와 관련이 있는 것 같지만 at은 명령이 성공적으로 완료되었는지 여부를 알려주는 로그 파일을 실제로 생성하지 않기 때문에 확신할 수 없습니다.

답변1

매개변수 확장을 통해 Bash에게 변수를 인용하도록 지시할 수 있습니다 api.

${parameter@operator}
확장은 연산자 값에 따라 매개변수 값의 변환이거나 매개변수 자체에 대한 정보입니다. 각 연산자는 단일 문자입니다.

  • Q 확장은 입력으로 재사용할 수 있는 형식으로 인용된 매개변수 값인 문자열입니다.

그래서:

echo curl -k "${api@Q}" | at "$time"

와 같이 따옴표를 이스케이프하면 echo curl -k \"$api\"확장 시 api필드 분할 및 와일드카드 확장이 수행되므로 콘텐츠에 따라 문제가 발생할 수 있습니다. 따라서 정상적으로 인용 "${api}"하고 bash에 를 사용하여 입력을 위해 다시 인용하도록 지시하는 것이 좋습니다 "${api@Q}".

참고로 예시 입력의 경우 출력은 다음과 같습니다.

$ echo curl -k "${api@Q}"
curl -k 'https://sms.service.com/Websms/sendsms.aspx?User=user&passwd=pass&mobilenumber=357&message=Your%20appointment%20is%20at%20%20.%20For%20cancellations%20call%2096989898.%20Thank%20you.&senderid=senderid&type=0'

출력의 URL 주위에 추가된 작은따옴표에 유의하세요.

답변2

이렇게 해야 했지

echo curl -k \"$api\" | at $time

관련 정보