LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-28-2014, 10:38 AM   #1
JoeTampa
LQ Newbie
 
Registered: Oct 2003
Posts: 5

Rep: Reputation: 0
BASH while-do script does not execute another script


I have a file that consists of a list of hosts and ports, space delimited:

10.2.4.11 443
10.55.22.77 4433
10.55.22.11 4444
10.55.22.53 443


I need to read these and run another bash script on each host and port. I have a script that reads the values and calls the script like so:

while read -r host port
do
./myscript $host $port
done < file



However, myscript never fires. I've tried calling it with bash ./myscript $host $port and with bash /path/to/myscript $host $port - but no joy.

Anyone have any ideas, or have a better way to do this?
 
Old 02-28-2014, 10:55 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
have you tried sticking an "echo" in front of your call to myscript to see if the while loop is even running and that host/port have the right values?
 
Old 02-28-2014, 11:06 AM   #3
JoeTampa
LQ Newbie
 
Registered: Oct 2003
Posts: 5

Original Poster
Rep: Reputation: 0
Yep, in the form of

echo "Host: $host --- Port: $port"

..And I see proper output.


However, a similar line in the called script never produces output at all, so it appears to not be firing. Running the called script directly works fine.
 
Old 02-28-2014, 11:10 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Try placing, set -xv, at the top of this script and see what is actually being executed.
 
1 members found this post helpful.
Old 02-28-2014, 11:12 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Well, if the echo works your while do loop is fine..

To help I think we need to see your "myscript" script

Please use bbcode tags

Code:
Like this
[code]
Like this
[/code]
 
Old 02-28-2014, 11:17 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
maybe the user doesnt have permissions to execute myscript ?
 
Old 02-28-2014, 11:54 AM   #7
_Avyd
LQ Newbie
 
Registered: Feb 2014
Distribution: Debian, RedHat
Posts: 9

Rep: Reputation: Disabled
What about something like this:

Quote:
# Run command on multiple hosts (read host from hostlist)
for i in $(cat hostlist);
do
echo $i; ssh -o StrictHostKeyChecking=no -l root $i -C "echo command";
done
 
1 members found this post helpful.
Old 02-28-2014, 11:57 AM   #8
byau
Member
 
Registered: Sep 2009
Location: Los Angeles, CA
Posts: 33

Rep: Reputation: 5
Change myscript to something simple so you can take that out of the equation like

#!/bin/bash
echo "myscript $1 $2"

Is there any screen output? The first thing I would think is myscript is not executable but then it would tell you so when you try to call it
 
Old 02-28-2014, 12:39 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by _Avyd View Post
What about something like this:
Please use code tags not quote

It would fail!

100% nothing wrong with the original while read loop

Yours will fail
Code:
# Run command on multiple hosts (read host from hostlist)
for i in $(cat hostlist);
do
    echo $i; ssh -o StrictHostKeyChecking=no -l root $i -C "echo command";
done
Since the list is host<space>port
Your for loop is

Code:
for i in host1 port1 host2 port2 host3 port3 ......
Like I said, nothing wrong with the while read in OP.

Problem, as has already been put out there, is likely

Script not executable ( or at least not for the user )

Incorrect/missing shebang ( #!/bin/bash )

Something else....

without seeing the script we don't know if the host and port are in the correct order
Just a guess.. no script to read ...
 
1 members found this post helpful.
Old 02-28-2014, 01:01 PM   #10
_Avyd
LQ Newbie
 
Registered: Feb 2014
Distribution: Debian, RedHat
Posts: 9

Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
Please use code tags not quote

It would fail!

100% nothing wrong with the original while read loop

Yours will fail
Code:
# Run command on multiple hosts (read host from hostlist)
for i in $(cat hostlist);
do
    echo $i; ssh -o StrictHostKeyChecking=no -l root $i -C "echo command";
done
Since the list is host<space>port
Your for loop is

Code:
for i in host1 port1 host2 port2 host3 port3 ......
Like I said, nothing wrong with the while read in OP.

Problem, as has already been put out there, is likely

Script not executable ( or at least not for the user )

Incorrect/missing shebang ( #!/bin/bash )

Something else....

without seeing the script we don't know if the host and port are in the correct order
Just a guess.. no script to read ...

I wrote "something like this" and didn't mean to copy paste it. Just an idea of implementation.

The code I copied is from my notes and is a working way of running commands on multiple server.
 
Old 02-28-2014, 01:26 PM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by _Avyd View Post
I wrote "something like this" and didn't mean to copy paste it. Just an idea of implementation.

The code I copied is from my notes and is a working way of running commands on multiple server.
Well, it is a poor way of doing it
Ok, it is "ok" if you have one host per line, but it is still sloppy code

The correct way is the 'original' while read loop


Edit

http://mywiki.wooledge.org/BashGuide

Read that, and drop the bad habits you have picked up

Last edited by Firerat; 02-28-2014 at 01:28 PM.
 
Old 02-28-2014, 01:44 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by JoeTampa View Post
Yep, in the form of

echo "Host: $host --- Port: $port"

..And I see proper output.


However, a similar line in the called script never produces output at all, so it appears to not be firing. Running the called script directly works fine.
What do you mean by "not be firing"? You say the script called directly works fine; so it sounds as if it is executable, but then you make this "not be firing" statement which causes confusion.

How about you go back to the start. You have a script, post it within [code][/code] tags. Show the output from calling the script directly. Explain next, more clearly; what the problem is. Using a term like "not be firing" means nothing helpful. State instead what desired behavior or output you are not attaining.
 
Old 02-28-2014, 02:01 PM   #13
_Avyd
LQ Newbie
 
Registered: Feb 2014
Distribution: Debian, RedHat
Posts: 9

Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
Well, it is a poor way of doing it
Ok, it is "ok" if you have one host per line, but it is still sloppy code

The correct way is the 'original' while read loop


Edit

http://mywiki.wooledge.org/BashGuide

Read that, and drop the bad habits you have picked up

I don't think there is a "correct" or "original" way of doing this as it's not a system like Windows.

The guide is great, thanks.
 
Old 03-03-2014, 10:25 PM   #14
JoeTampa
LQ Newbie
 
Registered: Oct 2003
Posts: 5

Original Poster
Rep: Reputation: 0
Figured it out

...The set -xv did the trick.

The second item on each line had a CR ("\r") appended, and that totally threw off the second script, which exited.

I got around it by changing the read statement to have three arguments, and the CR got tacked on to the third bogus one.


Thanks for the help, everyone!


Joe
 
Old 03-04-2014, 05:12 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
'\r' shouldn't be there; did you write that file on a Mac or possibly MS?
Use unix2dos to convert the file to *nix line endings only '\n' .
 
1 members found this post helpful.
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute bash script in C waqasdaar Programming 15 10-27-2015 02:54 PM
[SOLVED] execute bash script from pySide script sharky Programming 3 12-23-2013 01:49 PM
Bash inactivity execute script Gachl Linux - General 5 08-15-2013 04:33 PM
Can't execute Bash script. zbe Linux - Software 4 10-17-2008 08:05 AM
fastest way to execute another bash script ?? michael_util Programming 3 08-23-2004 10:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:21 AM.

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
Open Source Consulting | Domain Registration