1

I am trying to write a script to execute a couple of different java/r programs but I am having an issue with using a value from an array in bash. Here is my example:

INPUTS[0]=$1 #file
INPUTS[1]=$2 #file
INPUTS[2]=$3 #file
INPUTS[3]=$4 #file
TARGET=$5 #file
OUTPUTS[0]=$6 #file
OUTPUTS[1]=$7 #file
OUTPUTS[2]=$8 #file
OUTPUTS[3]=$9 #file
TYPE=$10 #file
PANEL=$11 #String

for ((X=0;X<${#INPUTS[@]};X++));do #for every file in this array
   CURRENT="${INPUTS[X]}" #look at current in file
   CURRENTOUT="${OUTPUTS[X]}" #and current out file
   #this is working just fine. I put in a file myfile.txt and it makes a new file **results.txt**
   java -jar javathing.jar INPUT=$CURRENT TARG=$TARGET OUTPUT=$CURRENTOUT IS=True #run this
   #This is throwing an error because it can't find **results.txt0**
   java -jar morejava.jar -i $CURRENTOUT -o $TYPE #then this
done
Rscript rthing.R $TYPE $PANEL #finally

So my first java outputs the correct file with a correct file name- results.txt

But when my second java program goes to look for it, it looks for the incorrect name. It looks for results.txt0

I am using the same variable for each one, but it doesn't seem to have the same effect. Can anyone explain this behavior?

5
  • 1
    For starters, // isn't a Bash comment character. Commented Oct 11, 2013 at 17:16
  • @CodeGnome I don't have those in my actual code. I was commenting just for the sake of this post. Does Stackoverflow have a comment character? As it seems there are at least 2 nitpickers I will change it. Commented Oct 11, 2013 at 17:17
  • Use the comment character of the language you're writing. # for bash Commented Oct 11, 2013 at 17:18
  • There's no way the variable CURRENTOUT can change between those two statements. Commented Oct 11, 2013 at 17:20
  • Not related to your problem, but you can set the arrays more concisely with INPUT=( "${@:1:4}" ) and OUTPUT=( "${@:6:4}" ). Commented Oct 11, 2013 at 17:25

1 Answer 1

3

Fix these lines:

TYPE=$10 #file
PANEL=$11 #String

They should be:

TYPE=${10} #file
PANEL=${11} #String

From the bash man page:

When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces (see EXPANSION below).

What you wrote was interpreted as:

TYPE=${1}0
PANEL=${1}1
Sign up to request clarification or add additional context in comments.

3 Comments

Why would I do this for these two variables, but none of the others?
Because the other positional parameters only have 1 digit.
You can use the braces for the others if you like, you just don't need to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.