Tag Archives: scripting

3 tricks to get multiple commands output in the same row

Three tricks to get two commands output in a single row. Normally two commands outputs will be always separated by a carriage return in Linux terminal. 

One of the major hurdles in scripting is to get your outputs formatted well according to requirements. In Linux/Unix each command stdout its output always in a new line. This is a hurdle in many situations where the coder wants two outputs, two terms, two variables in a single row for example CSV format. In this post, we will see how to present two or more commands output in a single row.

Read also : Scripting related posts

Normally when we execute more than one command, they will display outputs in different rows:

# date ; hostname
Sat Dec 24 01:35:50 EDT 2016
testsrv2

# echo text1; echo text2; echo text3
text1
text2
text3

In the above example, commands have outputs per row. We are going to accomplish all outputs in one row using the below tricks.

Trick 1:

Using the translate function. AS I said earlier each command output is accompanied by a carriage return. We will be translating carriage return with tab character hence getting two or more commands output in a single row.

# (date; hostname) |tr '\n' '\t'
Sat Dec 24 01:36:22 EDT 2016    testsrv2

# (echo text1; echo text2; echo text3) |tr '\n' '\t'
text1   text2   text3

Bypassing outputs to translate function we achieved single row output. Here we instructed tr function to translate all carriage returns (\n) to tab (\t).

If you want to output in CSV format (comma-separated value) then replace tab with a comma and you will get it.

# (date; hostname) |tr '\n' ','
Sat Dec 24 01:43:09 EDT 2016,testsrv2,

# (echo text1; echo text2; echo text3) |tr '\n' ','
text1,text2,text3,

You can observe one trailing comma which shows every command output ends with \n and hence it gets replaced by ,

Trick 2:

By defining each command output as a variable. Here we will store the output of each command in one variable and then we will echo those variables in a single line.

# mydate=`date`

# myname=`hostname`

# echo $myname $mydate
testsrv2 Sat Dec 24 01:46:04 EDT 2016

Here we stored date and hostname command’s output in mydate and myname variables. In the last command, we echoed both variables with space in between. Note that commands output can be stored in a variable by executing the command using backticks (in-line execution)!

Trick 3:

By echoing in-line execution outputs. In-line execution i.e. using backticks to execute commands within another command. We will be using this trick to echo outputs in a single row.

# echo "`date` `hostname`"
Sat Dec 24 01:50:36 EDT 2016 testsrv2

In the above example, first, we have executed commands and got outputs. But those outputs are fed into echo command. Hence echo command stdout them in a single row.

# echo "`echo text1` `echo text2` `echo text3`"
text1 text2 text3

Make a note that we executed each command in separate back tick quotes.

Run command on multiple linux servers from windows

Learn how to run the same/repetitive command on multiple Linux servers from the Windows machine. This trick uses command line putty ‘plink’ utility.

One of the major concerns for sysadmin is to run the same/repetitive command on multiple Linux servers in infra when there is no centralized tool available. In this post, we are going to see how to run a command on multiple Linux servers in one go. There is no need of saving your account password anywhere and even no need to have expect function in your source machine!

Pre-requisite

  • Windows machine with plink downloaded on it (download plink here)
  • Linux servers should be reachable from a windows machine

How to do it

plink is a putty command-line utility. Using plink we will be able to connect to the server by supplying IP, username, password on the command line. plink can be invoked from a command prompt on windows.

C:\Users\noname\Desktop>plink -ssh user1@10.10.1.11 -pw password@123 (hostname; date)
testserver
Tue Nov  1 12:54:33 IST 2016

C:\Users\noname\Desktop>

Goto Windows command prompt by typing cmd in a run window (windows key + r). Navigate to folder where plink executable is kept and then type in plink command as above.

  • ssh: protocol to connect
  • id@server IP
  • pw: account password
  • Commands to execute on Linux server in braces.

This is how plink works. Now to execute the same command on multiple servers we see below example. We will connect to 3 servers and execute hostname & date command.

First, put all 3 server’s IP addresses in single file ip_list.txt. Then execute simple for loop on that file as below in command prompt. This is a very basic batch script.

C:\Users\noname\Desktop>FOR /F "tokens=1,2* delims=," %G IN (C:\Users\noname\Desktop\ip_list.txt) DO plink -ssh user1@%G -pw password@123 (hostname; date)

testserver
Tue Nov  1 12:54:33 IST 2016

testserver1
Tue Nov  1 12:54:36 IST 2016

testserver2
Tue Nov  1 12:54:39 IST 2016

C:\Users\noname\Desktop>

Voila! all server’s command output is there! Commands executed on all servers in one go.

This is very useful when you need to check some single line outputs from all servers or if you want to run account refresh commands on all servers in one go.