Ambiguous redirect in shell script - Mandriva

This is a discussion on Ambiguous redirect in shell script - Mandriva ; Hi all, I am playing around learning some very basic shell scripting and have encountered an error message that I don't understand. the following code : 1 #!/bin/bash 2 3 now=$(date --rfc-3339 'date' | cut -c 3-4,6-7,9-10) 4 file="/mnt/PDW_Logfiles/"$now".flt" 5 ...

+ Reply to Thread
Results 1 to 13 of 13

Thread: Ambiguous redirect in shell script

  1. Ambiguous redirect in shell script

    Hi all,
    I am playing around learning some very basic shell scripting and
    have encountered an error message that I don't understand.

    the following code :

    1 #!/bin/bash
    2
    3 now=$(date --rfc-3339 'date' | cut -c 3-4,6-7,9-10)
    4 file="/mnt/PDW_Logfiles/"$now".flt"
    5 if [ -e $file ]
    6 then
    7 message=$(grep 1041138 $file |tr -d '[\r]' |
    cut -c 18-26,37-42,44- | uniq )
    8 mail -s "Fire Report" chris < $(echo $message)
    9 echo $message
    10 # rm -f ~/tmp/message.txt
    11 else
    12 echo "The file ($file) does not exist!"
    13 fi

    results in the following error message:

    ../lesson2.sh: line 8: $(echo $message): ambiguous redirect

    However, if I run this code:

    1 #!/bin/bash
    2
    3 now=$(date --rfc-3339 'date' | cut -c 3-4,6-7,9-10)
    4 file="/mnt/PDW_Logfiles/"$now".flt"
    5 if [ -e $file ]
    6 then
    7 message=$(grep 1041138 $file | cut -c
    18-26,37-42,44- | uniq | tr -d '[\r]' > ~/tmp/message.txt)
    8 mail -s "Fire Report" chris < message.txt
    9 echo $(cat message.txt)
    10 rm -f ~/tmp/message.txt
    11 else
    12 echo "The file ($file) does not exist!"
    13 fi
    14
    No error message.

    What makes the first example code line 8 ambiguous?

    Thanks in advance

    Chris Roy-Smith

  2. Re: Ambiguous redirect in shell script

    On Sat, 16 Aug 2008 17:55:20 +1000, Chris Roy-Smith wrote:
    > Hi all,
    > I am playing around learning some very basic shell scripting


    FYI: some light reading found here http://tldp.org/LDP/abs/html/index.html

    > and have encountered an error message that I don't understand.
    > 8 mail -s "Fire Report" chris < $(echo $message)
    > results in the following error message:
    > ./lesson2.sh: line 8: $(echo $message): ambiguous redirect


    Might have helped to have seen contents of $message.

    Based on what I think you have misunderstood so far, you should have
    done a mail -s "Fire Report" chris < $message

    When you want the variable contents just use the $var_here instead of
    $(echo $var_here)

    Tip. When debugging a bash script, you might see what going on by using set.
    Two examples:

    set -xv
    message=$(grep 1041138 $file | cut -c
    18-26,37-42,44- | uniq | tr -d '[\r]' > ~/tmp/message.txt)
    mail -s "Fire Report" chris < $(echo $message)
    set -

    set -x
    message=$(grep 1041138 $file | cut -c
    18-26,37-42,44- | uniq | tr -d '[\r]' > ~/tmp/message.txt)
    mail -s "Fire Report" chris < $(echo $message)
    set -


    > 3 now=$(date --rfc-3339 'date' | cut -c 3-4,6-7,9-10)


    Lots of learning going on there. Cut/past these two lines in a terminal

    now=$(date +%y%m%d)
    echo $now


    > 4 file="/mnt/PDW_Logfiles/"$now".flt"


    Cut/past these lines in a terminal


    now=$(date +%y%m%d)
    file="/mnt/PDW_Logfiles/$now.flt"
    echo $file

  3. Re: Ambiguous redirect in shell script

    Chris Roy-Smith wrote:
    > 7 message=$(grep 1041138 $file |tr -d '[\r]' |
    > cut -c 18-26,37-42,44- | uniq )
    > 8 mail -s "Fire Report" chris < $(echo $message)


    > What makes the first example code line 8 ambiguous?


    message=$(grep 1041138 $file |tr -d '[\r]' | cut -c 18-26,37-42,44- |
    uniq )
    Will translate to message="some string, or an error message", which
    yields

    mail -s "Fire Report" chris < $(echo "some string, or an error message")
    If the echo command works, you get
    mail -s "Fire Report" chris < $(some string, or an error message)

    And there is no variable known as (some string, or an error message)

    Cheers!

    jim b.

    --
    UNIX is not user unfriendly; it merely
    expects users to be computer-friendly.

  4. Re: Ambiguous redirect in shell script

    Bit Twister wrote:

    > On Sat, 16 Aug 2008 17:55:20 +1000, Chris Roy-Smith wrote:
    >> Hi all,
    >> I am playing around learning some very basic shell scripting

    >
    > FYI: some light reading found here
    > http://tldp.org/LDP/abs/html/index.html


    Yes I discovered tldp a while ago.... LOTS of reading there.
    As they say a little knowledge is a dangerous thing. It is taking
    me time to work my way through things, and then have a fiddle to
    try and consolidate what is read.

    >
    >> and have encountered an error message that I don't understand.
    >> 8 mail -s "Fire Report" chris < $(echo $message)
    >> results in the following error message:
    >> ./lesson2.sh: line 8: $(echo $message): ambiguous redirect

    >
    > Might have helped to have seen contents of $message.
    >
    > Based on what I think you have misunderstood so far, you should
    > have
    > done a mail -s "Fire Report" chris < $message
    >


    The code was more aimed at testing my understanding of various
    commands than being succinct. It looks like I simply
    missunderstood the use of the $( xxxxx yyyyyyy) construct.
    Jim Beard hit the nail on the head. (thanks Jim)
    After your comments, the code now looks like:

    [chris@develop scripts]$ cat -n lesson2.sh
    1 #!/bin/bash
    2
    3 # commented out, no data will come out of grep on this
    day
    4 # file="mnt/PDW_Logfiles/$(date +%y%m%d).flt"
    5
    6 # use a fixed value for now for debugging
    7 file="/mnt/PDW_Logfiles/080816.flt"
    8
    9 if [ -e $file ]
    10 then
    11 set -xv
    12 message=$(grep 1041138 $file | tr -d '[\r]' |
    cut -c 18-26,36- | uniq)
    13 mail -s "Fire Report" chris < $message
    14 echo "\n $message \n"
    15 set -
    16 else
    17 echo "The file ($file) does not exist!"
    18 fi


    running the script now produces:

    [chris@develop scripts]$ ./lesson2.sh
    grep 1041138 $file | tr -d '[\r]' | cut -c 18-26,36- | uniq
    ++ grep 1041138 /mnt/PDW_Logfiles/080816.flt
    ++ tr -d '[\r]'
    ++ cut -c 18-26,36-
    ++ uniq
    + message='16-08-08 149524 NEW NORFOLK (1) VEHICLE FIRE xxxxxxxx
    ROAD xxxxxxxx ROAD NEW NORFOLK
    16-08-08 149524 NEW NORFOLK (1) STOP MESSAGE // VEHICLE FIRE
    xxxxxxxx ROAD xxxxxxxx ROAD NEW NORFOLK - HAD A FIRE IN THE
    ENGINE BAY OF A FORD VAN FOLLOWING A DEGREASE, NOW
    EXTINGUISHED'
    + mail -s 'Fire Report' chris
    ../lesson2.sh: line 13: $message: ambiguous redirect
    + echo '\n 16-08-08 149524 NEW NORFOLK (1) VEHICLE FIRE xxxxxxxx
    ROAD xxxxxxxx ROAD NEW NORFOLK
    16-08-08 149524 NEW NORFOLK (1) STOP MESSAGE // VEHICLE FIRE
    xxxxxxxx ROAD xxxxxxxx ROAD NEW NORFOLK - HAD A FIRE IN THE
    ENGINE BAY OF A FORD VAN FOLLOWING A DEGREASE, NOW EXTINGUISHED
    \n'
    \n 16-08-08 149524 NEW NORFOLK (1) VEHICLE FIRE xxxxxxxx ROAD
    xxxxxxxx ROAD NEW NORFOLK
    16-08-08 149524 NEW NORFOLK (1) STOP MESSAGE // VEHICLE FIRE
    xxxxxxxx ROAD xxxxxxxx ROAD NEW NORFOLK - HAD A FIRE IN THE
    ENGINE BAY OF A FORD VAN FOLLOWING A DEGREASE, NOW EXTINGUISHED
    \n
    + set -

    * xxxxxxxx replaces info which may lead to identification of who
    had the fire. (I wonder if they now understand the flammability
    warning on the degreaser can?)

    It would be nice not to need a temporary file, but then as I
    understand it the use of them doesn't mess with the disk as much
    as that other popular OS


    Thanks again.

  5. Re: Ambiguous redirect in shell script

    On Sat, 16 Aug 2008 22:32:53 -0400, Chris Roy-Smith wrote:

    > 13 mail -s "Fire Report" chris < $message
    > + mail -s 'Fire Report' chris
    > ./lesson2.sh: line 13: $message: ambiguous redirect


    Try the following ...
    mail -s "Fire Report" chris << EOF
    $message
    ..
    EOF

    Note the the data portion of a mail message must end with a . on a line by itself.

    Regards, Dave Hodgins

    --
    Change nomail.afraid.org to ody.ca to reply by email.
    (nomail.afraid.org has been set up specifically for
    use in usenet. Feel free to use it yourself.)

  6. Re: Ambiguous redirect in shell script

    David W. Hodgins wrote:

    > On Sat, 16 Aug 2008 22:32:53 -0400, Chris Roy-Smith
    > wrote:
    >
    >> 13 mail -s "Fire Report" chris < $message
    >> + mail -s 'Fire Report' chris
    >> ./lesson2.sh: line 13: $message: ambiguous redirect

    >
    > Try the following ...
    > mail -s "Fire Report" chris << EOF
    > $message
    > .
    > EOF
    >
    > Note the the data portion of a mail message must end with a .
    > on a line by itself.
    >
    > Regards, Dave Hodgins
    >

    Thanks Dave

    Showing my ignorance here.....
    If I understand what you have said, then the resulting code
    should look like this:

    1 #!/bin/bash
    2
    3 # commented out, no data will come out of grep on this
    day
    4 # file="mnt/PDW_Logfiles/$(date +%y%m%d).flt"
    5
    6 # use a fixed value for debugging
    7 file="/mnt/PDW_Logfiles/080816.flt"
    8
    9 if [ -e $file ]
    10 then
    11 set -xv
    12 message=$(grep 1041138 $file | cut -c 18-26,36- |
    tr -d '[\r]' | uniq)
    13 echo $message
    14 mail -s "Fire Report" chris << EOF
    15 $message
    16 .
    17 EOF
    18 set -
    19 else
    20 echo "The file ($file) does not exist!"
    21 fi

    vi doesn't seem to like this as all the text after the first EOF
    is colored as if it were double quoted text ""

    attempting to execute the code gives:

    [chris@develop scripts]$ ./lesson2.sh
    ../lesson2.sh: line 22: syntax error: unexpected end of file

    I am thinking that I have miss-interpreted what you have said.
    The only reason for asking is that I'm trying to understand my
    mistake (and hopefully learn from it)

    The light-bulb moment reading the man page and info page for
    mail is yet to occurr.

    Thanks again to all the contributors
    Regards
    Chris Roy-Smith

  7. Re: Ambiguous redirect in shell script

    On Sun, 17 Aug 2008 02:18:51 -0400, Chris Roy-Smith wrote:

    > David W. Hodgins wrote:
    >> Try the following ...
    >> mail -s "Fire Report" chris << EOF
    >> $message
    >> .
    >> EOF


    > 14 mail -s "Fire Report" chris << EOF
    > 15 $message
    > 16 .
    > 17 EOF
    > attempting to execute the code gives:
    >
    > [chris@develop scripts]$ ./lesson2.sh
    > ./lesson2.sh: line 22: syntax error: unexpected end of file


    The data lines, and the EOF must start at the beginning of the lines.
    Don't put any spaces or tabs in there.

    Regards, Dave Hodgins

    --
    Change nomail.afraid.org to ody.ca to reply by email.
    (nomail.afraid.org has been set up specifically for
    use in usenet. Feel free to use it yourself.)

  8. Re: Ambiguous redirect in shell script

    Chris Roy-Smith wrote:
    > David W. Hodgins wrote:
    >
    >> On Sat, 16 Aug 2008 22:32:53 -0400, Chris Roy-Smith
    >> wrote:
    >>
    >>> 13 mail -s "Fire Report" chris < $message
    >>> + mail -s 'Fire Report' chris
    >>> ./lesson2.sh: line 13: $message: ambiguous redirect

    >> Try the following ...
    >> mail -s "Fire Report" chris << EOF
    >> $message
    >> .
    >> EOF
    >>
    >> Note the the data portion of a mail message must end with a .
    >> on a line by itself.
    >>
    >> Regards, Dave Hodgins
    >>

    > Thanks Dave
    >
    > Showing my ignorance here.....
    > If I understand what you have said, then the resulting code
    > should look like this:
    >
    > 1 #!/bin/bash
    > 2
    > 3 # commented out, no data will come out of grep on this
    > day
    > 4 # file="mnt/PDW_Logfiles/$(date +%y%m%d).flt"
    > 5
    > 6 # use a fixed value for debugging
    > 7 file="/mnt/PDW_Logfiles/080816.flt"
    > 8
    > 9 if [ -e $file ]
    > 10 then
    > 11 set -xv
    > 12 message=$(grep 1041138 $file | cut -c 18-26,36- |
    > tr -d '[\r]' | uniq)
    > 13 echo $message
    > 14 mail -s "Fire Report" chris << EOF
    > 15 $message
    > 16 .
    > 17 EOF
    > 18 set -
    > 19 else
    > 20 echo "The file ($file) does not exist!"
    > 21 fi
    >
    > vi doesn't seem to like this as all the text after the first EOF
    > is colored as if it were double quoted text ""
    >
    > attempting to execute the code gives:
    >
    > [chris@develop scripts]$ ./lesson2.sh
    > ./lesson2.sh: line 22: syntax error: unexpected end of file
    >
    > I am thinking that I have miss-interpreted what you have said.
    > The only reason for asking is that I'm trying to understand my
    > mistake (and hopefully learn from it)
    >
    > The light-bulb moment reading the man page and info page for
    > mail is yet to occurr.
    >
    > Thanks again to all the contributors
    > Regards
    > Chris Roy-Smith


    You may need EOT instead of EOF for your system.

    Cheers!

    jim b.

    --
    UNIX is not user unfriendly; it merely
    expects users to be computer-friendly.

  9. Re: Ambiguous redirect in shell script

    On Sun, 17 Aug 2008 02:18:51 -0400, Chris Roy-Smith wrote:

    > If I understand what you have said, then the resulting code
    > should look like this:
    > 14 mail -s "Fire Report" chris << EOF
    > 15 $message
    > 16 .
    > 17 EOF


    Close. Whatever string is used to delimit the input, in this case EOF, must
    be at the start of the line. Get rid of the leading spaces on the input to
    the mail command.

    14 mail -s "Fire Report" chris << EOF
    15 $message
    16 .
    17 EOF

    Regards, Dave Hodgins

    --
    Change nomail.afraid.org to ody.ca to reply by email.
    (nomail.afraid.org has been set up specifically for
    use in usenet. Feel free to use it yourself.)

  10. Re: Ambiguous redirect in shell script

    David W. Hodgins wrote:

    > On Sun, 17 Aug 2008 02:18:51 -0400, Chris Roy-Smith
    > wrote:
    >
    >> David W. Hodgins wrote:
    >>> Try the following ...
    >>> mail -s "Fire Report" chris << EOF
    >>> $message
    >>> .
    >>> EOF

    >
    >> 14 mail -s "Fire Report" chris << EOF
    >> 15 $message
    >> 16 .
    >> 17 EOF
    >> attempting to execute the code gives:
    >>
    >> [chris@develop scripts]$ ./lesson2.sh
    >> ./lesson2.sh: line 22: syntax error: unexpected end of file

    >
    > The data lines, and the EOF must start at the beginning of the
    > lines. Don't put any spaces or tabs in there.
    >
    > Regards, Dave Hodgins
    >

    Yes! that fixed it. Thanks for the explaining what I
    misunderstood. I didn't realize that I could redirect that way,
    I thought I had to type that input myself with that construct.

    Is it appropriate to post shell script questions here, or do I
    use the "comp.unix.shell" group? (I don't want to offend
    anyone.)

    Regards Chris Roy-Smith

  11. Re: Ambiguous redirect in shell script

    Jim Beard wrote:

    > Chris Roy-Smith wrote:
    >> David W. Hodgins wrote:
    >>
    >>> On Sat, 16 Aug 2008 22:32:53 -0400, Chris Roy-Smith
    >>> wrote:
    >>>
    >>>> 13 mail -s "Fire Report" chris < $message
    >>>> + mail -s 'Fire Report' chris
    >>>> ./lesson2.sh: line 13: $message: ambiguous redirect
    >>> Try the following ...
    >>> mail -s "Fire Report" chris << EOF
    >>> $message
    >>> .
    >>> EOF
    >>>
    >>> Note the the data portion of a mail message must end with a .
    >>> on a line by itself.
    >>>
    >>> Regards, Dave Hodgins
    >>>

    >> Thanks Dave
    >>
    >> Showing my ignorance here.....
    >> If I understand what you have said, then the resulting code
    >> should look like this:
    >>
    >> 1 #!/bin/bash
    >> 2
    >> 3 # commented out, no data will come out of grep on this
    >> day
    >> 4 # file="mnt/PDW_Logfiles/$(date +%y%m%d).flt"
    >> 5
    >> 6 # use a fixed value for debugging
    >> 7 file="/mnt/PDW_Logfiles/080816.flt"
    >> 8
    >> 9 if [ -e $file ]
    >> 10 then
    >> 11 set -xv
    >> 12 message=$(grep 1041138 $file | cut -c
    >> 18-26,36- |
    >> tr -d '[\r]' | uniq)
    >> 13 echo $message
    >> 14 mail -s "Fire Report" chris << EOF
    >> 15 $message
    >> 16 .
    >> 17 EOF
    >> 18 set -
    >> 19 else
    >> 20 echo "The file ($file) does not exist!"
    >> 21 fi
    >>
    >> vi doesn't seem to like this as all the text after the first
    >> EOF is colored as if it were double quoted text ""
    >>
    >> attempting to execute the code gives:
    >>
    >> [chris@develop scripts]$ ./lesson2.sh
    >> ./lesson2.sh: line 22: syntax error: unexpected end of file
    >>
    >> I am thinking that I have miss-interpreted what you have said.
    >> The only reason for asking is that I'm trying to understand my
    >> mistake (and hopefully learn from it)
    >>
    >> The light-bulb moment reading the man page and info page for
    >> mail is yet to occurr.
    >>
    >> Thanks again to all the contributors
    >> Regards
    >> Chris Roy-Smith

    >
    > You may need EOT instead of EOF for your system.
    >
    > Cheers!
    >
    > jim b.
    >

    Thanks Jim,
    both EOT and EOF work, so now I know an extra bit!
    regards Chris Roy-Smith.

  12. Re: Ambiguous redirect in shell script

    On Mon, 18 Aug 2008 10:27:22 +1000, Chris Roy-Smith wrote:

    > Is it appropriate to post shell script questions here, or do I
    > use the "comp.unix.shell" group? (I don't want to offend anyone.)


    Always better to post to the appropriate news group.

    Much better chance of getting a Subject Matter Expert to
    respond/review your question.

    And as always check http://groups.google.com/advanced_search
    to see if answers have not already been supplied.

    PS: About that tab reply
    mail -s "Fire Report" chris <<-EOF
    Note: <<- lets you use tab EOF as text terminator
    EOF

  13. Re: Ambiguous redirect in shell script

    On Sun, 17 Aug 2008 20:27:22 -0400, Chris Roy-Smith wrote:

    > Yes! that fixed it. Thanks for the explaining what I
    > misunderstood. I didn't realize that I could redirect that way,
    > I thought I had to type that input myself with that construct.


    Btw, the EOF can be replaced with any text string that is not in
    the data. Whatever string is used has to be on a line byitself,
    to be detected as the end of the redirected input.

    > Is it appropriate to post shell script questions here, or do I
    > use the "comp.unix.shell" group? (I don't want to offend
    > anyone.)


    While you'll find more bash experts elsewhere, and may get a faster
    or better answer, I certainly don't mind script questions here. After
    all, bash is part of Mandriva linux.

    Regards, Dave Hodgins

    --
    Change nomail.afraid.org to ody.ca to reply by email.
    (nomail.afraid.org has been set up specifically for
    use in usenet. Feel free to use it yourself.)

+ Reply to Thread