BASH - Need to echo for loop output to one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - Need to echo for loop output to one line
# 1  
Old 01-21-2015
BASH - Need to echo for loop output to one line

I'm trying to echo the release version of some of our Linux servers. Typically I do these types of things by "catting" a text file with the host names, "ssh-ing" to the host and running my string.

This is what I've written
Code:
for i in `cat versions.txt` ; do echo $i ; ssh $i cat /etc/issue | grep release ; done

This works, but I get output like
Code:
ServerName
Red Hat Enterprise Linux Server release 5.6 (Tikanga)

I'm trying to get it to look like
Code:
ServerName Red Hat Enterprise Linux Server release 5.6 (Tikanga)

I've looked around the forums and messed with printf and echo -n but I can't seem to get the syntax right. Any help is appreciated.
# 2  
Old 01-21-2015
Why not run grep on the server instead of cat? Less traffic.

Also, a while loop is more appropriate.

Use printf to print a string without adding a newline at the end.

ssh -n is used to prevent ssh from 'eating' all the input in versions.txt (otherwise it would run once then stop).

If ssh fails, it prints "couldn't login", so you don't get a line like server1 server2 server3 server4 server4-information when server1-3 fail to login.

Code:
while read i
do
        printf "%s " "$i"
        ssh -n $i grep release /etc/issue || echo "couldn't login"
done < versions.txt

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-21-2015
This is great, and exactly what I needed. Thank you very much! Smilie

I have a quick question, just to increase my own knowledge.

Why is a "while" loop better for this? Is that just a personal preference?
# 4  
Old 01-21-2015
The reason the while loop is better is because the for loop requires back-ticks this invokes another instance of the shell to execute, also the cat external executable needs to be loaded to read the file.

In contrast while can use the shell internal read command to load the file.

The assumption here is your versions.txt file has 1 host name per line and not something like:

Code:
server1 server2 server3
server4 server5 server6

# 5  
Old 01-21-2015
And the `cat file` stores the whole file in memory before the loop starts.
The while loop reads the file line by line, so only one line is in memory.
# 6  
Old 01-21-2015
Most importantly, it doesn't do quite exactly what you think it does -- splitting across whitespace, not lines. If you try to handle data any more complex than that, or there happens to be anything you didn't expect in it, you'll be in for a big surprise.

It also tends to break down a lot. There's a limit to how much you can cram in one statement, surprisingly small on some systems.

Also, it's inefficient because it crams it all into one statement. Just because you can doesn't mean you should.

See useless use of cat, useless use of backticks, and particularly dangerous backticks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Echo For ... Loop output into a file

Hi, All: I have one for ... loop code to generate a list of rename statement. However, echo the loop output on screen is OK. But store the echo output into a file is not working. So come here to seek help. My basic code is: ! /bin/ksh echo > DAS_VetFed.txt dir=/mydirectory cd $dir files=`ls... (6 Replies)
Discussion started by: duke0001
6 Replies

2. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

3. Shell Programming and Scripting

Grep echo awk print all output on one line

Hello, I've been trying to find the answer to this with Google and trying to browse the forums, but I haven't been able to come up with anything. If this has already been answered, please link me to the thread as I can't find it. I've been asked to write a script that pulls a list of our CPE... (51 Replies)
Discussion started by: rwalker
51 Replies

4. Shell Programming and Scripting

Basic bash, echo in loop for

Hi, I am trying to make a script to manage log. I want to write the name of the .gz I moved and the date : for i in `ls $replog/*.gz` do echo " $i " `echo $i date +%d:%m:%Y` `echo $datee `>> $replog/mrnet.log mv $i /var/log/vieux-logs done I need to echo... (10 Replies)
Discussion started by: Dabless
10 Replies

5. Shell Programming and Scripting

cannot pass a echo output to a variable in bash

Hi, I have a problem with passing a echo output into a variable in bash file='1990.tar' NAME='echo $file | cut -d '.' -f1'; echo $NAME the result is echo $file | cut -d . -f1 however with this one,#!/bin/bash file='1990.tar' echo $file | cut -d '.' -f1 the result is what I... (2 Replies)
Discussion started by: 1988PF
2 Replies

6. Shell Programming and Scripting

Script to loop line in a file and add info or do echo

I have a record.txt it will update weekly, and it could be 2 lines or more ... it just echo each line to the script san jose,23.34% tampa,2.15% dallas,30.20% seattle,44.29% Unknown,16.72% How do i write a shell script to give me a test.pl or bash file which contain #!/home/perl... (8 Replies)
Discussion started by: sabercats
8 Replies

7. Shell Programming and Scripting

echo two command output in the same line

Hi, I try to write script and echo two command at the same line . echo "A" echo "B" How can I pipe above two command at the same line in text file . So, in the output text file , you can see below ??? A B not A B Any sugggestion ??? (4 Replies)
Discussion started by: chuikingman
4 Replies

8. Shell Programming and Scripting

Echo and a command's output on the same line

Hello, I'm writing some bash scripts and I'm trying to get an echo command and the output of another command to display on the same line. For example: I want to run echo "Operating System: " unameand have it displayed as Operating System: Darwin Thanks for your help! (7 Replies)
Discussion started by: codyhazelwood
7 Replies

9. Shell Programming and Scripting

Output of both the echo statement in one line

I have script like echo -n FINISHED FEXP: ${TABLE2EXP} echo $STATUS I want the output of both the echo statement in one line How can i do this (3 Replies)
Discussion started by: scorp_rahul23
3 Replies

10. Shell Programming and Scripting

How to place the output of two different echo statements on one line

Hello there, I wrote a shell script to modify the code for some of our clients in our client database. Before starting the data modification the program performs a few checks. When a check is being performed, it should be shown on the screen of the user running the program, the result of... (5 Replies)
Discussion started by: JoBa
5 Replies
Login or Register to Ask a Question