Find the answer to your Linux question:
Results 1 to 6 of 6
Hello everyone, I am borrowing the following bash script for use with my Mythbuntu system. Code: #!/bin/sh REMOTE_NAME=DirectTV_RC16 for digit in $(echo $1 | sed -e 's/./& /g'); do irsend ...
  1. #1
    Just Joined!
    Join Date
    Feb 2005
    Posts
    8

    Issue with a for loop in bash script

    Hello everyone,

    I am borrowing the following bash script for use with my Mythbuntu system.

    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 OK
    When I pass a value to the script such as "select", it runs the irsend command once for each letter in the value "select" instead of substituting the entire value and only running the irsend command once. Any ideas how to fix this?

    Thanks,
    Robert

  2. #2
    Just Joined!
    Join Date
    Feb 2005
    Posts
    8
    For instance, here is the output if I pass "one" to the above script.

    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:~$

  3. #3
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Quote Originally Posted by rmarkin View Post
    Hello everyone,

    I am borrowing the following bash script for use with my Mythbuntu system.

    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 OK
    When I pass a value to the script such as "select", it runs the irsend command once for each letter in the value "select" instead of substituting the entire value and only running the irsend command once. Any ideas how to fix this?

    Thanks,
    Robert
    If you don't know what this

    Code:
    sed -e 's/./& /g'
    does, you had better read the sed manpage.

  4. #4
    Just Joined!
    Join Date
    Feb 2005
    Posts
    8
    Thank you for the RTFM. I will read through it again in an attempt to understand it.

  5. #5
    Just Joined!
    Join Date
    Feb 2005
    Posts
    8
    Well, I believe I actually understand less about the sed command now that I have read the manpage three times.

    Can I infer from your post that the problem I am having is not related to the for loop and in fact is a problem with the sed command in the script? If so, is there some other way to achieve the same result?

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    I took that piece of code and put it into a script to see what happens. This is what I got:

    Code:
    # ./a.bash select
    #!/bin/bash -vx
    
    echo $1 | sed -e 's/./& /g'
    + echo select
    + sed -e 's/./& /g'
    s e l e c t
    So the sed command is placing spaces between each letter so that's why you're getting multiple runs instead of one.

    Why not take out the sed command entirely:

    Code:
    for digit in $(echo $1); do

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...