I want to write a script that loops through 15 strings (array possibly?) Is that possible?
Something like:
for databaseName in listOfNames
then
# Do something
end
I want to write a script that loops through 15 strings (array possibly?) Is that possible?
Something like:
for databaseName in listOfNames
then
# Do something
end
Single line looping,
you will get an output like this,
That is possible, of course.
See Bash Loops for, while and until for details.
You can use it like this:
Also works for multi-line array declaration
Implicit array for script or functions:
In addition to anubhava's correct answer: If basic syntax for loop is:
there is a special case in bash:
When running a script or a function, arguments passed at command lines will be assigned to
$@
array variable, you can access by$1
,$2
,$3
, and so on.This can be populated (for test) by
A loop over this array could be written simply:
Note that the reserved work
in
is not present and no array name too!Sample:
Note that this is same than
Then into a script:
Save this in a script
myscript.sh
,chmod +x myscript.sh
, thenSame in a function:
Then
If you are using Korn shell, there is "set -A databaseName ", else there is "declare -a databaseName"
To write a script working on all shells,
It should be work on all shells.
This is also easy to read: