LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-06-2015, 08:36 PM   #1
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Rep: Reputation: Disabled
Multiple Commands from One Command Line


Hi.
I want to run two commands from terminal and write the results to a file.
I want the output file to contain a listing of the hostname and directory .xyz if it exists on that server.

I've created a server-list file which contains 100 servers.
so, I attempted to do this, but cannot get the hostname from each server to write to the file, but the ls -a command does appear in the file.

ssh Tom@192.168.1.5 ls -a | grep .xyz; hostname

Any input is appreciated!
ntbluez
 
Old 11-06-2015, 09:06 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Your errors are:
  • you run hostname on your local system
  • rather than listing the content of .xyz, you list the content of Tom's home directory
  • you don't look for a directory named .xyz. The dot has a special meaning for grep, namely "any character". So, you look for a file whose name has at least four characters and includes "xyz"

Something like this might be your solution:
Code:
ssh Tom@192.168.1.5 sh -c "ls -a .xyz 2>/dev/null || hostname"
This runs a shell on the remote system and asks that shell to
  • execute the ls
  • discard any error messages from the ls command
  • run hostname only if the ls command succeeds
If you want the hostname to be printed even if there is no .xyz directory, replace the double pipe by a semicolon.

Last edited by berndbausch; 11-06-2015 at 10:49 PM.
 
1 members found this post helpful.
Old 11-06-2015, 09:07 PM   #3
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Place it in single quotes?

Code:
ssh Tom@192.168.1.5 'ls -a | grep \.xyz; hostname'
Do you want the contents of .xyz listed or just the fact that it exists?
Also, this will be run from the home directory of "Tom" - e.g. /home/tom/
 
1 members found this post helpful.
Old 11-07-2015, 07:48 AM   #4
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks!
Yes. I want to know if the directory exists, not necessarily if there are files inside the directory.
Appreciate the feedback
 
Old 11-07-2015, 09:15 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by ntbluez View Post
Yes. I want to know if the directory exists, not necessarily if there are files inside the directory.
Then you might want "test" instead of "ls"

Code:
test -d ./xyz/ && echo There || echo Not There
 
1 members found this post helpful.
Old 11-07-2015, 09:45 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by Sefyir View Post
Place it in single quotes?

Code:
ssh Tom@192.168.1.5 'ls -a | grep \.xyz; hostname'
Is that sufficient? The local shell evaluates and strips off the ticks; th remote shell gets
Code:
ls -a |grep \.xyz; hostname
and strips off the backslash. The grep gets
Code:
.xyz
and that also matches
Code:
axyz
. And there is no anchor so it will even match
Code:
helloxyz
and
Code:
helloxyzhello
.
The previous solution is precise. Or
Code:
ssh Tom@192.168.1.5 'ls -d .xyz; hostname'

Last edited by MadeInGermany; 11-07-2015 at 09:48 AM.
 
Old 11-07-2015, 06:55 PM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
It's hard to manage all the backslashes and other quoting mechanisms when nesting command lines like this. MadeinGermany has got it right I think. A test or an ls are simpler solutions.
 
Old 11-10-2015, 06:29 PM   #8
ntbluez
LQ Newbie
 
Registered: Nov 2015
Posts: 21

Original Poster
Rep: Reputation: Disabled
I appreciate the feedback and hints you've provided! I noticed the output lists the information on two separate lines. What if I want the output to list the computer name and directory name or if it exists on one line? What would the command be to achieve this? Thanks again!
 
Old 11-10-2015, 10:51 PM   #9
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by ntbluez View Post
I appreciate the feedback and hints you've provided! I noticed the output lists the information on two separate lines. What if I want the output to list the computer name and directory name or if it exists on one line? What would the command be to achieve this? Thanks again!
Without knowing the command you are entering, it's kind of difficult to answer your question.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run multiple commands on one line Fixit7 Puppy 3 01-17-2015 04:29 PM
Where are command line commands stored ProfWEB Linux - General 2 01-25-2014 03:00 AM
ftp command line client - run multiple ftp commands in one line dlugasx Linux - Server 1 09-13-2012 06:24 AM
multiple ftp commands in 1 line hiddendragon Linux - Server 2 10-30-2011 12:17 PM
command line commands bcbotha Linux - Newbie 15 12-01-2008 08:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration