Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Simple enough request, I suppose. I need to make a shell script (Can be #!/bin/bash or #!/bin/sh as long as the operations get done) to run a .sh script file in several directories using a table. An explanation of what I need to accomplish is to have a table containing for instance 3 server directories; server1, server2 and server3. Now to start those servers, a .sh script is inside those directories titled 'start.sh' which automates starting the server. So, a piece of coding at the bottom of where the table contents is defined needs to do for instance select one of those directory names and start a sh script then continue on with the rest of the servers. How would I go about this?
If you can't understand what I said previously, here is a non-working descriptive code sniplet to better word it.
Code:
table=server1,server2,server3 <- All of the server names
executable=start.sh
for every name in the table do this command
sh $selected table entry/$executable
repeat above command for all table entries
done
While the above solution does work, it depends on shell word-splitting to do so. As long as the input strings don't contain spaces or globbing characters it's ok, but it's not the recommended way.
Scalar variables are really designed for holding (and manipulating) only single strings. If you have lists of things, arrays are the much preferred solution, as mentioned above.
Fortunately only a couple of small modifications are needed to switch over.
Code:
#!/bin/bash
table=( server1 server2 server3 )
for Server in "${table[@]}"; do
echo "yes it does work, look $Server"
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.