ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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?
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!
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.
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
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.