0

I have been trying to figure this out for hours and it just keeps giving me problems. I'm trying to pass 2 delimiter-containing strings as paramters to a bash script, iterate through them and echo the corresponding value 1,2,3 etc from array 1 in array 2's iteration

#!/bin/sh

export IFS='@@'
ThumbFilenames=$1

counterFiles=1

for thumbFilename in $ThumbFilenames; do
        thumbFile[${counterFiles}]="${thumbFilename}"
        counterFiles=$((counterFiles+1))
done

ThumbsIn=$2
counterThumbs=1
for thumbnumber in $ThumbsIn; do
        echo "${thumbFile[${counterThumbs}]}"
        echo "\n"
        counterThumbs=$((counterThumbs+1))
done

however, running

./script.sh file1@@file2@@file3@@file4 thumb1@@thumb2@@thumb3@@thumb4

it just gives me this output

./script.sh: 9: thumbFile[1]=file1: not found
./script.sh: 9: thumbFile[2]=: not found
./script.sh: 9: thumbFile[3]=file2: not found
./script.sh: 9: thumbFile[4]=: not found
./script.sh: 9: thumbFile[5]=file3: not found
./script.sh: 9: thumbFile[6]=: not found
./script.sh: 9: thumbFile[7]=file4: not found

the output i need is

file1
file2
file3
file4
b747fp
  • 175
  • 3
  • 12
  • no error from my opensuse but IFS separator seems failing with this code by loading 1 file name than an empty one like in your code (normal due to behaviour of IFS with more than 1 character length) – NeronLeVelu Oct 26 '15 at 06:52
  • For adding variables to an array, have a look at [this answer](http://stackoverflow.com/a/18041780/1765658) – F. Hauri - Give Up GitHub Oct 26 '15 at 08:25

2 Answers2

1

IFS supports only single character delimiter. You also should use /bin/bash in shebang instead of /bin/sh.

You script can be like this:

#!/usr/bin/env bash

export IFS='@'
ThumbFilenames="${1//@@/@}"

thumbFile=()

for thumbFilename in $ThumbFilenames; do
   thumbFile+=("$thumbFilename")
done

ThumbsIn="${2//@@/@}"
counterThumbs=0
for thumbnumber in $ThumbsIn; do
   echo "${thumbFile[${counterThumbs}]}"
   ((counterThumbs++))
done

Output:

file1
file2
file3
file4
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    or better `#!/usr/bin/env bash` – Andreas Louv Oct 26 '15 at 07:15
  • 2
    Not necessarily though. If you're writing a script that requires a high level of security or a certain version of bash, `/bin/bash` is perhaps a better choice, since otherwise you might pick up whatever outdated or malicious `bash` executable in the PATH. (For the record, I usually use `/usr/bin/env bash`.) – 4ae1e1 Oct 26 '15 at 07:27
0

You are specifiing sh as the shell for the script. That (quite old) shell does not support arrays, thus all var[index] will fail. If you could use bash, then this simpler script should work for you:

#!/bin/bash

thumbFile=(x $1)  # This simple line will break $1 into an array
                  # with the index tumbFile[1] equal to file1.

unset thumbFile[0]   # Cosmetic: Remove the array element that contains x.

printf "%s " "${thumbFile[@]}"; echo; echo  # print all values in thumbFile

ThumbsIn=$2
counterThumbs=1
for thumbnumber in $ThumbsIn; do
        echo "${thumbFile[${counterThumbs}]}"
        echo -e "\n"
        counterThumbs=$((counterThumbs+1))
done

call it as this:

./script "file1 file2 file3 file4" "thumb1 thumb2 thumb3 thumb4"

The double quotes will keep the input as one parameter until an un-quoted $1 is used inside the script.