LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-21-2009, 04:12 PM   #1
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Rep: Reputation: 15
Trying to run a script...


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.

I tried running it like this:

Code:
[@crux source]$ chmod ugo+x avgwordlen
[@crux source]$ ./avgwordlen
Usage: avgwordlen filename1 ...
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?
 
Old 03-21-2009, 04:24 PM   #2
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
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 04:38 PM.
 
Old 03-21-2009, 04:37 PM   #3
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
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

Thanks again!!
 
Old 03-21-2009, 04:39 PM   #4
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
:-)
No problem, we all have our silly moments
 
Old 03-21-2009, 07:21 PM   #5
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
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

The same script, did the following command:

Code:
[@crux source]$ ./avgwordlen filename1
filename1 5.1
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?
 
Old 03-21-2009, 08:32 PM   #6
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
Quote:
Originally Posted by shorte85 View Post
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

The same script, did the following command:

Code:
[@crux source]$ ./avgwordlen filename1
filename1 5.1
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 08:38 PM.
 
Old 03-21-2009, 09:02 PM   #7
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
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.
 
Old 03-22-2009, 05:45 AM   #8
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
Quote:
Originally Posted by shorte85 View Post
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....
 
  


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
Correct output from script appears only when script is run interactively kaplan71 Linux - Software 2 01-15-2009 12:47 PM
I want to run script on the server from client machine in windows in a perl script vpradeep Linux - Newbie 2 09-01-2008 04:29 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 10:56 AM
Is a script, run at boot time from init.d, run with root authority? tmbrwolf53 Linux - Server 2 03-31-2007 09:15 PM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 10:34 AM


All times are GMT -5. The time now is 04:31 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration