Reputation: 3
I am trying to work out how i can have multiple functions in one script and choose the function with an argument. what seems to be the issue is that if i choose a function the optarg does'nt seem to be run with the script. in this example I would run the script as such ~# ./script.sh -a -c wordlist.txt that to only run the first function with the wordlist of choice same as ~# ./script.sh -b -c wordlist.txt
#!/bin/bash
one()
{
for i in $(cat $wordlist); do
wget http://10.10.10.10/$i
}
two()
{
for i in (cat $wordlist); do
curl http://10.10.10.10/$i
}
while getopts "abc:" option; do
case "${option}" in
c) wordlist=${OPTARG} ;;
a) one;;
b) two;;
esac
done
Upvotes: 0
Views: 649
Reputation: 530960
When parsing command-line arguments, don't try to act on them immediately. Simply remember what you have seen. After you have parsed all the options, you can take action based on what you have learned.
Note that one
and two
can be replaced by a single function parameterized by the program (wget
or curl
) to run; while you are at it, pass the word list as an argument as well.
get_it () {
# $1 - program to run to fetch a URL
# $2 - list of words to build URLs
while IFS= read -r line; do
"$1" http://10.10.10.10/"$line"
done < "$2"
}
while getopts "abc:" option; do
case "${option}" in
c) wordlist=${OPTARG} ;;
a) getter=wget;;
b) getter=curl;;
esac
done
get_it "$getter" "$wordlist"
Upvotes: 5