LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

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

Rep: Reputation: 31
Simple Bash function


I'm gathering some data from a few machines that I have named box1, box2, box3, etc. I want to automate this, but I'm not a good scripter... yet...

How to I tell my script to increment the computer name by one number in the command line?


declare -i COMPUTERNUMBER=1

# List Files in /tmp

printf "%s" box"$COMPUTERNUMBER"
ssh box"$COMPUTERNUBER" ls -al /tmp/ | awk '{print $9, $5}'


As you can see I'm telling my script that $COMPUTERNUMBER is equal to 1 and added "box" makes "box1." What I want to do is add "$COMPUTERNUMBER + 1", and loop until it reaches a certain amount of computers? So I have 10 computers (1-10) named exactly the same except for the last number and I want to list what is under each /tmp directory and only print columns 9 and 5. I'm I way off here?

Thanks in advance.

Last edited by mijohnst; 10-18-2005 at 06:26 PM.
 
Old 10-18-2005, 06:37 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Assuming bash:
Code:
COUNT=1
while [ $COUNT -le 10 ]; do
	echo box$COUNT   # or ssh box$COUNT ls -la ..
	COUNT=$(($COUNT+1))
done
or:
Code:
for COUNT in $(seq 1 10); do
	echo box$COUNT   # or ssh box$COUNT ls -la ..
done
 
Old 10-18-2005, 11:35 PM   #3
alf55
LQ Newbie
 
Registered: Oct 2005
Posts: 7

Rep: Reputation: 0
Quote:
Originally posted by Hko
Assuming bash:
Code:
COUNT=1
while [ $COUNT -le 10 ]; do
	echo box$COUNT   # or ssh box$COUNT ls -la ..
	COUNT=$(($COUNT+1))
done
or:
Code:
for COUNT in $(seq 1 10); do
	echo box$COUNT   # or ssh box$COUNT ls -la ..
done
In bash

Code:
let count=1
while [ $count -lt 11 ] ; do
    echo ....
     let count++
done
The "let" bash command is there to allow simpler usage of math expressions.


For more help on "let" type "help let" from the bash prompt.

The help for bash commands is found via "help cmd" while help for external commands to the shell is found via "man cmd" and sometimes via "info cmd".
 
Old 10-19-2005, 11:01 AM   #4
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 401

Original Poster
Rep: Reputation: 31
You guys are awesome... Thanks so much!

What would you recommend as a good tutoral on using bash for admin purposes? I know there are a lot out there, I just want to see what you all think. Thanks again!
 
Old 10-19-2005, 11:05 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by alf55
The "let" bash command is there to allow simpler usage of math expressions.
I didn't know that. THX.
 
Old 10-19-2005, 11:07 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by mijohnst
What would you recommend as a good tutoral on using bash for admin purposes?
http://www.tldp.org/guides.html#abs
 
Old 10-19-2005, 12:12 PM   #7
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 401

Original Poster
Rep: Reputation: 31
I'm using the 'while' that you suggested and it's working great for listing the directories! Now if I want to recognize a pattern in the file size and run a command on it, can I add an "if" statement in the while loop? So I guess what I want to do is look at the file size and delete it if it equals a certain size. Do I need to make the $5 in my awk command a variable?


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


I guess my problem is that when I put $5 looks for a variable and not the $5 in my awk command. I know I'm doing this wrong. I need to take a class because I'm a
 
Old 10-19-2005, 05:55 PM   #8
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 401

Original Poster
Rep: Reputation: 31
Thanks to you all, I've got my script doing almost what I want it to do. I'm telling it to ssh to a machine, list a directory, grep for a file size and awk out a command.



while [ $COUNT -le 3 ]; do
echo ""
echo n"$COUNT"
ssh n"$COUNT" ls -al /tmp/test* | grep 4106 | awk '{print "cp",$9," /tmp/junk"}' | bash
COUNT=$(($COUNT+1))
done


My only problem now is that it's moving the file into the "/tmp/junk" directory on the machine I'm running the script from instead of where it's collecting the data. How do I force it to use the "/tmp/junk" on the computer that the data is being gathered from? This has actually been pretty fun. Thanks for the help!
 
Old 10-20-2005, 02:10 PM   #9
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
Quote:
ls -al /tmp/test* | grep 4106 | awk '{print "cp",$9," /tmp/junk"
Wtf is this supposed to do?

Using 'ls -al | ...' to retrieve data about a file is pointless (though many people do it, and not just newbies).

Use the stat command, e.g.

Code:
stat -c "%s %n" /bin/bash
will print the size and name of /bin/bash. See man stat for other % parameters available.

Quote:
My only problem now is that it's moving the file into the "/tmp/junk" directory on the machine I'm running the script from instead of where it's collecting the data. How do I force it to use the "/tmp/junk" on the computer that the data is being gathered from? This has actually been pretty fun. Thanks for the help!
Well, it would do. The | is interpreted by your shell and therefore is being run on your local machine, not the box you're ssh'ing to. Try quoting the whole thing.
 
Old 10-20-2005, 02:27 PM   #10
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 401

Original Poster
Rep: Reputation: 31
Thanks ioerror! Yes, I'm a newbie... But I learn more by the day. Thanks for the input!
 
Old 10-20-2005, 04:26 PM   #11
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Yet one more way to count in Bash.

for (( num=1; num<10; num++ )); do
COMPUTERNAME=box$num
...
done
 
Old 10-22-2005, 08:40 AM   #12
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Bear in mind that stat() is not a command that's available on other UNIX systems so if you want your script to run on other systems than Linux, ls is the way to go.
 
Old 10-22-2005, 08:45 AM   #13
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by mijohnst
Thanks to you all, I've got my script doing almost what I want it to do. I'm telling it to ssh to a machine, list a directory, grep for a file size and awk out a command.



while [ $COUNT -le 3 ]; do
echo ""
echo n"$COUNT"
ssh n"$COUNT" ls -al /tmp/test* | grep 4106 | awk '{print "cp",$9," /tmp/junk"}' | bash
COUNT=$(($COUNT+1))
done


My only problem now is that it's moving the file into the "/tmp/junk" directory on the machine I'm running the script from instead of where it's collecting the data. How do I force it to use the "/tmp/junk" on the computer that the data is being gathered from? This has actually been pretty fun. Thanks for the help!
You need to hide the pipes from your shell:
Code:
ssh n"$COUNT" ls -al /tmp/test* \| grep 4106 \| awk '{print "cp",$9," /tmp/junk"}'
I haven't tested this; normally I'd just put the whole pipeline in quotes, but I'll leave you to work out the details of getting the quoting to work.
 
Old 10-23-2005, 12:40 PM   #14
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 401

Original Poster
Rep: Reputation: 31
Thanks eddiebaby1023 for looking at this... I'm going to play more with this as a way to just learn what I'm doing. Hiding the pipes is something I hadnt' though of. Thanks!
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash function pazvant Linux - Software 1 03-08-2005 07:47 AM
simple python function question Tyir Programming 2 03-28-2004 10:53 PM
Can't get a simple C function to work with GCC chris.hicks Programming 3 10-29-2003 11:09 AM
stat function in BASH... PokerFace Programming 1 11-04-2002 11:31 AM
compile within bash function? adam_boz Programming 1 09-17-2002 11:13 PM


All times are GMT -5. The time now is 09:52 AM.

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