Operating System - Linux
1753521 Members
4831 Online
108795 Solutions
New Discussion юеВ

Really weird script problem

 
SOLVED
Go to solution
John Atkinson_2
New Member

Really weird script problem

When I run the following script the result depends on what directory I am in. The script is in /tmp.

#!/bin/sh

DATABASE=AP0041D

echo "1: [${DATABASE}]"
echo "2: [$DATABASE]"
echo 3: [${DATABASE}]
echo 4: [$DATABASE]

When run from any directory except / the output is:
$ ./quotes.sh
1: [AP0041D]
2: [AP0041D]
3: [AP0041D]
4: [AP0041D]

But from / the output is:
$ /tmp/quotes.sh
1: [AP0041D]
2: [AP0041D]
3: 1
4: 1

This only happens in HP-UX. I am using HP-UX 11.11 on PA-RISC. On several other versions of Unix (AIX and SuSE SLES 8), the output is the same as the first output even when run from /

Any ideas on how to prevent this inconsistency?
10 REPLIES 10
Arunvijai_4
Honored Contributor

Re: Really weird script problem

Can you add set -vx in your script and execute in 11.11 (PA) ?

-- Arun
"A ship in the harbor is safe, but that is not what ships are built for"
John Atkinson_2
New Member

Re: Really weird script problem

run from / with set -vx

$ /tmp/quotes3.sh
DATABASE=AP0041D
+ DATABASE=AP0041D

echo "1: [${DATABASE}]"
+ echo 1: [AP0041D]
1: [AP0041D]
echo "2: [$DATABASE]"
+ echo 2: [AP0041D]
2: [AP0041D]
echo 3: [${DATABASE}]
+ echo 3: 1
3: 1
echo 4: [$DATABASE]
+ echo 4: 1
4: 1
RAC_1
Honored Contributor

Re: Really weird script problem

Looks that script is doing what it is supposed to do. Posting of script will help.
Also make sure you define all required variables. Also in all variables, use ${XX}

Also set -u will error out, when it encounters unset variable.
There is no substitute to HARDWORK
Stephen Keane
Honored Contributor
Solution

Re: Really weird script problem

Do you, per chance, have a file in "/" named "1", if so (and if you can safely do so) can you remove it/rename it and re-run your script in "/"
Muthukumar_5
Honored Contributor

Re: Really weird script problem

post information on,

# what /sbin/sh

#!/bin/sh

DATABASE=""

echo "1: [${DATABASE}]"
echo "2: [$DATABASE]"
echo 3: [${DATABASE}]
echo 4: [$DATABASE]

#!/bin/sh

DATABASE="AP0041D"

echo "1: [${DATABASE}]"
echo "2: [$DATABASE]"
echo 3: [${DATABASE}]
echo 4: [$DATABASE]

#!/bin/sh

DATABASE=A

echo "1: [${DATABASE}]"
echo "2: [$DATABASE]"
echo 3: [${DATABASE}]
echo 4: [$DATABASE]

hth.
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Really weird script problem

What stev specified is perfectly right. Just take a look at this.

# /tmp/weird.sh (executed from root)
1: [AP0041D]
2: [AP0041D]
3: [AP0041D]
4: [AP0041D]

# touch 1

# /tmp/weird.sh

1: [AP0041D]
2: [AP0041D]
3: 1
4: 1

# rm -f 1

# /tmp/weird.sh
1: [AP0041D]
2: [AP0041D]
3: [AP0041D]
4: [AP0041D]
"A ship in the harbor is safe, but that is not what ships are built for"
Stephen Keane
Honored Contributor

Re: Really weird script problem

You'll find that if you have a file by the name of 'A', 'P', '0', '4', '1' or 'D' you'll get a similar effect. Do you see why?
john korterman
Honored Contributor

Re: Really weird script problem

Hi,

i "unprotected mode" i.e. if not protected by quotes, e.g. lines 3 and 4 in your script - the shell expands wildcards to filenames in thecurrent directory.
Compare:
# ls [AP041D]

which will match the file names A, P, 0, 4, 1, or D in the directory you are in.


regards,
John K.
it would be nice if you always got a second chance
Zeev Schultz
Honored Contributor

Re: Really weird script problem

man sh-posix:

"Braces are required when parameter is followed by a letter, digit, or underscore that should not be interpreted as part of its name or when a named parameter is subscripted. If parameter is one or more digits, it is a positional parameter.A positional parameter of more than one digit must be enclosed in braces."

And :

Inside double quote marks (""), parameter and command substitution occurs...

Meaning, last two echoes doesn't do the substitution...
So computers don't think yet. At least not chess computers. - Seymour Cray