ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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!
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
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!
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.
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.
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!
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.