jason dancks
jason dancks

Reputation: 1142

bash: sed: unexpected behavior: displays everything

I wrote what I thought was a quick script I could run on a bunch of machines. Instead it print what looks like might be directory contents in a recursive search:

version=$(mysql Varnish -B --skip-column-names -e "SELECT value FROM sys_param WHERE param='PatchLevel'" | sed -n 's/^.*\([0-9]\.[0-9]*\).*$/\1/p')
if [[ $(echo "if($version == 6.10) { print 1; } else { print 0; }" | bc) -eq 1 ]]; then
    status=$(dpkg-query -l | awk '{print $2}' |  grep 'sg-status-polling');
    cons=$(dpkg-query -l | awk '{print $2}' |  grep 'sg-consolidated-poller');
    if [[ "$status" != "" && "$cons" != "" ]]; then
        echo "about to change /var/www/Varnish/lib/Extra/SG/ObjectPoller2.pm"; echo;
        cp /var/www/Varnish/lib/Extra/SG/ObjectPoller2.pm /var/www/Varnish/lib/Extra/SG/ObjectPoller2.pm.bkup;
        sed -ir '184s!\x91\x93!\x91\x27--timeout=35\x27\x93!'  /var/www/Varnish/lib/Extra/SG/ObjectPoller2.pm;
        sed -n 183,185p /var/www/Varnish/lib/Extra/SG/ObjectPoller2.pm; echo;
    else
        echo "packages not found. Assumed to be not applicable";
    fi
else
    echo "This is 4.$version, skipping";
fi

The script is supposed to make sure Varnish is version 4.6.10 and has 2 custom .deb packages installed (not through apt-get). then makes a backup and edits a single line in a perl module from [] to ['--timeout=35']

it looks like its tripping up on the sed replace one liner.

Upvotes: 0

Views: 58

Answers (1)

There are two major problems (minor ones addressed in comments). The first is that you use the decimal code for [] instead of the hexa, so you should use \x5b\x5d instead of \x91\x93. The second problem is that if you do use the proper codes, sed will still interpret those syntactically as []. So you can't escape escaping. Here's what you should call:

sed -ri'.bkup' '184s!\[\]![\x27--timeout=35\x27]!' /var/www/Varnish/lib/Extra/SG/ObjectPoller2.pm

And this will create the backup for you (but you should double check).

Upvotes: 1

Related Questions