Discussion:
extra apostrophe in bash script
(too old to reply)
Burkhard Schultheis
2018-07-19 14:07:27 UTC
Permalink
We have the following statement in a bash script running on AIX 7.2:

[ -n "$(grep \"^$2=\" \"$1\" 2>/dev/null)" ]

During run, $2 gets the value nagios_retcode= and $1 gets
/var/lib/ibm72dev_backup_state.txt.

This statement does not work because the resulting line looks like

grep '"^nagios_retcode="' '"/var/lib/ibm72dev_backup_state.txt"'

On Linux it does work, but not on AIX. Where does the extra apostrophes
come from? And how to avoid it?

Thanks in advance for your help!

Regards
Burkhard
Burkhard Schultheis
2018-07-19 14:50:38 UTC
Permalink
Post by Burkhard Schultheis
[ -n "$(grep \"^$2=\" \"$1\" 2>/dev/null)" ]
During run, $2 gets the value nagios_retcode= and $1 gets
/var/lib/ibm72dev_backup_state.txt.
This statement does not work because the resulting line looks like
grep '"^nagios_retcode="' '"/var/lib/ibm72dev_backup_state.txt"'
On Linux it does work, but not on AIX. Where does the extra apostrophes
come from? And how to avoid it?
Thanks in advance for your help!
Regards
Burkhard
You can try the following script:

#!/bin/bash

P1="^nagios_update="
P2="/var/lib/ibm72dev_backup_state.txt"
echo "P1: $P1, P2: $P2"
[ -n "$(grep \"^$P1=\" \"$P2\" 2>/dev/null)" ]
RC=$?
echo "RC: $RC"
if [ $RC -ne 0 ]
then
echo "does not work"
else
echo "OK"
fi

On my AIX system it gives "does not work, P1 and P2 are shown without
any quotes.

Regards
Burkhard
Burkhard Schultheis
2018-07-19 18:38:46 UTC
Permalink
Post by Burkhard Schultheis
Post by Burkhard Schultheis
[ -n "$(grep \"^$2=\" \"$1\" 2>/dev/null)" ]
During run, $2 gets the value nagios_retcode= and $1 gets
/var/lib/ibm72dev_backup_state.txt.
This statement does not work because the resulting line looks like
grep '"^nagios_retcode="' '"/var/lib/ibm72dev_backup_state.txt"'
On Linux it does work, but not on AIX. Where does the extra
apostrophes come from? And how to avoid it?
Thanks in advance for your help!
Regards
Burkhard
#!/bin/bash
P1="^nagios_update="
P2="/var/lib/ibm72dev_backup_state.txt"
echo "P1: $P1, P2: $P2"
[ -n "$(grep \"^$P1=\" \"$P2\" 2>/dev/null)" ]
[ -n "$(grep \"$P1\" \"$P2\" 2>/dev/null)" ]
Post by Burkhard Schultheis
RC=$?
echo "RC: $RC"
if [ $RC -ne 0 ]
then
        echo "does not work"
else
        echo "OK"
fi
Regards
Burkhard
Burkhard Schultheis
2018-07-20 08:07:41 UTC
Permalink
Post by Burkhard Schultheis
[ -n "$(grep \"^$2=\" \"$1\" 2>/dev/null)" ]
During run, $2 gets the value nagios_retcode= and $1 gets
/var/lib/ibm72dev_backup_state.txt.
This statement does not work because the resulting line looks like
grep '"^nagios_retcode="' '"/var/lib/ibm72dev_backup_state.txt"'
On Linux it does work, but not on AIX. Where does the extra apostrophes
come from? And how to avoid it?
Thanks in advance for your help!
In the meantime we have solved the problem: :-)

grep_result=$(grep "^$2=" "$1")
[ -n "$grep_result" ]

Regards
Burkhard
Lorinczy Zsigmond
2018-07-29 08:25:03 UTC
Permalink
Post by Burkhard Schultheis
[ -n "$(grep \"^$2=\" \"$1\" 2>/dev/null)" ]
Just for the record: the \backslashes should be removed:
[ -n "$(grep "^$2=" "$1" 2>/dev/null)" ]

Loading...