0

I am trying to write a script that uses some environtment variables that are set by the sms sending/receiving program gammu. These variables are set at the moment when messages are received/sent and are not accessible otherwise. This means that the script must run by the daemon to make use of these variables. This isn't a problem, but actually accessing these! This is the example script I am trying to run from withing gammu.

#!/bin/sh
for i in `seq $SMS_MESSAGES` ; do
        echo SMS_"${i}"_NUMBER SMS_"${i}"_TEXT >> smslog.dat
done

Basically I want to make a log of all the messages received. However with the above script all I get is SMS_1_NUMBER SMS_1_TEXT. How can I get the values stored in these variables?

2

Your script could do just

env | grep -E '^SMS_.*_(NUMBER|TEXT)=' >>smslog.dat

Alternatively,

i=1

while [ "$i" -le "$SMS_MESSAGES" ]; do
    printf 'Message %d number = "%s"\n' "$i" "$( eval printf '%s' "\$SMS_${i}_NUMBER" )"
    printf 'Message %d text   = "%s"\n' "$i" "$( eval printf '%s' "\$SMS_${i}_TEXT" )"

    i=$(( i + 1 ))
done >>smslog.dat

To be sure that you write to smslog.dat in the correct location, either use cd in your script to the correct directory, or specify smslog.dat with its full path.

  • thank you for your answer but for some reason nothing is written in the smslog.dat with the second option which I would prefer. – lucian Feb 16 '18 at 14:51
  • @lucian If you add echo "$SMS_MESSAGES" at the top before the loop, what does it print? – Kusalananda Feb 16 '18 at 14:54
  • when i send a message I get in the gammu log file the correct output: Subprocess output: 1. This means that gammu has launched the script and processed it – lucian Feb 16 '18 at 14:56
  • @lucian I have just now fixed two syntax errors (missing " in two places). Sorry about that, try again. – Kusalananda Feb 16 '18 at 14:59
  • 1
    you were right! full path to the smslog.dat file was the answer! thank you so much for your help! – lucian Feb 16 '18 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.