Hi all,
Currently I am using AIX at work. I am no administrator of the system, merely a user.
I am trying to create a script that will accept either a file with a list of files as content, or a list of files. At first I build it for myself, but I want it to be available for my colleagues in the longer run.
The script is called mailen (Dutch for mailing). Either you start the script as: ./mailen file1 file2 file3 or as: ./mailen -f file_list file1 file2
In the first case, it should e-mail the files (file1, file2 and file3) to me as an attachment. In the second case, it should read the content of the file file_list and e-mail me the files in the content from that list and file1 and file2.
So I started with a script that would e-mail me the files I put on the command line. The script works as designed.
The next step is to try and see if I can make a script that will get the content of the file in case I use the -f (file) option.
The script I have should contain 2 options, namely -v (verbose) and -f (file to read content from).
This is what I have so far:
Code:
#!/bin/bash
#
V_FLAG=
F_FLAG=
FILE=
while getopts vf: opt
do
case $opt in
v)
V_FLAG=1
;;
f)
F_FLAG=1
FILE="$OPTARG"
;;
\?)
echo ""
echo "Usage of mailen:"
echo "$0 [-v] [-f filename] [file ...]"
echo ""
exit 1
;;
esac
done
shift $((OPTIND - 1))
echo "Rest $@"
echo ""
echo "V_FLAG=${V_FLAG}"
echo "F_FLAG=${F_FLAG}"
echo "FILE=${FILE}"
exit 0
Now running it:
$ ./mailen_getopts.sh -f foo foo1 foo2
Rest foo1 foo2
V_FLAG=
F_FLAG=1
FILE=foo
$ ./mailen_getopts.sh -f foo -v foo1 foo2
Rest foo1 foo2
V_FLAG=1
F_FLAG=1
FILE=foo
$ ./mailen_getopts.sh -v foo -f foo1 foo2
Rest foo -f foo1 foo2
V_FLAG=1
F_FLAG=
FILE=
As you can see, in the first 2 cases output is as expected. However in the last case, -f seems not to be an option anymore... For some reason if you tell getopts that an option should not have an argument, it will not parse the rest of the string if you don't provide a new option right away.
Has anyone encountered this before? Does anyone know how to overcome this without loosing too much flexibility?