From your question it is not sureclear what you want !
anyhowAnyhow it seems that you want the number correspondentcorresponding to the last argument
#!/bin/bash
foo=0;
while (($#));do
case "${1}" in
'-n')
foo=1;
shift
;;
'-e')
foo=2;
shift
;;
'-i')
foo=3;
shift
;;
'-k')
foo=4;
shift
;;
*)
echo invalid flag;
exit 1;
;;
esac
done
echo This is foo: $foo
#!/bin/bash
foo=0;
while [[ $# -gt 0 ]]; do
case "${1}" in
'-n')
foo=1;
shift
;;
'-e')
foo=2;
shift
;;
'-i')
foo=3;
shift
;;
'-k')
foo=4;
shift
;;
*)
echo "Invalid flag";
exit 1;
;;
esac
done
echo "This is foo: $foo"
else ifIf instead you wannawant a mecanismemechanism that treats and validates arguments before being processed
, you can use something like
#!/bin/bash
inputf='';
outputf='';
text='';
format='';
while (($#));do
case "${1}" in
'-i')
inputf="${2}";
shift 2
;;
'-o')
outputf="${2}";
shift 2
;;
'-t')
text="${2}";
shift 2
;;
'-f')
format="${2}";
shift 2
;;
esac
done
#!/bin/bash
inputf='';
outputf='';
text='';
format='';
while [[ $# -gt 0 ]];do
case "${1}" in
'-i')
inputf="${2}";
shift 2
;;
'-o')
outputf="${2}";
shift 2
;;
'-t')
text="${2}";
shift 2
;;
'-f')
format="${2}";
shift 2
;;
esac
done