[Solved] For Loop with 2 commands? (hostname prob)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] For Loop with 2 commands? (hostname prob)
# 1  
Old 06-19-2012
[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! Smilie
# 2  
Old 06-19-2012
host is also a command so it is not wise at all to use it as a variable name...
Quote:
for host in hostnames_here
What is hostnames_here ? A file?
This User Gave Thanks to vbe For This Post:
# 3  
Old 06-19-2012
And what doesnt work?
This User Gave Thanks to vbe For This Post:
# 4  
Old 06-19-2012
Quote:
Originally Posted by vbe
host is also a command so it is not wise at all to use it as a variable name...
What is hostnames_here ? A file?

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 ----------

Quote:
Originally Posted by vbe
And what doesnt work?
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.
# 5  
Old 06-19-2012
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 ..
This User Gave Thanks to jayan_jay For This Post:
# 6  
Old 06-19-2012
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.
This User Gave Thanks to vbe For This Post:
# 7  
Old 06-19-2012
Quote:
Originally Posted by jayan_jay
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 ..
that did it for me! thanks very much everyone! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] For loop help

Hello, This is really breaking my head. I request you help to solve this problem. I have a list of files at the source directory (/tmp) as below, NewTransfer_20131202_APAC.dat NewTransferFile_20131202_APAC.dat NewTransfer_20131203_APAC.dat NewTransferFile_20131203_APAC.dat... (3 Replies)
Discussion started by: sravicha
3 Replies

2. Shell Programming and Scripting

[Solved] Usage of shell commands inside a C program

Hi I have a program int main(int srgc, char *argv) { for(int i=1; i<50; i++) { system("dd if=/dev/zero of=file$i bs=1024 count=$i"); } return 0; } My doubt is how to use the "$i" value inside C code Please help (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

[Solved] Do not want to print sftp commands in log

Hi, While running my ksh file, I require the logs to be written to another file. For this I use the below code: write_log() { echo `date +"%d %h, %Y %H:%M:%S"` " : " $* >> ${LOG_FILE} } But inside my ksh file, am connecting to sftp server and executing some commands. So while i see... (8 Replies)
Discussion started by: confused_info
8 Replies

4. Shell Programming and Scripting

[Solved] Run multiple commands in invoked program

Hi, I have coded a program in Haskell using the compiler Hugs and the program requires multiple commands (with parameters) to be entered into it, it then outputs the result of its execution. I need to test a lot of different options (i.e. the parameters) so it would be obvious to automate the... (0 Replies)
Discussion started by: tz742
0 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Wildcards used in find, ls and grep commands

Platforms : Solaris 10 and RHEL 5.6 I always get double quotes , single quotes and asteriks mixed up for find, ls and grep commands. The below commands retrieve the correct results. But , unders stress , I get all these mixed up :mad: .So, i wanted to get a clear picture. Please check if... (7 Replies)
Discussion started by: John K
7 Replies

6. UNIX for Dummies Questions & Answers

[Solved] About commands for linux executable files

I have seen commands like this: (hello is the executable file or maybe script file) ./hello hello .hello So, anyone could tell me the differences among these commands? (2 Replies)
Discussion started by: icyfight
2 Replies

7. Solaris

[SOLVED] Issue with hostname command

Hi, i'm having issues with one of my db servers, each time i run the hostname command i get "-a" as result this is the only zone on this server where i have this issue. could any one help me with this issue (5 Replies)
Discussion started by: EduardoDiaz
5 Replies

8. Shell Programming and Scripting

[Solved] Value of a variable is not recognised for commands comes from external file

Hi, my script is setting a variable with value and this variable is present in my another command that is coming from external file and this command is internally called after this variable is set. but while execution of this command, the value is not retrieved properly. say, my script... (5 Replies)
Discussion started by: rbalaj16
5 Replies

9. Shell Programming and Scripting

[Solved] Problem with mail commands & format

1. I generated a log for the script. log: abc def ghi ijk If I use general (plain text) format for mail sending, it was displaying correctly with new lines where I have. Eg: abc def ghi ijk But I tried with HTML format using the code: (8 Replies)
Discussion started by: karumudi7
8 Replies

10. UNIX for Dummies Questions & Answers

prob's with while loop

Hi Gurus, I have a requirement where I have files in a folder For example in home/input i have files 20080111.06.12.45_abc.xml 20080112.06.10.48_abc1.xml 20080113.06.08.55_abc2.xml 20080114.06.12.35_abc3.xml Actually i will get the files as abc.xml , i rename & put it with timestamp.... (2 Replies)
Discussion started by: pssandeep
2 Replies
Login or Register to Ask a Question