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
2
  • 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) Commented Oct 26, 2015 at 6:52
  • For adding variables to an array, have a look at this answer Commented Oct 26, 2015 at 8:25

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

or better #!/usr/bin/env bash
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.)
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.

Comments

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.