Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I'm trying to run a script named avgwordlen, and avgwordlen script contains the following:
Code:
#!/bin/bash
if [ "$1" == "" ]
then
echo 'Usage: avgwordlen filename1 ...'
exit 1
fi
for arg in $*
do
stats=(`cat $arg | wc`)
# Since wc outputs 3 numbers (namely, # of lines, # of words, and # of chars)
# it has been stored in stats[0], stats[1], stats[2] respectively.
# Now use calculator tool bc to find the average word length.
echo $arg `echo "scale=1 ; (${stats[2]} - ${stats[0]}) / ${stats[1]}" | bc`
done
Now I was trying to play with it, and made a file called filename1 and put a bunch of my friends names in it.
Now, as I understand it... All I get out of running this script is "Usage: avgwordlen filename1 ..." However, I thought it was supposed to give me the number of lines, number of words and number of chars in three columns. Am I doing something wrong?
The script is asking you to give it a filename as an 'argument'. So as you've created a file called 'filename1'. You need to execute it as:-
Code:
./avgwordlen filename1
The script is asking you to give it a filename to run the script against. You can run the script against any file, not just files called 'filename1' ie:-
Code:
# ls
file1 file2 file3 file4
./avgwordlen file1
But in the same light the script there is also able to parse more than one filename at once and give you details on each file you pass to the script as 'arguments'... for instance
Code:
# ls
file1 file2 file3 file4
./avgwordlen file1 file2 file4
So try it out by doing something similar as i've shown... do an 'ls' (list/show files/folders in the current directory basically), and then pick a file or two to run the script against ('arguments') and then execute the script using the filenames you got from output of the 'ls' command.
The reason your not seeing anything other than the output you showed us above, is because you haven't given the script any 'arguements' (filenames for the script to execute against).
Code:
if [ "$1" == "" ]
then
echo 'Usage: avgwordlen filename1 ...'
exit 1
fi
This part of the script is doing a simple check on the 'arguments' (filenames) passed into it, because you've not given it any then the script displays the message you see. When i talk about 'arguments' like this what i'm talking about is:-
Code:
./command argument
./command {arg1} [arg2] [arg3]
In the above {}=required []=optional arguments.
Last edited by helptonewbie; 03-21-2009 at 03:38 PM.
I just gave it a try, thank you so much!! I appreciate your help. I really do!! Geez, something so simple that I overlooked, as soon as I had looked at your post I smacked my forehead because I instantly knew what I had done wrong. lol
Okay, I've ran into a problem. Either I'm not doing the math problem correctly to make sure what the script brought up was correct, or I'm once again not understanding the script. lol
But when I checked manually, as far as making sure that it calculated it correctly. I got 4.8 for filename1. I got this by counting the number of words, number of lines, and number of chars. The contents within the filename1 file is the following:
What is your favorite color?
What is your least favorite color?
Do you know how many times I have to say this?
Three hundred times.
This is what I got:
# of lines (stats[0]) = 4
# of words (stats[1]) = 25
# of chars (stats[2]) = 124
This is how I calculated it:
124-4/25 = 4.8
How am I getting 4.8 when it should be 5.1, am I getting something wrong?
Okay, I've ran into a problem. Either I'm not doing the math problem correctly to make sure what the script brought up was correct, or I'm once again not understanding the script. lol
But when I checked manually, as far as making sure that it calculated it correctly. I got 4.8 for filename1. I got this by counting the number of words, number of lines, and number of chars. The contents within the filename1 file is the following:
What is your favorite color?
What is your least favorite color?
Do you know how many times I have to say this?
Three hundred times.
This is what I got:
# of lines (stats[0]) = 4
# of words (stats[1]) = 25
# of chars (stats[2]) = 124
This is how I calculated it:
124-4/25 = 4.8
How am I getting 4.8 when it should be 5.1, am I getting something wrong?
I hope i'm not doing your homework for you.... its homework for a reason, you would learn a lot more doing background reading.
However i will give a hint at the problem you are experiencing. I will tell you that the numbers your using to do the manual calculation, is in fact incorrect for that piece of text. I will ask you to go off and look at this page:- http://www.linuxjournal.com/content/bash-arrays
Will teach you the basics of bash variable arrays, and then i would tell you to add a command to the script that would 'echo' (hint!), out all the values in the stats array. You will find the numbers don't match the numbers you think are being used in the calculation. And then i will say that you should really work out why the numbers don't match the numbers you have displayed.
Do what i've said here and then show the output you get (show your change in the script), if you do all that, it should be reasonably obvious whats happening. If you still have a problem after that. Come back here and i'll try to hint some more :-)
Last edited by helptonewbie; 03-21-2009 at 07:38 PM.
I actually figured it out on my own. I forgot that when you create files in the VI editor, that it also counts the very last space after each line as a character as well. So that is where my math was going wrong and was able to get the same answer as the script was giving me. So it's all good, and thank you for the information you gave me helptonewbie.
I actually figured it out on my own. I forgot that when you create files in the VI editor, that it also counts the very last space after each line as a character as well. So that is where my math was going wrong and was able to get the same answer as the script was giving me. So it's all good, and thank you for the information you gave me helptonewbie.
Excelent, i'm glad to hear it. However its not a space that goes at the end of each line... its something different. See if you can work it out, its all useful stuff to know. Its not just Vi either....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.