Alan G. Alan G. - 5 months ago 33
Linux Question

Create script to run multiple commands with variable in linux

So basically, I'm trying to streamline a process where I run an executable script to update remote devices that are connected to my server. Initially, I had a few devices that I would update, but it's gotten to the point where it's over 40 and it takes forever to update them all manually. I'd like to create a script to update them all at the same time using the executable, but the only thing I can't figure out is how to include a variable in the script that I can input on the command line, as the

version_number
has to be manually inputted.

Right now, it looks something like this :

./updater-app device_number version_number


What I've been doing is just copying and pasting a long command to somewhat streamline the process, however, with over 40 devices to update, it's somewhat tedious, and will only get worse with remote devices to update in the future

./updater-app device1 version20 & ./updater-app device2 version20 & ./updater-app device3 version20 &
.... and so on.

Since the device numbers all stay the same, basically I just want to create a shell script that allows me update all of them at the same time, but input a
version_number
variable on the command line, as that changes every time a new version of the device software comes out.

Answer Source

I guess you can create a loop and run the whole update script one by one. It will run parallel, as it's running in background

listOfDevices=("device1" "device2" "device3" ....)
version=$1

for device in "${listOfDevices[@]}"
do
   ./updater-app $device $version &
done