Search code examples
bashfor-loopsqlplus

Executing multiple sql files in a bash for loop


I'm trying to run the following lines of code in bash to run multiple files on a database.

#!/bin/bash
for file in ${arrIN}; do
    echo "Executing ${file}..";
    sqlplus ${db_user}/${db_password}@${db_host}:1521/${db_sid} @${file};
done

For some reason, it will only execute the first file on the database, but won't keep executing them. When I check how many files are in arrIn it prints two, so I know there are multiple files. When I run this:

file1=${arrIN[0]}
file2=${arrIN[1]}    
sqlplus ${db_user}/${db_password}@${db_host}:1521/${db_sid} @${file1}
sqlplus ${db_user}/${db_password}@${db_host}:1521/${db_sid} @${file2}

It executes both files as expected. I would like to acomplish this in a for loop


Solution

  • #!/bin/bash
    
    arr=("0000" "1111")
    
    for i in "${arr[@]}"
    do
        echo $i
    done
    
    • you need [@] to loop on all the items in the array.
    • without [@], it just loops on the first item, like you experienced.