LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2017, 03:27 PM   #1
hopeless_n00b
LQ Newbie
 
Registered: Aug 2014
Posts: 21

Rep: Reputation: Disabled
bash scripts


I must be looking in the wrong places, because I have not found documentation for bash scripts which explain in clear-cut language exactly when it is that a bash script executes all the commands, and when it is that a bash script stops before executing all the commands.

My best guess from experimenting is that a bash script stops early at the first command whose exit code is different from zero.

Can anyone confirm, or are there exceptions? How does this work, and can anyone point me to a short introduction which does not skip over such fundamentally crucial issues?
 
Old 05-08-2017, 03:59 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That depends on 'errexit' option (see command 'set -e')
 
Old 05-08-2017, 04:22 PM   #3
hopeless_n00b
LQ Newbie
 
Registered: Aug 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
At this point I am so confused I can't form a reasonable conjecture or ask a reasonable question...

Let me give a semi-concrete example; sorry I can't make a fully concrete example -- unless it is not sanitized -- but perhaps you understand enough that my shortcomings don't get in the way...

Consider
Code:
while read host id
do
echo "host = $host"
mac="$(ssh $host "ifconfig | sed -n 's/://g;s/.*HWaddr \([^ ]*\).*/\1/p'")"
echo "$mac"
done < /tmp/hostIDs
The result is that the "do loop" seems only to execute one time -- I get a host and mac printed out -- even though the file /tmp/hostIDs has many lines. This is very confusing because if I simply comment out the line containing the ssh, like so
Code:
while read host id
do
echo "host = $host"
#mac="$(ssh $host "ifconfig | sed -n 's/://g;s/.*HWaddr \([^ ]*\).*/\1/p'")"
echo "$mac"
done < /tmp/hostIDs
then the loop executes many times and the host is printed out for every line in /tmp/hostIDs.

At first I thought it was because the ssh caused early termination, but if that is true, how can I force the loop to complete?
I have no clue!
 
Old 05-08-2017, 04:44 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The first ssh might have read all your input. Try sg like this:
Code:
...
mac="$(ssh $host "..." </dev/null)"
...
 
1 members found this post helpful.
Old 05-08-2017, 04:59 PM   #5
hopeless_n00b
LQ Newbie
 
Registered: Aug 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
Partial progress (although I have no clue as to what is going on)...
Consider
Code:
while read host id                                                                       
do                                                                                       
command="ssh $host \"ifconfig | sed -n 's/://g;s/.*HWaddr \([^ ]*\).*/\1/p'\""           
echo "$command"                                                                          
done < /tmp/hostIDs
This loop completes with no early termination. Moreover it prints out a bunch of lines like
Code:
ssh machine0 "ifconfig | sed -n 's/://g;s/.*HWaddr \([^ ]*\).*/\1/p'"
ssh machine1 "ifconfig | sed -n 's/://g;s/.*HWaddr \([^ ]*\).*/\1/p'"
.
.
.
Moreover if I cut and paste one of those lines into an xterm, it executes and prints out the mac address of the machine.

How do I preserve the fact that the loop does not terminate early, and also assign the (printed out) result of the ssh command -- i.e., the mac address -- to a variable in my script?

Neither
Code:
mac=`$command`
nor
Code:
mac=$(eval $command)
work, and both cause the loop to terminate early.

I tried
Code:
mac="$($command </dev/null)"
and that seems pointed in the right direction because the loop does not terminate early, but I still can't make it work.
 
Old 05-08-2017, 05:01 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Are there any error messages displayed when you run the script?

What is the format of your hostIDs file. Your read statement expects two variables with the default separator being a space i.e.

host1 id1
host2 id2

The id variable is unused. What is the id variable?

If you are using a regular username not all distributions include /sbin in a regular users path and since ifconfig is depreciated it may not even be installed if running the latest version.

Mind like a steal trap... Nothing gets in or out.

NevemTeve is correct that ssh reads from standard input therefore it is reading the rest of the values in the loop. An alternative solution is to add a -n i.e.

Code:
mac=$(ssh -n $host ...)
Although the command should still work you do not need the quotes.
Code:
mac=$(ssh $host "ifconfig | sed -n 's/://g;s/.*HWaddr \([^ ]*\).*/\1/p'" </dev/null)

Last edited by michaelk; 05-08-2017 at 05:58 PM. Reason: update
 
Old 05-08-2017, 06:07 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
#!/bin/bash

count=0
bcount=0

while read host id
do
((++count))

   while [[ $bcount -lt $count ]] ;
   do

     HoldMacArray[$bcount]=input
     ((++bcount))

   done
done

# to look inside of array.length

for (( i=0 ; i < ${#HoldMacArray[@]} ; i++ ))
{
  echo "${HoldMacArray[i]}"
 }
or just one loop
Code:
count=0
while read file
do

arrayMac=[$count]=input
((++count))

done

Last edited by BW-userx; 05-08-2017 at 06:30 PM.
 
Old 05-08-2017, 11:33 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: here a complete example
Code:
#!/bin/sh

remote_cmd="ifconfig | sed -n 's/://g;s/.*HWaddr \([^ ]*\).*/\1/p'"

while read host id
do
    echo "host = $host"
    mac="$(ssh -n $host "$remote_cmd")"
    RC="$?"
    echo "RC=$RC mac=$mac"
done </tmp/hostIDs
 
Old 05-09-2017, 12:56 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I do not understand why do we need to run sed on the remote side (and also why sed?):
Code:
ssh host ifconfig | awk '/HWaddr/ { gsub(":", ""); print $NF }'
or even better:
Code:
while read host
do
    echo "host = $host"
    ssh $host ifconfig </dev/null
done </tmp/hostIDs | awk '/host =/; /HWaddr/ { gsub(":", ""); print $NF }'
by the way what is that id good for? while read host id
 
Old 05-09-2017, 08:27 AM   #10
hopeless_n00b
LQ Newbie
 
Registered: Aug 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
The post from michaelk sorted me out (though if I were more preceptive, the first answer might have).

My problem was indeed that I needed a -n flag for ssh. I'll peruse the remaining answers to solidify my understanding.

This community is great! I appreciate all your help.
 
Old 05-09-2017, 01:49 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
-n is equal to </dev/null
see man ssh
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python Scripts as a Replacement for Bash Utility Scripts LXer Syndicated Linux News 1 01-17-2013 08:08 AM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
Where are BASH commands stored? Not scripts but what bash exe uses. theKbStockpiler Programming 11 02-23-2011 03:06 PM
[SOLVED] Finding bugs in bash scripts, Analyis tool for bash traene Programming 2 10-31-2009 11:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:09 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
Open Source Consulting | Domain Registration