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 https://www.raspberrypi.org/forums/v...ic.php?t=67899 |
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.
|
I actually solved it by trial and error!
the working script: Code:
#!/bin/bash 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. |
Good job fixing the problem. Here is how I did it:
Code:
#!/usr/bin/env bash EDIT2: I forgot to handle the user entering a number less than 1. |
anyway www.shellcheck.net can be used to check your script.
|
All times are GMT -5. The time now is 07:59 PM. |