Unix - eval command example

Unix - eval command example

 
Have you ever have the need to remotely execute a long list of command to a list of unix machines to perform tasks like gathering their information? Well, if you did then you will mostly came across an issue where you ran out of quoting variables to quote the long list of command that you need to execute.

For example using "" (double quote) to execute a command uptime remotely.
root@myserver # ssh root@server1 "uptime"


If you need to execute uptime along with for example df and search for string "nfs" using awk you can use the below command.
root@myserver # ssh root@server1 "uptime;df |awk '/nfs/'"


So far the above two example are simple and doable without much issue as we have used double quote (") and single quote (') to wrap our command. What if you need to execute more command and ran of out quotes? Well one easy solution is to ssh multiple times to same machine to execute different sets of command. To some that might look troublesome and inefficient. Another more efficient way is to use eval command.

Eval Command. Eval is a built in linux or unix command. The eval command is used to execute the arguments as a shell command on unix or linux system.

How to use it? First save the commands that you need to execute in a file. You can use vi editor. Below is the content of the file which contains the needed commands to be executed on a unix machine.
root@myserver # cat commands.txt
hostname;uptime;sar -u 3 3;df |awk '/nfs/';echo "hostname"


Next create a simple script to do what we need where in this case is to ssh remotely to a unix machine to executed the needed command contains in file commands.txt. Below is what the content of the script would look like.
root@myserver # cat script.sh
commands=`cat commands.txt` ssh root@server1 eval $commands


Remember to set the script to executable with chmod and it is ready to be used.
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
bash for do done AIX
Viewed 1680 times since Mon, Jun 4, 2018
Convert JSON to CSV with bash script
Viewed 12180 times since Mon, Jan 20, 2020
java Simple Java JDBC Program To Execute SQL Select Query
Viewed 3298 times since Sun, Jan 9, 2022
Unix - Examples for grep command
Viewed 2854 times since Fri, Jun 8, 2018
Epoch & Unix Timestamp Conversion Tools
Viewed 48802 times since Fri, Jun 22, 2018
Display basic information about Physical Volumes
Viewed 3212 times since Sun, Jun 3, 2018
Convert CSV to JSON with jq
Viewed 26871 times since Mon, Jan 20, 2020
To do a quick check on the number of path present (does not mean all are Enabled] using for loop
Viewed 3677 times since Fri, Jun 8, 2018
java Simple Java JDBC Program To Execute SQL Update Statement
Viewed 12365 times since Sun, Jan 9, 2022
O’Reilly’s CD bookshelf
Viewed 11926 times since Wed, Jun 27, 2018