LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-20-2005, 01:30 PM   #1
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 401

Rep: Reputation: 31
Help with piping


I'm having trouble understanding something. I've posted in another section about this, but maybe this is a better place for it. I'm trying to run a single command from one machine to another machine, but I can't get it to work right. I hope someone can explain what I'm doing wrong.

I run this command to find a file with a certain size and move it another directory:


ssh machine2 ls -al /tmp/test* | grep 4106 | awk '{ print "cp", $9, "/tmp/junk/" }' | bash


I get this error:


cp: cannot stat `/tmp/testfile2-1.txt': No such file or directory


When I terminal to the node and remove the ssh machine2 this command works just fine.

ls -al /tmp/test* | grep 4106 | awk '{ print "cp",$9,"/tmp/junk/" }' | bash


I'm I doing something wrong here? Thanks for the help!

Last edited by mijohnst; 10-20-2005 at 01:37 PM.
 
Old 10-20-2005, 01:53 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
I haven't put it into ssh yet but on your original concept, the part of incrementing count can be trimmed down to something like this...
for (( i=1; i=<3; i++ )) ; do
echo n$i
ssh n$i

Quote:
declare COUNT=1

while [ $COUNT -le 32 ]; do
echo "n""$COUNT"
ssh n"$COUNT" ls -al /tmp/*.txt | awk '{print $9, $5}'\
if [[ $5 = 4106 ]] ; then # Look for file size of 4106k
echo "Deleting $9"
fi
COUNT=$(($COUNT+1))
done
Here's a sample I was putzing with on the local machine.
Code:
#!/bin/bash

clear
for i in tmp/*.txt ;do
   a=`echo $i | cut -d/ -f2`
   b=`stat -c %s "$i"`
if [ $b -ge 4106 ] ; then
   echo $a is large $b
   echo `mv "$i" tmp/junk`
else
   echo $a is small $b
fi
done
 
Old 10-20-2005, 05:37 PM   #3
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 401

Original Poster
Rep: Reputation: 31
Wow homey... I like you're script a lot better then I do mine. It's much more advanced. I hope that I can get it working with my while loop to do many machines. Thanks for the input!
 
Old 10-20-2005, 06:16 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
You're welcome!
A couple of notes: Where I have tmp/*.txt ... I made a tmp in the home folder. In reality, you might have to change that to /tmp/*.txt and change the cut command to match. .... -f3 or what ever gives you the correct file name.

For example:
Code:
#!/bin/bash

clear
for i in /tmp/*.txt ;do
   a=`echo $i | cut -d/ -f3`
   b=`stat -c %s "$i"`
if [ $b -ge 4106 ] ; then
   echo $a is large $b
   echo `mv "$i" /tmp/junk`
else
   echo $a is small $b
fi
done
Also, on the machine loop, you might want to use something other than i as that is already being used in the script.
For example:
for (( c=1; c=<3; c++ )) ; do
echo n$c
ssh n$c

I have never tried something like this on ssh so I'm not sure how to proceed with that part yet.
 
Old 10-20-2005, 06:35 PM   #5
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Re: Help with piping

Quote:
Originally posted by mijohnst
I'm having trouble understanding something. I've posted in another section about this, but maybe this is a better place for it. I'm trying to run a single command from one machine to another machine, but I can't get it to work right. I hope someone can explain what I'm doing wrong.

I run this command to find a file with a certain size and move it another directory:


ssh machine2 ls -al /tmp/test* | grep 4106 | awk '{ print "cp", $9, "/tmp/junk/" }' | bash


I get this error:


cp: cannot stat `/tmp/testfile2-1.txt': No such file or directory


When I terminal to the node and remove the ssh machine2 this command works just fine.

ls -al /tmp/test* | grep 4106 | awk '{ print "cp",$9,"/tmp/junk/" }' | bash


I'm I doing something wrong here? Thanks for the help!
I think the problem here is that the pipe is applied after the ssh. In other words, you do a local grep and a local awk and a local bash cp on a remote file name and the file does not exist locally.
try :
ssh machine2 "ls -al /tmp/test* | grep 4106 | awk '{ print "cp", $9, "/tmp/junk/" }' | bash"
(with the quotes so the whole piped commands are ssh'ed and executed remotely and not just the ls)

Last edited by Agrouf; 10-20-2005 at 06:41 PM.
 
Old 10-21-2005, 08:28 AM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
I was putzing with the ssh part. I think you would best put the machine names into a file and read that file. For me at least, the machines aren't likely to be called m1, m2 m3 etc etc.
Here is a sample list.txt which has the machine names.
Code:
curly
tower
moe
vaio
desk1
There is a nice example of "Here Document" in the Advanced Bash Scripting Guide which you can read online.
Note: Unless you have ssh to run without passwords, you will have to put in a password twice for each machine.
Code:
#!/bin/bash
LIST="/home/list.txt"
FILE=test
(
cat <<'EOF'
#!/bin/bash

for i in /tmp/*.txt ; do
   a=`echo $i | cut -d/ -f3`
   b=`stat -c %s "$i"`
if [ $b -ge 4106 ] ; then
   echo $a is large $b
   echo `mv "$i" /tmp/junk`
else
   echo $a is small $b
fi
done

exit 0
EOF
) > $FILE
chmod +x $FILE

for name in `cat ${LIST}` ; do
   scp $FILE root@$name:/home
   ssh root@$name exec /home/test
done
exit
Edit: made some changes.

Last edited by homey; 10-21-2005 at 09:21 AM.
 
Old 10-21-2005, 01:26 PM   #7
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 401

Original Poster
Rep: Reputation: 31
Homey,

You are too cool for words... Thank you so much for taking the time to figure out a problem for such a newbie. I wish there was a way to repay you for your work.

I'm started tweaking your script and it's working!!
 
Old 10-21-2005, 04:14 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Glad to help!
Don't forget to google for the Advanced Bash Scripting Guide. It has a lot of great info and examples.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Piping video out to the TV only. dredgemortle Mandriva 3 07-05-2004 12:48 AM
piping between backends robhargreaves Programming 1 05-01-2004 07:01 AM
Piping question OtisLinux Linux - Software 1 02-12-2004 01:38 PM
piping data tearinox Linux - Newbie 9 12-09-2003 01:43 PM
piping commands john8675309 Linux - Software 3 12-06-2003 07:42 AM


All times are GMT -5. The time now is 01:33 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration