I would like to log various things when calling functions in a bash script and so I am trying to have a special place to do this
function CallAndLog {
echo "$@" > /tmp/debug
res=$("$@" 2> /tmp/error)
if [ $? -gt 0 ]
then
echo "error : $(</tmp/error)" >> /tmp/log
fi
echo "$res" >> /tmp/log
}
val="a test"
CallAndLog curl --data '"'"query=$val"'"' http://google.com
I get this
- /tmp/debug
curl --data "query=a test" http://google.com
- /tmp/error
curl: (6) Couldn't resolve host 'test"' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 967 100 959 100 8 1060 8 0:00:01 --:--:-- 0:00:01 1572
- /tmp/log
<!DOCTYPE html>...
/tmp/debug is perfect, this is exactly what I want to execute, and if I copy and paste that it works. However the error suggests that "a" and "test" were split into 2 and curl tried "test" as the url. This is what I don't understand
I have tried LOTS of different things : $, $@, "$", "*@", various ways to put the single and double quotes, so after hours of struggling I call for your help !!! Many Thanks