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?