1

I have a shell script that gets called like this

ex file1 file2 file3 word1 word2 .... wordn

I what to have file1 file2 file3 in an array and I tried the following

for i in $(seq 1 3)
do
FILES[$i]=$0;
` expr shift `;
echo "FILES["$i"]="$FILES[$i];
done;

So I want in a sequence to shift 3 times the arguments and save them like this...but it gives me the error :

./ex: 11: ./ex: FILES[1]=./ex: not found
FILES[1]=[1]
./ex: 11: ./ex: FILES[2]=./ex: not found
FILES[2]=[2]
./ex: 11: ./ex: FILES[3]=./ex: not found
FILES[3]=[3]

Also I tried like this :

for i in $(seq 1 3)
do
FILES[$i]=$i;  // this actually means 1,2 or 3;I would like to mean $1,...
` expr shift `;
echo "FILES["$i"]="$FILES[$i];
done;

To make it be $1 not 1 I tried something like $($i) or ${$i} but says is bad substitution . Thanks !

4
  • took me a while to understand your last question, but I did now. If still needed, what you can do is: for i in {1..3}; do echo "${!i}"; done. or, of course, alternatively FILES[i]="${!i}" Commented Jun 16, 2014 at 12:20
  • Solved it eventually. But still thanks! Nice to know this too ! Commented Jun 16, 2014 at 12:39
  • FYI -- it would be more in line with best practices / convention to name your array files. All-uppercase names are reserved for builtins and environment variables; when you use them for your own purposes, you risk overwriting an environment variable by mistake. Commented Jun 16, 2014 at 15:05
  • Also, seq is a nonstandard command. for ((i=1; i<3; i++)); do would be the more portable (within platforms having bash, not POSIX sh) and efficient approach, working on platforms that don't have seq, and not having the overhead of starting an external program just to count some numbers! Commented Jun 16, 2014 at 15:07

2 Answers 2

2
#!/bin/bash
FILES=("${@:1:3}")
shift 3

the double quotes allow files names to contain spaces if they are enclosed by quotes, and still occupy one single array item. If that is undesired, leave the quotes out. Parameters for first item and number of items may be variables, no need for literal values there.

Sign up to request clarification or add additional context in comments.

Comments

1

Do this:

for i in {1..3}; do
    FILES[$i]=$1
    shift
done

The first argument is $1, not $0, that's the name of the script. And you don't put shift in backticks; that substitutes the output back into the command line, which is why you're getting an error when it tries to execute that as a command.

4 Comments

I will have actually more than the 3 files. I will update the question ! Thanks although ! Didn't know I can do that !
I made the changes and I called it like this ex file1 file2 file3 word1 and it gives me the following error :./ex: 9: ./ex: FILES[{1..3}]=file1: not found Line 9 is FILES[$i]=$1;
Do you have #!/bin/bash at the beginning of your script? That looks like it's running without support for arrays.
bash doesn't need $ with variables for array indici. FILES[i] is ok too, but the $ sign doesn't hurt. you may want to consider to double quote $1 though. disadvantage of brace expansion is that they work with literal values only, not with variables - just as hint if OP goes that route and finds out later ("...will have actually more than the 3 files")

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.