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?

2
  • 2
    This good answer may help: stackoverflow.com/a/7530327/1983854 Commented Aug 5, 2013 at 15:56
  • Thanks a lot. I have another doubt. Look my Edit, please. Commented Aug 5, 2013 at 19:57

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Where can I put this code in my script? before or inside my while getopts $params OPT; ?
heck, I didn't even see the final shift at the bottom of your code. Right there at the very bottom.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.