Chef messing up sed commmand


#1

Hi

I am trying to get a bash script to work that has a sed command in it. The sed
command has escape characters in it. For some reason Chef seems to be taking
these escape characters out and trying to execute the sed script which fails.
The sed script works in the console

Below is the original test script I made. This works as expected.

if [ $(grep -c postrotate 1) -ne 0 ]; then if [ (grep -c checksum_log.sh $1) -ne 1 ]; then
sed -i ‘s/endscript//usr/bin/checksum_log.sh $1\n endscript/’ $1
fi
fi

The Bash script block in Chef is the same as above contained within a <<-EOF
block with $1 being replaced with a file name.

The error in chef is concerning the sed command itself.

Is there any special escaping I need to do?

Thanks.

Kind Regards
Hamza Khan-Cheema


#2

Hi.

Its OK, I found the solution. You need to double up the escape characters
for it to work.

Thanks
Hamza Khan-Cheema
On 22 Apr 2013 10:48, hamza@khan-cheema.com wrote:

Hi

I am trying to get a bash script to work that has a sed command in it.
The sed
command has escape characters in it. For some reason Chef seems to be
taking
these escape characters out and trying to execute the sed script which
fails.
The sed script works in the console

Below is the original test script I made. This works as expected.

if [ $(grep -c postrotate 1) -ne 0 ]; then if [ (grep -c checksum_log.sh $1) -ne 1 ]; then
sed -i ‘s/endscript//usr/bin/checksum_log.sh $1\n endscript/’ $1
fi
fi

The Bash script block in Chef is the same as above contained within a
<<-EOF
block with $1 being replaced with a file name.

The error in chef is concerning the sed command itself.

Is there any special escaping I need to do?

Thanks.

Kind Regards
Hamza Khan-Cheema


#3

On Apr 22, 2013, at 12:48 AM, hamza@khan-cheema.com wrote:

Hi

I am trying to get a bash script to work that has a sed command in it. The sed
command has escape characters in it. For some reason Chef seems to be taking
these escape characters out and trying to execute the sed script which fails.
The sed script works in the console

Below is the original test script I made. This works as expected.

if [ $(grep -c postrotate 1) -ne 0 ]; then if [ (grep -c checksum_log.sh $1) -ne 1 ]; then
sed -i ‘s/endscript//usr/bin/checksum_log.sh $1\n endscript/’ $1
fi
fi

The Bash script block in Chef is the same as above contained within a <<-EOF
block with $1 being replaced with a file name.

Make that <<-‘EOF’ to make it a non-interpolated string block, otherwise you have to deal with Ruby string formatting on top of Bash formatting. You can also use %q[…]

–Noah


#4

Thanks for the quick reply :slight_smile:

I didn’t know about <<-‘EOF’

Kind Regards
Hamza Khan-Cheema
On 22 Apr 2013 11:05, “Noah Kantrowitz” noah@coderanger.net wrote:

On Apr 22, 2013, at 12:48 AM, hamza@khan-cheema.com wrote:

Hi

I am trying to get a bash script to work that has a sed command in it.
The sed
command has escape characters in it. For some reason Chef seems to be
taking
these escape characters out and trying to execute the sed script which
fails.
The sed script works in the console

Below is the original test script I made. This works as expected.

if [ $(grep -c postrotate 1) -ne 0 ]; then if [ (grep -c checksum_log.sh $1) -ne 1 ]; then
sed -i ‘s/endscript//usr/bin/checksum_log.sh $1\n endscript/’ $1
fi
fi

The Bash script block in Chef is the same as above contained within a
<<-EOF
block with $1 being replaced with a file name.

Make that <<-‘EOF’ to make it a non-interpolated string block, otherwise
you have to deal with Ruby string formatting on top of Bash formatting. You
can also use %q[…]

–Noah