I would like to extract the IP address that appears after --host
param in the cron job -
34 15 * * * (cd /to/the/directory; ./my_program.pl --param1 --host 198.181.111.222 --server 198.181.333.444 --version 1.0 --port 80 --user uid >> collect.log 2>&1)
Thing is the parameters can appear in random order in the cron. I tried using cut
but that works only when the params are strictly in order.
I tried following some suggestions on SO -
str=$(crontab -l)
echo "${str}"
awk -F'--' '{ for(i=1;i<=NF;i++) print $i }' <<< $str
But this splits str
by --
and prints all token on separate lines.
How can we store 198.181.111.222
in a variable $ip
?