FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » The Bash Shell Thread

   
Author Topic: The Bash Shell Thread
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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


IP: Logged | Report this post to a Moderator
Artemisia Tridentata
Member
Member # 8746

 - posted      Profile for Artemisia Tridentata   Email Artemisia Tridentata         Edit/Delete Post 
I thought you were going to complain about high gasoline prices and the evils of multi-national oil companies.
Posts: 1167 | Registered: Oct 2005  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
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.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
hmm the crux of the issue here ius that I need to read every nth line. does "read line" support such tinkering?
IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
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?

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
"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 ?

IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
fugu do you have msn?
IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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?
IP: Logged | Report this post to a Moderator
Mike
Member
Member # 55

 - posted      Profile for Mike   Email Mike         Edit/Delete Post 
This sounds like homework to me...
Posts: 1810 | Registered: Jan 1999  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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?

IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
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.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Bokonon
Member
Member # 480

 - posted      Profile for Bokonon           Edit/Delete Post 
Blayne, don't be circumspect, and don't get people to do your homework.

-Bok

Posts: 7021 | Registered: Nov 1999  |  IP: Logged | Report this post to a Moderator
Lyrhawn
Member
Member # 7039

 - posted      Profile for Lyrhawn   Email Lyrhawn         Edit/Delete Post 
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.
Posts: 21898 | Registered: Nov 2004  |  IP: Logged | Report this post to a Moderator
HollowEarth
Member
Member # 2586

 - posted      Profile for HollowEarth   Email HollowEarth         Edit/Delete Post 
have you tried find? (I realize that its name is hardly indicative of what it does and all.)
Posts: 1621 | Registered: Oct 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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.
IP: Logged | Report this post to a Moderator
El JT de Spang
Member
Member # 7742

 - posted      Profile for El JT de Spang   Email El JT de Spang         Edit/Delete Post 
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.
Posts: 5462 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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.

IP: Logged | Report this post to a Moderator
El JT de Spang
Member
Member # 7742

 - posted      Profile for El JT de Spang   Email El JT de Spang         Edit/Delete Post 
Not true at all. Stop being so lazy.
Posts: 5462 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
Bokonon
Member
Member # 480

 - posted      Profile for Bokonon           Edit/Delete Post 
What version of tar are you using? What OS are you running?

-Bok

Posts: 7021 | Registered: Nov 1999  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
When the short path is not available, the long might do.
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Mike
Member
Member # 55

 - posted      Profile for Mike   Email Mike         Edit/Delete Post 
Indeed, my version of tar doesn't take -n.
Posts: 1810 | Registered: Jan 1999  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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.

IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
Read what I said above carefully, the answer lies within.
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
specifying the actual path you mean?
IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
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.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Bokonon
Member
Member # 480

 - posted      Profile for Bokonon           Edit/Delete Post 
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

Posts: 7021 | Registered: Nov 1999  |  IP: Logged | Report this post to a Moderator
Fusiachi
Member
Member # 7376

 - posted      Profile for Fusiachi   Email Fusiachi         Edit/Delete Post 
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.

Posts: 433 | Registered: Feb 2005  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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.

IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
Something like that. Not the subdirectories of root, though, everything in your home directory.
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
i usually refer to my home directory as root since its my personal linux server account.
IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
Don't; it is inaccurate and will result in misunderstandings when you discuss things with others.
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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...
IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
You won't have permission to do that.
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
humbug.
IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
*chuckle*

How dare the admins keep users from messing with things they shouldn't mess with on servers [Razz]

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
trying to make a MySQL server in my subaccount.
IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
That is almost certainly a Bad Idea and possibly against your school's IT policies.
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
considering im in computer science and supposed to learn this stuff I cannot possibly comprehend why it would be.
IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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.

IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
aaaah -ctime not -atime.


anyone know how to get rid of wierdly named files? like "-n"???

IP: Logged | Report this post to a Moderator
Bokonon
Member
Member # 480

 - posted      Profile for Bokonon           Edit/Delete Post 
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

Posts: 7021 | Registered: Nov 1999  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
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).

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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.
IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
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.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Fusiachi
Member
Member # 7376

 - posted      Profile for Fusiachi   Email Fusiachi         Edit/Delete Post 
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.
Posts: 433 | Registered: Feb 2005  |  IP: Logged | Report this post to a Moderator
HollowEarth
Member
Member # 2586

 - posted      Profile for HollowEarth   Email HollowEarth         Edit/Delete Post 
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.

Posts: 1621 | Registered: Oct 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
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.
IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

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