Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed doesn't work from within bash script

Tags:

bash

replace

sed

I've searched for hours looking for the answer to this question which seems frustratingly simple...

I have a bash script which I've simplified to find the line that's stopping it from working and am left with:

#!/bin/bash
#
sed -i -e "s/<link>/\n/g" /usb/lenny/rss/tmp/rss.tmp

If I run this script, nothing happens to the file rss.tmp - but if I call this exact same sed command from the terminal, it makes all the replacements as expected.

Anyone have any idea what I'm doing wrong here?

like image 624
Jamie Avatar asked Nov 26 '22 19:11

Jamie


1 Answers

Based on the discussion the issue sounds like it is a cygwin shell problem. The issue is that shell scripts may not have \r\n line terminations - they need \n terminations. Earlier versions of cygwin behaved differently. The relevant section from a Cygwin FAQ at http://cs.nyu.edu/~yap/prog/cygwin/FAQs.html

Q: Mysterious errors in shell scripts, .bashrc, etc


    A: You may get mysterious messages when bash reads
    your .bashrc or .bash_profile, such as
        "\r command not found"
    (or similar).  When you get rid of empty lines, the
    complaints about "\r" disappears, but probably other
    errors remain.  What is going on?

    The answer may lie in the fact that a text file (also
    called ASCII file) can come in two formats:
    in DOS format or in UNIX format. 
    Most editors can automatically detect the formats
    and work properly in either format.
    In the DOS format, a new line is represented by two characters:
    CR (carriage return or ASCII code 13) and LF (line feed or ASCII code 15).
    In the UNIX format, a new line is represented by only
    one character, LF.  When your .bashrc file is read,
    bash thinks the extra character is the name of a command,
    hence the error message.

    In Cygwin or unix, you can convert a file INFILE in DOS format
    to a file OUTFILE in Unix format by calling:

        > tr -d '\15'  OUTFILE

    NOTE:
    If you now compare the number of characters in INFILE and OUTFILE,
    you will see that the latter has lost the correct
    number of characters (i.e., the number of lines in INFILE):

        > wc INFILE OUTFILE
like image 103
joemooney Avatar answered Feb 08 '23 19:02

joemooney