I am trying to process a command line using getopts in bash. I have to pass three arguments after -w option. For example, -w 1 do loop. It should print one line before and after each line from loop that contains the pattern do. This is the code I have now:
#!/bin/bash
file=`ls | grep ^$1$`
pattern=`cat $file |grep -B$2 -A$2 $3`
while getopts":w:" opt
do
case $opt in
w) $2=$OPTARG ; pattern=$OPTARG ; file=$OPTARG ;array=($OPTARG)
;;
*)echo " usage: -w <pattern>"
exit 1
;;
esac
done
echo "${#array[@]}"
echo "line: $2, pattern: $pattern, file: $file"
Is there anyway to retrieve the three variables from one flag?