Operating System - Linux
1826614 Members
2998 Online
109695 Solutions
New Discussion

Bash script from SCO doesn't work under RH ES 5

 
SOLVED
Go to solution
Debbie Fleith
Regular Advisor

Bash script from SCO doesn't work under RH ES 5

I am having a problem with one of our bash scripts we used on SCO that now don't work on our new platform RH ES 5.
In the following example I want INFILE to be set to the value of the last parameter that I pass to the script. Instead of this I get INFILE set to ‘1’ if I pass one parameter or ‘2’ if I pass 2 parameters to the script.

We use this syntax on SCO Unix in many scripts. Does anyone have any idea of a workaround or what the correct syntax should be?

--------------------------------------

#! /bin/bash

if [ "$#" != 0 ]
then
INFILE=`eval echo $"$#"`
echo $INFILE
fi
1 REPLY 1
Heironimus
Honored Contributor
Solution

Re: Bash script from SCO doesn't work under RH ES 5

Your variables are getting interpolated multiple times and in the wrong order, so it's trying to expand $" as a variable too early. You need to escape the first dollar sign:

INFILE=`eval echo \\$"$#"`

But I think you could probably make it a little simpler with something like this:

eval INFILE="\$$#"

The different behaviors may be caused by different versions of bash. Newer versions are more strictly POSIX-compliant.