LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash scripting question (https://www.linuxquestions.org/questions/linux-general-1/bash-scripting-question-682923/)

rmarkin 11-12-2008 11:10 AM

bash scripting question
 
Hello everyone,

Code:

#!/bin/sh
REMOTE_NAME=DirectTV_RC16
for digit in $(echo $1 | sed -e 's/./& /g'); do
irsend --device=/dev/lircd1 SEND_ONCE $REMOTE_NAME $digit
sleep 0.4 # note, you may have to tweak the interdigit delay up a bit
done
irsend --device=/dev/lircd1 SEND_ONCE $REMOTE_NAME select

Currently if I pass a value to the above script such as "one", I get the following results. I would like the script to run the irsend command once and pass the value "one" instead of the below behavior.


Code:

robert@myth-dvr:~$ /usr/local/bin/change_channel.sh one
irsend: command failed: SEND_ONCE DirectTV_RC16 o
irsend: unknown command: "o"
irsend: command failed: SEND_ONCE DirectTV_RC16 n
irsend: unknown command: "n"
irsend: command failed: SEND_ONCE DirectTV_RC16 e
irsend: unknown command: "e"
robert@myth-dvr:~$


Any ideas would be greatly appreciated.

Robert

jan61 11-12-2008 01:47 PM

Moin

I don't exactly understand, what you want. Which values do you expect as $1? If you want to split the argument into single digits, you first have to assure that the argument is a number. Actually your sed splits every argument (even if it is a number or not) into single characters.

Maybe this sed helps:
Code:

sed -e 's/[0-9]/& /g'
Jan

unSpawn 11-12-2008 01:57 PM

The "for" loop breaking up stuff?
Code:

#!/bin/bash
DEVICE="/dev/lircd1"
REMOTE_NAME="DirectTV_RC16"
CMD="irsend --device=$DEVICE SEND_ONCE $REMOTE_NAME"
$CMD "$@"; $CMD select
exit 0


lumak 11-13-2008 01:52 PM

I think I know what you are trying to do... but I'm not sure...

If you are pressing a number combination 1 2 3 4 that gets cached then translated and separated with your for loop... then you really just need spaces in between them.

Code:

function function_name () {
COMMANDS="$1"
# will automatically loop with each word.  No sed needed.
# if you need to change the field separator to anything other than white space... you need to change an environment variable
# I forgot what it is at the moment.
for digit in $1; do
...
done
...
}

# Each word will be treated as a separate loop through the for loop.
function_name "one two three four"

However, it would be better to not have to quote the numbers and just use "$@" in place of "$1"


It's kind of hard to understand what you are trying to do with the for command if you don't tell us more information.


All times are GMT -5. The time now is 12:25 PM.