Loop through an array of strings in Bash?
#1
I'm currently learning Bash scripting and have come across a scenario where I need to run certain database operations on a set of database names. I thought of storing these names in an array and then looping through it to apply the operation. However, my knowledge of arrays and loops in Bash is quite limited. Here's the basic pseudo-code I have in mind, but I'm not sure how to properly declare the array and use it in the loop. Can someone provide me with a correctly formatted Bash script that would work for looping through, say, 15 database names?

Code:
pseudo
for databaseName in listOfNames
then
# Do something with databaseName
end

I'd appreciate it if the shared code could include some basic operations like echoing the database name just to confirm the loop is working, and comments to explain what each part of the script is doing.
Reply
#2
Sure, let me help you out. You need to first declare an array with the database names. In Bash, an array can be created like this:

Code:
databaseNames = ('db1'
    'db2'
    'db3'
    'db4'
    'db5'
    'db6'
    'db7'
    'db8'
    'db9'
    'db10'
    'db11'
    'db12'
    'db13'
    'db14'
    'db15')

Then, you use a `for` loop to iterate over the array elements. Here's an example script:

Code:
databaseNames = ('db1'
    'db2'
    'db3'
    'db4'
    'db5'
    'db6'
    'db7'
    'db8'
    'db9'
    'db10'
    'db11'
    'db12'
    'db13'
    'db14'
    'db15')
# Loop through each name in the array
for databaseName in "${databaseNames[@]}"
do
    echo "Working on database: $databaseName"
# Your database operations here
done

This will loop through the array and echo each database name.
Reply
#3
I also needed to add a few other operations in the loop, such as checking if a database exists before performing operations on it. For instance, it could be useful to include an `if` statement that checks for the database's existence. Here's how you might do that:

Code:
do
    echo "Checking if $databaseName exists..."
if [
    [-d "/path/to/databases/$databaseName"]
];
then
echo "$databaseName exists. Performing operations..."
# Perform operations here
else
    echo "$databaseName does not exist. Skipping..."
fi
done

Just remember to replace "/path/to/databases/" with the actual path where your databases are stored.
Reply
#4
It's always a good idea to add error checking in your scripts too. For database operations which can potentially change or harm data, consider adding prompts for user confirmation before executing those operations.
Additionally, what if you need to do something more complex with the database names? Let's say, for example, you need to append a timestamp or log the completion of each operation to a file. Here's how you can append a timestamp to each database name:

Code:
timestamp = $(date + % Y % m % d % H % M % S)
for databaseName in "${databaseNames[@]}"
do
    dbNameWithTimestamp = "${databaseName}_${timestamp}"
echo "Renaming $databaseName to $dbNameWithTimestamp..."
# Code to rename or otherwise work with the database
done

And here's an example of how you might log the completion time for each operation to a file:

Code:
do
    echo "Starting operation on $databaseName at $(date)" | tee - a "$logFile"
# Perform your operations here
echo "$databaseName operation completed at $(date)" | tee - a "$logFile"
done

Notice the use of `tee -a` which allows you to append the output to a file while also displaying it on the standard output.
Reply
#5
Thanks, everyone, for the assistance. It seems that I now have a much better understanding of the basics as well as some additional safety and logging practices. Based on all the contributions, I've compiled a final script that seems to do what I need:

Code:
databaseNames = ('db1'
    'db2'
    'db3'
    'db4'
    'db5'
    'db6'
    'db7'
    'db8'
    'db9'
    'db10'
    'db11'
    'db12'
    'db13'
    'db14'
    'db15')
timestamp = $(date + % Y % m % d % H % M % S)
logFile = "database_operations.log"
for databaseName in "${databaseNames[@]}"
do
    echo "Starting operation on $databaseName at $(date)" | tee - a "$logFile"
if [
    [-d "/path/to/databases/$databaseName"]
];
then
echo "$databaseName exists. Performing operations..." | tee - a "$logFile"
dbNameWithTimestamp = "${databaseName}_${timestamp}"
# Perform operations here
else
    echo "$databaseName does not exist. Skipping..." | tee - a "$logFile"
fi
echo "$databaseName operation completed at $(date)" | tee - a "$logFile"
done

Ready to be run after copying, I've ensured that every piece of the code includes necessary explanations and safety checks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)