LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2015, 08:22 PM   #1
gdizzle
Member
 
Registered: Jul 2012
Posts: 234

Rep: Reputation: Disabled
Need Help with Positional Parameters


Hi I have this script that works great for one file.

e.g:

Code:
./filestats.sh /etc/hosts
/etc/hosts:
   lines: 5
   blank: 1 (20%)
   chars: 251 in 16 word(s) (15 char/word)

I want it to work for multiple files:

Code:
e.g:

./filestats.sh /etc/hosts /etc/resolv.conf

How would I get the code below to dynamically parse any number of files?

shift, forloop? confused at the moment.

Thanks for your time.


Below is the code:

Code:
#!/bin/bash
#filestats
 
#!/bin/bash
 
if ! [[ "$#" -ge 1 ]]
then
        echo "Usage: $0 <filename>"
        exit 188
fi
 
#Count the amount of lines for the file
 
lines="$(wc -l < "$1")"  
 
#Count the amount of blank lines in the file
 
blanklines="$(grep -c '^$' "$1")"
 
#Calculate the Percent
 
percent=$((blanklines*100/lines))
 
#Count the amount of chars for the file
 
chars="$(wc -c < "$1")"  
 
#Count the amount of words for the file
words="$(wc -w < "$1")"  
 
#Total Chars per word
totalchar=$((chars/words))
 
# Print the Outputs
 
totalchar=$((chars/words))
 
echo "$1:"
echo "  " "lines: $lines"
echo "  " "blank: $blanklines" '('$percent'%)'
echo "  " "chars: $chars in ${words} word(s) ($totalchar char/word)"
 
Old 05-20-2015, 09:10 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
shift - see example 4-7 in (my copy anyway) of abs
 
1 members found this post helpful.
Old 05-20-2015, 10:09 PM   #3
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
until [ -z "$1" ]
do
echo "processing $1"
shift
done
 
1 members found this post helpful.
Old 05-20-2015, 10:38 PM   #4
gdizzle
Member
 
Registered: Jul 2012
Posts: 234

Original Poster
Rep: Reputation: Disabled
Thanks guys for your help
 
  


Reply



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
perl positional parameters/ casperdaghost Programming 6 11-11-2011 01:08 AM
HELP! Positional Parameters are giving me a headache! Scott_82 Linux - Newbie 7 05-06-2007 03:32 PM
shell: positional parameters... neidias Programming 1 04-04-2005 02:45 PM
Positional Parameters (arguments) desbyleo Solaris / OpenSolaris 2 10-25-2004 11:51 AM
Help with positional parameters Read_Icculus Linux - General 7 11-02-2003 04:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:44 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
Open Source Consulting | Domain Registration