Sure, I'll remove the BAUDRATE configuration from the script. Here's the updated version:
Script: sms_checker.sh
bash
#!/bin/bash
# Configuration
PHONE_NUMBER="80763"
MODEM_DEVICE="/dev/ttyUSB0" # Adjust if needed
# Function to send SMS
send_sms() {
local number="$1"
local message="$2"
echo "Sending SMS to $number: $message"
sms_tool -d "$MODEM_DEVICE" send "$number" "$message"
}
# Function to receive SMS
receive_sms() {
echo "Waiting for SMS reply..."
sms_tool -d "$MODEM_DEVICE" recv
}
# Function to send USSD code
send_ussd() {
local code="$1"
echo "Sending USSD code: $code"
sms_tool -d "$MODEM_DEVICE" -D ussd "$code"
}
# Function to parse USSD response (simplified)
parse_ussd_response() {
local response="$1"
# Example: "Your balance expires in 25 days"
if [[ "$response" =~ "days" ]]; then
days=$(echo "$response" | grep -oE '[0-9]+ days' | grep -oE '[0-9]+')
if [[ -n "$days" ]]; then
echo "$days"
else
echo "0" # Default if no days found
fi
else
echo "0" # Default if no days found
fi
}
# Main script
echo "Starting SMS/USSD check..."
# Step 1: Send initial SMS
send_sms "$PHONE_NUMBER" "START"
# Step 2: Wait for reply
while true; do
reply=$(receive_sms)
echo "Received reply: $reply"
# Check if reply is "active"
if [[ "$reply" == *"active"* ]]; then
echo "Device is active. Exiting."
exit 0
else
echo "Device not active. Proceeding to USSD check..."
break
fi
done
# Step 3: Send USSD code *101#
ussd_response=$(send_ussd "*101#")
# Step 4: Parse USSD response for days remaining
days_remaining=$(parse_ussd_response "$ussd_response")
echo "Days remaining: $days_remaining"
# Step 5: Check if days > 30
if [[ "$days_remaining" -gt 30 ]]; then
echo "More than 30 days remaining. Exiting."
exit 0
else
echo "Less than 30 days remaining. Calculating new date..."
# Step 6: Calculate new date (add days to current date)
new_date=$(date -d "+$days_remaining days" +"%Y-%m-%d")
echo "New date: $new_date"
# (Optional) Store the number and new date in a file
echo "$PHONE_NUMBER,$new_date" >> sms_log.csv
echo "Logged to sms_log.csv"
fi
exit 0
How It Works
Sends START SMS to 80763.
Waits for a reply:
If the reply contains active, it exits.
If not, it proceeds to USSD.
Sends USSD *101# and checks the response.
Parses the response to extract days remaining (e.g., "Your balance expires in 25 days" → 25).
If days > 30, exits.
If days ≤ 30, calculates a new date using date -d and logs the number + new date to sms_log.csv.
How to Run
Save as sms_checker.sh.
Make it executable:
bash
chmod +x sms_checker.sh
Run it:
bash
./sms_checker.sh
Notes
Adjust MODEM_DEVICE if needed.
The USSD response parsing is simplified—modify parse_ussd_response() to match your modem's output.
If sms_tool requires extra flags (e.g., -j for JSON), adjust accordingly.