2025年4月10日 星期四

Specific band for Telit modules

Telit的Band都是用算的...= ="

AT#BND?

#!/bin/bash

function display_bands() {
    local num=$1
    bands=()
    band_number=1

    while [[ $num -gt 0 ]]; do
        if ((num & 1)); then
            bands+=("band $band_number")
        fi
        num=$((num >> 1))
        band_number=$((band_number + 1))
    done

    if [ ${#bands[@]} -eq 0 ]; then
        echo "No bands enabled."
    else
        echo "Enabled bands: ${bands[*]}"
    fi
}

function calculate_number() {
    local input=$1
    IFS=',' read -r -a band_array <<<"$input"
    num=0

    for band in "${band_array[@]}"; do
        band=$(echo "$band" | xargs) # Trim whitespace
        if [[ $band =~ ^[0-9]+$ ]]; then
            num=$((num | (1 << (band - 1)))) # Set the corresponding bit
        else
            echo "Invalid band number: $band"
            return
        fi
    done

    echo "Resulting number: $num"
}

# Main script logic
echo "Choose an option:"
echo "1. Enter a number to see enabled bands"
echo "2. Enter band numbers to calculate the resulting number"
echo "3. Convert hex to decimal"
read -p "Enter your choice (1 or 3): " choice

if [[ $choice -eq 1 ]]; then
    read -p "Enter a number: " num
    if [[ $num -lt 0 ]]; then
        echo "Invalid input. Please enter a non-negative number."
        exit 1
    fi
    display_bands "$num"
elif [[ $choice -eq 2 ]]; then
    read -p "Enter band numbers (comma-separated): " band_input
    calculate_number "$band_input"
elif [[ $choice -eq 3 ]]; then
    read -p "Enter a hexadecimal number: " hex_num
    if [[ $hex_num =~ ^[0-9a-fA-F]+$ ]]; then
        dec_num=$((16#$hex_num))
        echo "Decimal equivalent: $dec_num"
    else
        echo "Invalid hexadecimal number."
    fi
else
    echo "Invalid choice. Please enter 1, 2, or 3."
fi
example:
$ ./band.sh
Choose an option:
1. Enter a number to see enabled bands
2. Enter band numbers to calculate the resulting number
Enter your choice (1 or 2): 1
Enter a number: 134219914
Enabled bands: band 2 band 4 band 8 band 12 band 28


$ ./band.sh
Choose an option:
1. Enter a number to see enabled bands
2. Enter band numbers to calculate the resulting number
Enter your choice (1 or 2): 2
Enter band numbers (comma-separated): 2,4,8,12,28
Resulting number: 134219914

沒有留言:

張貼留言