LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell-script that takes two parameters (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-that-takes-two-parameters-4175667616/)

Alphonse 01-13-2020 07:08 AM

shell-script that takes two parameters
 
Hello everybody!

Im trying to write a shell-script that takes two parameters, a device id and a device state.
The goal is to turn ON/OFF 433mhz RF outlets.

The hardware and config parts are working, i can turn on/off a RF outlet by sending the correct code with a program (codesend), so this thread is merely about the scripting.


Now, since the codes are unique for each device and state (dev1_on=1381717, dev1_off=1381719), i will need a shellscript to take two parameters, the device id and the device state.

The shell script will then send the correct code for the requested device id and state.


The function is similar to another script i downloaded off internet, witch i copied and modified, hoping it would work, but it does not.

My scripting "skills" are a joke, my linux skills are modest at best and my only real experience with such things is the programming language C++.. So please point out any oddities or idioticys i may be guilty of.. :)

the results im getting are in swedish, so im manually translating here:

usr@server:/opt$ ./rf433_send.sh 4 on
./rf433_send.sh: line 21: syntax-error near the unexpected symbol ”)”
./rf433_send.sh: line 21: ` "on") echo /opt/RF433Utils/RPi_utils/codesend 1381717 ;;'


the script:
Code:

#!/bin/bash

# must be called with a parameter
# rf433_send.sh [channel] [command]
# will send the associated command to the corresponding RF outlet over 433mhz radio.

if [ $# -lt 2 ] #Check to see if at least two parameters was supplied
then
  echo "Must be called with the channel id and command to send to the RF outlet"
  echo "example: " $0 "1 on"        # $0 is the name of the program
  echo "For help, use: " $0 " -? "
  exit 1
fi

  if [ $# -ge 2 ]      # if there were 2 or more parameters
  then
        case $1 in
          "-?")      echo "Supported channel id is 1..14" ;;
          "1")  case $2 in 
          "-?") echo
                  "on")      echo /opt/RF433Utils/RPi_utils/codesend 1381717 ;;
                  "off")      echo /opt/RF433Utils/RPi_utils/codesend 1381719 ;;
                esac
          "2")  case $2 in 
          "-?")
                  "on")      echo /opt/RF433Utils/RPi_utils/codesend 1394005 ;;
                  "off")      echo /opt/RF433Utils/RPi_utils/codesend 1394004 ;;
                esac
          "3")  case $2 in 
          "-?")
                  "on")      echo /opt/RF433Utils/RPi_utils/codesend 1397077 ;;
                  "off")      echo /opt/RF433Utils/RPi_utils/codesend 1397076 ;;
                esac
          "4")  case $2 in 
          "-?")
                  "on")      echo /opt/RF433Utils/RPi_utils/codesend 1397845 ;;
                  "off")      echo /opt/RF433Utils/RPi_utils/codesend 1397844 ;;
                esac
        esac
  else
    echo "input needs a second parameter"
    echo "usage: " $0 " [device-id] [state]"
    echo "device id is 1-16, state is on/off"
  fi
  ;;  # end of the input case

  *) echo $1 "is not a recognized parameter. " $0 " -? for a list." ;;
esac
exit 0

disclaimer: the original script was stolen from here:
https://www.raspberrypi.org/forums/v...ic.php?t=67899

individual 01-13-2020 07:49 AM

The formatting of the script makes it difficult to read. I would suggest storing each code in an array, i.e. all "on" codes in an array and all "off" codes in an array. Then you can look up each code by the id, e.g. 1, 2, 3, depending on if the user wants to turn the device on or off. Doing it this way also eliminates the need to write '/opt/RF433Utils/RPi_utils/codesend' multiple times.

Alphonse 01-13-2020 08:08 AM

I actually solved it by trial and error!

the working script:
Code:

#!/bin/bash

# must be called with a parameter
# rf433_send.sh [channel] [command]
# will send the associated command to the corresponding RF outlet over 433mhz radio.

if [ $# -lt 2 ] #Check to see if at least two parameters was supplied
then
        echo "Must be called with the channel id and command to send to the RF outlet"
        echo "example: " $0 "1 on"        # $0 is the name of the program
        echo "For help, use: " $0 " -? "
        exit 1
fi

  if [ $# -ge 2 ]      # if there were 2 or more parameters
then
        case $1 in
        "-?")      echo "Supported channel id is 1..14" ;;
        "1")         
                case $2 in 
                        "on")      echo /opt/RF433Utils/RPi_utils/codesend 1381717
                                                /opt/RF433Utils/RPi_utils/codesend 1381717
                                ;;
                        "off")      echo /opt/RF433Utils/RPi_utils/codesend 1381719
                                                /opt/RF433Utils/RPi_utils/codesend 1381719
                                ;;
                        *)                        echo "unknown state, should be "on" or "off""
                                ;;
                        esac
                ;;
        "2")         
                case $2 in 
                        "on")      echo /opt/RF433Utils/RPi_utils/codesend 1394005
                                                /opt/RF433Utils/RPi_utils/codesend 1394005
                                ;;
                        "off")      echo /opt/RF433Utils/RPi_utils/codesend 1394004
                                                /opt/RF433Utils/RPi_utils/codesend 1394004
                                ;;
                        *)                        echo "unknown state, should be "on" or "off""
                                ;;
                        esac
                ;;
        "3")         
                case $2 in 
                        "on")                  echo /opt/RF433Utils/RPi_utils/codesend 1397077
                                                /opt/RF433Utils/RPi_utils/codesend 1397077
                                ;;
                        "off")      echo /opt/RF433Utils/RPi_utils/codesend 1397076
                                                /opt/RF433Utils/RPi_utils/codesend 1397076
                                ;;
                        *)                        echo "unknown state, should be "on" or "off""
                                ;;
                        esac
                ;;
        "4")         
                case $2 in
                        "on")      echo /opt/RF433Utils/RPi_utils/codesend 1397845
                                                /opt/RF433Utils/RPi_utils/codesend 1397845
                                ;;
                        "off")      echo /opt/RF433Utils/RPi_utils/codesend 1397844
                                                /opt/RF433Utils/RPi_utils/codesend 1397844
                                ;;
                        *)                        echo "unknown state, should be "on" or "off""
                                ;;
                        esac
                ;;
        *)                echo "unknown device id, should be "1"..."14""
                ;;
        esac
else
        echo "input needs a second parameter"
        echo "usage: " $0 " [device-id] [state]"
        echo "device id is 1-16, state is on/off"
fi
# end of the input case
exit 0

Im not competent enough to create an array, or to use it..
I would prefer to "merge" the command lines, to have the program path as part 1 and the command code as part two; "part1 part2" = "/path/to/program commandCode"
But at this precise moment in time; im VERY exited that its even working but this is only half-way, i still have to implement the script into the openHAB home automation server.

individual 01-13-2020 08:16 AM

Good job fixing the problem. Here is how I did it:
Code:

#!/usr/bin/env bash

declare -a on_codes=(1381717 1394005 1397077 1397845)
declare -a off_codes=(1381719 1394004 1397076 1397844)

devid="$1"
action="$2"

if [[ -z $action || "$*" = *'-?'* ]]; then
    echo "Must be called with the channel id and command to send to the RF outlet"
    echo "example: $0 1 on"        # $0 is the name of the program
    echo "For help, use: $0 -?"
    exit
else
    if [[ $devid -lt 1 || $devid -gt ${#on_codes[@]} ]]; then
        echo "Invalid device id"
        exit
    fi

    # arrays are 0 indexed.
    devid=$((devid - 1))
    code=0
    case "$action" in
        on) code="${on_codes[$devid]}" ;;
        off) code="${off_codes[$devid]}" ;;
        *) echo 'Invalid action' && exit ;;
    esac

    echo /opt/RF433Utils/RPi_utils/codesend $code
fi

EDIT: I just used your usage statements, hence the difference in quoting.
EDIT2: I forgot to handle the user entering a number less than 1.

pan64 01-13-2020 10:27 AM

anyway www.shellcheck.net can be used to check your script.


All times are GMT -5. The time now is 07:59 PM.