LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-15-2013, 07:27 PM   #1
TRUNoise
Member
 
Registered: Apr 2013
Location: /home/
Distribution: Ubuntu 14.04
Posts: 50

Rep: Reputation: 7
Smile Bash script help with tables :S


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
Hope I was descriptive enough.
 
Old 07-15-2013, 07:38 PM   #2
camh
Member
 
Registered: Feb 2005
Distribution: Slack/Debian
Posts: 163
Blog Entries: 2

Rep: Reputation: 33
http://www.cyberciti.biz/faq/bash-for-loop-array/
 
1 members found this post helpful.
Old 07-15-2013, 07:42 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
untested

coproc requires bash version 4 or greater
Code:
#!/bin/bash
table="server1 server2 server3"
executable=start.sh

for Server in $table;do
    coproc $Server { sh $Server/$executable }
done
 
Old 07-15-2013, 07:48 PM   #4
TRUNoise
Member
 
Registered: Apr 2013
Location: /home/
Distribution: Ubuntu 14.04
Posts: 50

Original Poster
Rep: Reputation: 7
Quote:
Originally Posted by camh View Post
Google failed me in finding a useful page such as this. Thank you.
 
Old 07-15-2013, 07:48 PM   #5
camh
Member
 
Registered: Feb 2005
Distribution: Slack/Debian
Posts: 163
Blog Entries: 2

Rep: Reputation: 33
Quote:
Originally Posted by Firerat View Post
untested

coproc requires bash version 4 or greater
Code:
#!/bin/bash
table="server1 server2 server3"
executable=start.sh

for Server in $table;do
    coproc $Server { sh $Server/$executable }
done
This won't work since you have $table defined as a string. It needs to be an array.
 
Old 07-15-2013, 08:03 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by camh View Post
This won't work since you have $table defined as a string. It needs to be an array.
Code:
#!/bin/bash
table="server1 server2 server3"

for Server in $table;do
   echo "yes it does work, look $Server"
done
 
Old 07-15-2013, 09:19 PM   #7
camh
Member
 
Registered: Feb 2005
Distribution: Slack/Debian
Posts: 163
Blog Entries: 2

Rep: Reputation: 33
Quote:
Originally Posted by Firerat View Post
Code:
#!/bin/bash
table="server1 server2 server3"

for Server in $table;do
   echo "yes it does work, look $Server"
done
Sorry, my bad. I jumped the gun (it's been a long day)
 
Old 07-15-2013, 09:27 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by camh View Post
Sorry, my bad. I jumped the gun (it's been a long day)
No worries,

an array would be safer, as it would better handle white space,
but I guessed these had none
 
Old 07-19-2013, 04:39 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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

How can I use array variables?
http://mywiki.wooledge.org/BashFAQ/005/
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] MySQL: Comparing values in diff tables SQL Query and BASH watice Programming 4 12-24-2012 01:56 PM
Script to dump SQL tables Alexrkkl Linux - Newbie 5 12-28-2011 12:10 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
bash script to list mount point in html tables pobrika Programming 3 10-21-2009 06:16 PM
Bash scripting, mysql, and creating tables. hondaman Linux - General 2 09-25-2005 06:17 AM


All times are GMT -5. The time now is 08:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration