Working on a shell script where in the Query_String I need to separate one of the strings into two. For an example, my string has router=bras1+dallastx and I need to separate bras1 and dallastx into different variables. The DOMAIN variable works but I can not get the CISCO variable going. Any suggestions?
Here is my url that feeds the script:
http://mywebpage/syslog.cgi?router=router1+dallastx&month=01&day=01&string=security
NAME=`echo "$QUERY_STRING" | sed -n 's/^.*router=\([^&]*\).*$/\1/p'`
for opt in $NAME; do
CISCO= `echo $NAME | sed 's/+/ /g' | awk '{print $1}'`
DOMAIN=`echo $NAME | sed 's/+/ /g' | awk '{print $2}'`
done
How to set variables using QUERY_STRING ?
When you are using eval ex. in cgi scripts, then you must check the string before using eval. ";&<>|\ ... and variable name is not ex. PATH and so on ...
Eval is nice, but eval is also dangerous, if you not check string before eval.
This ex. not include testing.
#!/someposixsh
QUERY_STRING="router=router1+dallastx&month=01&day=01&string=security"
# parse values to the array using delimiter &
IFS="&" values=( $QUERY_STRING )
for paramstr in ${values[@]}
do
# add " " around the value
paramstr="${paramstr/=/=\"}\""
echo "$paramstr"
# set variable
eval $paramstr
done
echo "router:$router"
echo "month:$month"
Sorry, I don't mean to be harping on PHP too much these days, but FYI, if you wrote your script in PHP, you then get all the web query variables "for free" in the $_GET[] superglobal variable available to all PHP scripts.
$router=$_GET['router'];
$month=$_GET['month'];
etc.
Then, you simply explode your $router string with explode()