This is topic The Bash Shell Thread in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=050093

Posted by Blayne Bradley (Member # 8565) on :
 
Google's being uncooperative, so for making a loop that terminates when the end of file is reached how would I do it or is counting the number of lines with grep the only way?

code:
add=$2
count=0
num=$(grep -c "" $1)

while $count <= $num
do
stuff
count=$[count+add]
done


 
Posted by Artemisia Tridentata (Member # 8746) on :
 
I thought you were going to complain about high gasoline prices and the evils of multi-national oil companies.
 
Posted by fugu13 (Member # 2859) on :
 
You're thinking too low level. Bash is a high level language. Try looking through these results: http://www.google.com/search?client=safari&rls=en&q=read+file+bash&ie=UTF-8&oe=UTF-8

The first link will probably be helpful.
 
Posted by Blayne Bradley (Member # 8565) on :
 
hmm the crux of the issue here ius that I need to read every nth line. does "read line" support such tinkering?
 
Posted by fugu13 (Member # 2859) on :
 
For that you'll need a counter in conjunction with while read line, or similar technique. I'm not familiar enough with bash to know what makes the most sense, but a counter isn't awful. You'll be freed from needing to know in advance how many lines there are (which is expensive), too.

Btw, it is almost certainly better to construct your script to take its input from stdin rather than an argument. And if you want to count lines, let me introduce you to the command wc.

What do you want the script to do, overall?
 
Posted by Blayne Bradley (Member # 8565) on :
 
"create a bash script, which takes 2 parameters, a file name, an interger n; the script should output every nth line of the file to standard out. Print an errr message if the file does not exist."


To run a script witht he 2 passed parameters its ./scriptname param1 param2 ?
 
Posted by Blayne Bradley (Member # 8565) on :
 
fugu do you have msn?
 
Posted by Blayne Bradley (Member # 8565) on :
 
Question if I do ./Script param1 param2 and check if the file exists how do I surpress bash's default standard ouput if a file/directory does not exist?
 
Posted by Mike (Member # 55) on :
 
This sounds like homework to me...
 
Posted by Blayne Bradley (Member # 8565) on :
 
on a related note anyone know how to non recusively find all files in my root directory older then 24 hours?

also how do i make a tarball non-recusrsively? I found the command online apparently the flag is 'n' but that doesnt seem to work.


tar cvf tarball.tar.gz ~

where does the n go?
 
Posted by fugu13 (Member # 2859) on :
 
Experimentation is the soul of programming.

Or something like that.

If you want to find, look to find. If you want to tar, look to tar. Ponder these well, and all will be revealed. It helps if you are drive a stick.
 
Posted by Bokonon (Member # 480) on :
 
Blayne, don't be circumspect, and don't get people to do your homework.

-Bok
 
Posted by Lyrhawn (Member # 7039) on :
 
quote:
Originally posted by Artemisia Tridentata:
I thought you were going to complain about high gasoline prices and the evils of multi-national oil companies.

I think you mean multi-national conglogmatates.
 
Posted by HollowEarth (Member # 2586) on :
 
have you tried find? (I realize that its name is hardly indicative of what it does and all.)
 
Posted by Blayne Bradley (Member # 8565) on :
 
quote:
Originally posted by Bokonon:
Blayne, don't be circumspect, and don't get people to do your homework.

-Bok

Don't be stupid, I'm not asking people to do my homework I'm asking for an explanation on how 2 commands work, which neither man nor the book seem to give any concrete examples, I have tried trial and error and it always recursively goes through every subdirectory when i just want the root.
 
Posted by El JT de Spang (Member # 7742) on :
 
For about the 11th time, you'll never be even a passable programmer if you don't have the discipline to solve your own problems.
 
Posted by Blayne Bradley (Member # 8565) on :
 
The most inane advice I had ever heard, this isn't a logic error, its just not understanding the manual pages or the linux book are saying on the command, they ay that tar cvf . makes a tarball and the n flag is needed to make it not do things recursively, I put it n tar, tar n, ncvf, cvfn, cvf -n, -n cvf its not working.

If no one at all asks for help when there's a problem then nothing is learned or accomplished.
 
Posted by El JT de Spang (Member # 7742) on :
 
Not true at all. Stop being so lazy.
 
Posted by Bokonon (Member # 480) on :
 
What version of tar are you using? What OS are you running?

-Bok
 
Posted by fugu13 (Member # 2859) on :
 
When the short path is not available, the long might do.
 
Posted by Mike (Member # 55) on :
 
Indeed, my version of tar doesn't take -n.
 
Posted by Blayne Bradley (Member # 8565) on :
 
quote:
Originally posted by El JT de Spang:
Not true at all. Stop being so lazy.

Now your just being an ignoramus, I would ask my teacher this question but I am not currently able to ask him the question, I have tried all of the above and it hasn't worked, so prove that I'm being lazy or don't post at all.

I am using Windows XP using Putty to access my schools Linux server, the version of tar is the GNU version.

Bash is the GNU born again Bourne Shell.
 
Posted by fugu13 (Member # 2859) on :
 
Read what I said above carefully, the answer lies within.
 
Posted by Blayne Bradley (Member # 8565) on :
 
specifying the actual path you mean?
 
Posted by fugu13 (Member # 2859) on :
 
Nope. It is a clue related to command line switches.

edit: and while I got tar to work non-recursively, I did not use the -n option, but I did read the man page.

Btw, why do you need to tar non-recursively? That's a pretty rare requirement.
 
Posted by Bokonon (Member # 480) on :
 
You didn't give me any version numbers, which would be more pertinent. Stuff can change quite a bit between versions.

Oh, and fugu definitely gave you a lead to follow-up on in that post.

-Bok
 
Posted by Fusiachi (Member # 7376) on :
 
Not that GNU aficionados like man pages, but...the man page for GNU tar solves your problem. At least the version shipped w/Debian.

man blayne, on the other hand, leaves me in the dark.

Best of luck.
 
Posted by Blayne Bradley (Member # 8565) on :
 
wait a second seems I'm a newb and it turns out I read the instructions of the question wrong


"archive as a tarball (*.tar.gz file) all the files i your home directory tree that have been modified in the last 24 hous. Hint use find.

Seems I'm supposed to do it to all the files in all of the sub directories of root.

Silly me [Embarrassed]

so I need to do some wierd piping of find to the tar command i think.
 
Posted by fugu13 (Member # 2859) on :
 
Something like that. Not the subdirectories of root, though, everything in your home directory.
 
Posted by Blayne Bradley (Member # 8565) on :
 
i usually refer to my home directory as root since its my personal linux server account.
 
Posted by fugu13 (Member # 2859) on :
 
Don't; it is inaccurate and will result in misunderstandings when you discuss things with others.
 
Posted by Blayne Bradley (Member # 8565) on :
 
hmm, I try to do a groupadd command, the man page says it exists but when i try to execute the command it doesnt work...
 
Posted by fugu13 (Member # 2859) on :
 
You won't have permission to do that.
 
Posted by Blayne Bradley (Member # 8565) on :
 
humbug.
 
Posted by fugu13 (Member # 2859) on :
 
*chuckle*

How dare the admins keep users from messing with things they shouldn't mess with on servers [Razz]
 
Posted by Blayne Bradley (Member # 8565) on :
 
trying to make a MySQL server in my subaccount.
 
Posted by fugu13 (Member # 2859) on :
 
That is almost certainly a Bad Idea and possibly against your school's IT policies.
 
Posted by Blayne Bradley (Member # 8565) on :
 
considering im in computer science and supposed to learn this stuff I cannot possibly comprehend why it would be.
 
Posted by Blayne Bradley (Member # 8565) on :
 
hmm find has just broke on me.

find ~ -atime 1 used to find me all he files accessed in the last 24 hours.

now it doesnt.

Wierd.
 
Posted by Blayne Bradley (Member # 8565) on :
 
aaaah -ctime not -atime.


anyone know how to get rid of wierdly named files? like "-n"???
 
Posted by Bokonon (Member # 480) on :
 
rm \-n

Any weird characters can be escaped as literals with a preceding backslash. You can also probably just put quotes around the file name you want to delete.

-Bok
 
Posted by fugu13 (Member # 2859) on :
 
First, computer science only peripherally relates to installing databases. It is a useful skill, but not one essential to being a decent, or even an expert, computer scientist.

Second, even if it is a good skill to learn, that does not mean it is a good idea to do on any account you have access to. A database server can, for instance, put considerable load on a system. If you do not secure it properly, your account could be compromised. There will be contention for ports, that could disrupt services on the server. Et cetera.

And you're supposed to already have access to a database, aren't you? Yeah, you must have been using it before. So if you want to install a database, install it on your own machine (preferably running linux, but MySQL installs okay on Windows).
 
Posted by Blayne Bradley (Member # 8565) on :
 
Actually our CS program seems to cover all the bases, we will be isntalling MySQL and Linux in the next couple of weeks, I was hoping to do some of the leg work ahead of time.
 
Posted by fugu13 (Member # 2859) on :
 
If you're going to be installing Linux, you'll be doing at least some of that stuff on a completely different box.

Of course, installing Linux (at least for the common variants) is as easy as installing Windows, nowadays, so I wonder what they're going to be teaching you.

And the proper way to install MySQL is (pretty much always) to use the command to install the MySQL package with your package manager.
 
Posted by Fusiachi (Member # 7376) on :
 
quote:
Originally posted by fugu13:
If you're going to be installing Linux, you'll be doing at least some of that stuff on a completely different box.

Of course, installing Linux (at least for the common variants) is as easy as installing Windows, nowadays, so I wonder what they're going to be teaching you.

Well, maybe they'll be using slackware and compiling the kernel. Regardless of the degree of difficulty, Comp Sci students ought to have some contact with linux/unix.
 
Posted by HollowEarth (Member # 2586) on :
 
quote:
Originally posted by Blayne Bradley:
so I need to do some wierd piping of find to the tar command i think.

Watch out for the line length limit. Its really easy to go over it when piping from find to tar. There are several ways around it, but its really terrible when you untar that file to find out it didn't really work.

Personally, I like using -T.
 
Posted by Blayne Bradley (Member # 8565) on :
 
ive decided to write the find results toa file and then run a do while loop going line by line tar balling it individually, tar seems to have by default cmpatibility with paths.
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2