1

I'm writing a script to elaborate many text file.

I need to pass N text files to my bash script.

The script invocation is like this:

:~$ ./bristat [-u user] [-p] [-m] file1.log...fileN.log

The script elaborate the logfile(s) following arguments -u -m -p.

  • args -u -m -p are optional (i can invoke the script with none, any or all of these args);
  • file1.log...fileN.log are necessary for the execution ( 0 < files <= N )
  • logfiles have all the suffix .log

My question is: how to identify and check these logfiles in the command line? I Don't care (now) about content of the files and what to do, I just need the script recognise them, do the attributes checking, and then process them (but how to process is not what I ask here). I don't know if I was clear. Ask for better clarifications.

This is my code without files checking. I need to integrate here.

#!/bin/bash

if [ $# == 0 ]; then
    echo "No argument passed:: ERROR"
    exit 
fi

usage="Usage: bristat [-u args] [-p] [-m] logfile1...logfileN" 
params=":u:pm" 
U=0 P=0 M=0

while getopts $params OPT; do
    case $OPT in        u)
        case ${OPTARG:0:1} in
                -)
            echo "Invalid argument $OPTARG" >&2
            exit
        esac   
        echo "[-u] User = $OPTARG" >&2
        U=$((++U))
        ;;  p)
        echo "[-p] Number of lost games = " >&2
        P=$((++P))
        ;;  m)
        echo "[-m] Average of total points = " >&2
        M=$((++M))
        ;;  \?)
        echo $usage >&2
        exit
        ;;  :)
        echo "Option [-$OPTARG] requires an argument" >&2
        exit
        ;;
    esac 
done   

#check for duplicate command in option line 
if [ "$U" -gt "1" ]; then
echo "Duplicate option command line [-u]"
exit 
fi

if [ "$P" -gt "1" ]; then
echo "Duplicate option command line [-p]"
exit 
fi 

if [ "$M" -gt "1" ]; then
echo "Duplicate option command line [-m]"
exit 
fi

shift $[$OPTIND -1] # Move argument pointer to next.

For more clarity, the script examine the logfile to return statistics:

  • -u check if user is an authorized name
  • -m returns the average of total points about a game
  • -p returns the number of lost match about a game

Edit

If I want to call the arguments in random position? I mean that (i.e.):

:~$ ./bristat [-u user] [-p] [-m] file1.log file2.log file3.log

:~$ ./bristat [-m] file1.log file2.log [-u user] [-p] file3.log

:~$ ./bristat [-m] file1.log [-p] file2.log [-u user] file3.log

could be the same invocations. How can I change my code? Any suggestions?

Community
  • 1
  • 1
Kyrol
  • 3,475
  • 7
  • 34
  • 46

1 Answers1

0

You want to iterate your list of filenames with shift

after you get your arguments,

shift $(( OPTIND-1 ))
while [ -f $1 ] 
do 
   #do whatever you want with the filename in $1.
   shift
done
hometoast
  • 11,522
  • 5
  • 41
  • 58
  • Where can I put this code in my script? before or inside my `while getopts $params OPT;` ? – Kyrol Aug 05 '13 at 16:03
  • heck, I didn't even see the final shift at the bottom of your code. Right there at the very bottom. – hometoast Aug 05 '13 at 16:04
  • 1
    Regarding your "arguments in any order"... there needs to be SOME structure to it. *Especially* if there is a "list" of items in there somewhere. In other words, don't do that. – hometoast Aug 09 '13 at 10:38