LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-13-2020, 08:08 AM   #1
Alphonse
LQ Newbie
 
Registered: May 2017
Posts: 6

Rep: Reputation: Disabled
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
 
Old 01-13-2020, 08:49 AM   #2
individual
Member
 
Registered: Jul 2018
Posts: 261
Blog Entries: 1

Rep: Reputation: 207Reputation: 207Reputation: 207
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.
 
Old 01-13-2020, 09:08 AM   #3
Alphonse
LQ Newbie
 
Registered: May 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 01-13-2020, 09:16 AM   #4
individual
Member
 
Registered: Jul 2018
Posts: 261
Blog Entries: 1

Rep: Reputation: 207Reputation: 207Reputation: 207
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.

Last edited by individual; 01-13-2020 at 09:21 AM.
 
Old 01-13-2020, 11:27 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,488

Rep: Reputation: 4308Reputation: 4308Reputation: 4308Reputation: 4308Reputation: 4308Reputation: 4308Reputation: 4308Reputation: 4308Reputation: 4308Reputation: 4308Reputation: 4308
anyway www.shellcheck.net can be used to check your script.
 
  


Reply

Tags
shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hibernation takes ~10 seconds only once after a reboot; After resume, it takes about 3-4 minutes. geni4u Linux - Laptop and Netbook 10 01-21-2018 02:07 PM
edit a file /etc/fstab , what parameters can be changebale and which parameters lenin99 Debian 7 01-24-2017 05:03 AM
[SOLVED] Why there are two separate parameters in /proc/sys/net/ipv4/ for security parameters gprathap1121@gmail.com Linux - Security 4 07-14-2014 01:41 AM
LXer: Stella takes CentOS (which takes RHEL) and adds many of the desktop packages you're missing; a LXer Syndicated Linux News 0 08-11-2012 11:33 PM
Benchmarking a tape drive, time to copy takes forever, time to append takes longer? jeriryan Linux - Hardware 2 06-15-2008 01:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration