Shell Script Errors

I’m having a hard time making a simple script work on a QNX 6.1.0 x86
system. I’m not much of a scripter, so any suggestions would be appreciated.

#!/bin/sh
bu_file=“backup_”date +%m%d%y_%H%M
echo “Backup of /home/servo/lib and /home/servo/bin directories”
echo “Output File will be " $bu_file”.tar"
if [ $1 == “-P” ]
then
echo “Absolute pathnames will be used”
PATH_PREFIX=pwd
tar -cPvf $bu_file.tar $PATH_PREFIX/servo/lib/*
$PATH_PREFIX/servo/bin/*
else
echo “Relative pathnames will be used”
tar -cvf $bu_file.tar lib/* bin/*
fi

When I execute the script with the command, sh bs, the fact that the “-P”
flag is missing is ignored.
The output follows
Backup of /home/servo/lib and /home/servo/bin directories
Output File will be backup_021803_1834.tar
bs[13]: [: -P: unexpected operator/operand
Relative pathnames will be used


Sincerely,
David Kuechenmeister

David Kuechenmeister <david.kuechenmeister@viasat.com> wrote:

I’m having a hard time making a simple script work on a QNX 6.1.0 x86
system. I’m not much of a scripter
if [ $1 == “-P” ]

There is a thread called “shell questions” in qdn.public.qnxrtp.devtools
which you may find very relevant to this problem …

Thanks.

“John Garvey” <jgarvey@qnx.com> wrote in message
news:b2u1k1$j2g$1@nntp.qnx.com

David Kuechenmeister <> david.kuechenmeister@viasat.com> > wrote:
I’m having a hard time making a simple script work on a QNX 6.1.0 x86
system. I’m not much of a scripter
if [ $1 == “-P” ]

There is a thread called “shell questions” in qdn.public.qnxrtp.devtools
which you may find very relevant to this problem …

I’m having a hard time making a simple script work on
a QNX 6.1.0 x86
system. I’m not much of a scripter, so any
suggestions would be appreciated.

#!/bin/sh
bu_file=“backup_”date +%m%d%y_%H%M
echo “Backup of /home/servo/lib and /home/servo/bin
directories”
echo “Output File will be " $bu_file”.tar"
if [ $1 == “-P” ]

Change the line above to the following:

if [ ${1:-none} = “-P” ]

then
echo “Absolute pathnames will be used”
PATH_PREFIX=pwd
tar -cPvf $bu_file.tar
ile.tar $PATH_PREFIX/servo/lib/*
$PATH_PREFIX/servo/bin/*
else
echo “Relative pathnames will be used”
tar -cvf $bu_file.tar lib/* bin/*
fi

When I execute the script with the command, sh bs,
the fact that the “-P”
flag is missing is ignored.
The output follows
Backup of /home/servo/lib and /home/servo/bin
directories
Output File will be backup_021803_1834.tar
bs[13]: [: -P: unexpected operator/operand
Relative pathnames will be used


Sincerely,
David Kuechenmeister

Serge Yuschenko wrote:

if [ $1 == “-P” ]

Change the line above to the following:

if [ ${1:-none} = “-P” ]

Actually, for that to work, you need the double-equal-signs: “==”

The shell doesn’t treat “variables” in the same way C does. It does not
compare the data at a memory location like C. When the shell is
interpreting the script, it rewrites it and SUBSTITUTES whatever was
given as $1 wherever it sees “$1” in the script. If nothing was given to
the shell as $1, the shell substitutes nothing everywhere it sees “$1”
in the script.

What’s happening in your case is that:

if [ $1 == “-P” ]

is being rewritten by the shell as:

if [ == “-P” ]

when you don’t specify a $1 at the command line. The result is a syntax
error.

The solution is to rewrite the line so there will always be something
there as a placeholder, so the shell substitution doesn’t mung your
code. One common way is this:

if [ “X$1” == “X-P” ]

Prepending a junk character to both items being compared ensures that
something will be there to compare when the time comes.

Thanks for the suggestions. I was looking at the afore-mentioned thread in
the other group and settled on the syntax,
if [ “$1” == “-P” ]
which seems to work well in either the presence or absence of a command line
parameter.

Sincerely,
David Kuechenmeister

“Mathew Kirsch” <mkirsch@ocdus.jnj.com> wrote in message
news:b2vs9a$2qq$1@inn.qnx.com

Serge Yuschenko wrote:
if [ $1 == “-P” ]

Change the line above to the following:

if [ ${1:-none} = “-P” ]

Actually, for that to work, you need the double-equal-signs: “==”

The shell doesn’t treat “variables” in the same way C does. It does not
compare the data at a memory location like C. When the shell is
interpreting the script, it rewrites it and SUBSTITUTES whatever was
given as $1 wherever it sees “$1” in the script. If nothing was given to
the shell as $1, the shell substitutes nothing everywhere it sees “$1”
in the script.

What’s happening in your case is that:

if [ $1 == “-P” ]

is being rewritten by the shell as:

if [ == “-P” ]

when you don’t specify a $1 at the command line. The result is a syntax
error.

The solution is to rewrite the line so there will always be something
there as a placeholder, so the shell substitution doesn’t mung your
code. One common way is this:

if [ “X$1” == “X-P” ]

Prepending a junk character to both items being compared ensures that
something will be there to compare when the time comes.

Mathew Kirsch <mkirsch@ocdus.jnj.com> wrote:

Serge Yuschenko wrote:
if [ $1 == “-P” ]

Change the line above to the following:

if [ ${1:-none} = “-P” ]

Actually, for that to work, you need the double-equal-signs: “==”

Actually, you don’t. Both “==” and “=” seem to be defined on most
systems I checked, but the single “=” has the advantage of being POSIX:

http://www.opengroup.org/onlinepubs/007904975/utilities/test.html
http://www.gnu.org/manual/bash-2.05a/html_node/bashref_68.html#SEC75
http://www.research.att.com/~gsf/man/man1/test.html
http://www.freebsd.org/cgi/man.cgi?query=test&manpath=FreeBSD+4.7-RELEASE&format=html