|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[Solved] For Loop with 2 commands? (hostname prob)
Hi I would like to ssh into serveral servers, print the hostname and do an ls (of files in a certain directory), and add these details to a file. so i have this: Code:
#!/usr/bin/bash for host in hostnames_here do ssh $host echo "$HOSTNAME" ls /directory_location > ~/new_file_name.txt done it doesn't work for me, it lists the files but doesn't echo/print the hostname.......which i need to distinguish between servers. any help gladly received!
|
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
host is also a command so it is not wise at all to use it as a variable name...
Quote:
|
| The Following User Says Thank You to vbe For This Useful Post: | ||
horhif (06-19-2012) | ||
| Sponsored Links | ||
|
|
#4
|
|||
|
|||
|
Quote:
no, sorry i left out the names of the servers trying to help. hostnames_here would be the server names i.e. server1 server2 server3 etc.. ---------- Post updated at 06:49 AM ---------- Previous update was at 06:43 AM ---------- so what i'd like to see would be (in the output file) servername1 file1_in_server1 file2_in_server1 (SPACE) servername2 file1_in_server2 file2_in_server2 (SPACE) etc etc what i get now is just filename1 filename2 filename3 filename4 Just one long list of file names. I am not getting the name of the server in the list, so am not sure what files are from what server. There is no break in the list....i.e. not showing server1 / server 2 etc. |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
If your
HOSTNAME variable value is set in all servers, then you will retrieve it via
ssh Code:
$ env | grep -w HOSTNAME HOSTNAME=xxxxxxxxx Instead of it, go with the hostname command Code:
ssh $host " hostname ; ls -ltr /dir_location " Hope this helps .. |
| The Following User Says Thank You to jayan_jay For This Useful Post: | ||
horhif (06-19-2012) | ||
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
As Jayan_jay suggests HOSTNAME is not initialized or it is the one from source machine so you have to use a command:
hostname then to get 2 command executed you need to use the double quotes to "make your command "look" as one.
|
| The Following User Says Thank You to vbe For This Useful Post: | ||
horhif (06-19-2012) | ||
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Quote:
![]() |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Solved] Use of until loop for user confirmation | anuragpgtgerman | Shell Programming and Scripting | 4 | 04-27-2012 11:38 AM |
| [Solved] Value of a variable is not recognised for commands comes from external file | rbalaj16 | Shell Programming and Scripting | 5 | 11-01-2011 11:33 AM |
| [Solved] Problem with mail commands & format | karumudi7 | Shell Programming and Scripting | 8 | 10-18-2011 01:57 PM |
| [SOLVED] for loop to process files | graysky | Shell Programming and Scripting | 1 | 09-06-2011 04:03 PM |
| prob's with while loop | pssandeep | UNIX for Dummies Questions & Answers | 2 | 01-11-2008 05:22 PM |
|
|