Reputation: 603
I am struggling with a small bash shell script that should list my csv files and upload them to a external Service using curl. when i echo my command variable out to console and execute it i works fine, but when i execute it within my script it complains about a extra qouta "
my script looks like this
#!/usr/bin/bash
LOGDIR="/var/log/tyk/"
FILE_EXSTENSION_NAME="*.csv"
CURLCMD="curl -k -i -H"
PORT="9992"
ENDPOINT="/endpoint"
URL="https://localhost"
for i in `ls $LOGDIR$FILE_EXSTENSION_NAME`; do
filename=`echo $i | awk -F "/" ' { print $5 }'`
echo $filename
CMD="$CURLCMD \"filename: $filename\" -F \"data=@$LOGDIR$filename\" $URL:$PORT$ENDPOINT"
$CMD
done
When i run it i getfollowing output
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:--0curl: (6) Could not resolve host: 2018-October-18-10.csv"; Unknown error
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (26) couldn't open file "/var/log/tyk/2018-October-18-10.csv""
If i do a echo $CMD in my script i got
curl -k -i -H "filename: 2018-October-18-10.csv" -F "data=@/var/log/tyk/2018-October-18-10.csv" https://localhost:9992/endpoint
and that works ok
I cannot figure out what it is that i am not doing wrong
Upvotes: 3
Views: 7761
Reputation: 27195
First of all, don't store commands in variables. Apart from that, there are two problems:
$()
around $($CMD)
seems suspicous. Not only do you execute the command stored in $CMD
, but you also execute the output of that command!When you enter the command echo "a b"
then bash
will process that command such that echo
is executed with the argument a b
. The output will be a b
.
When you store that command inside a variable and expand that variable, bash
will process the variable's content differently. The command echo
will be executed with the arguments "a
and b"
. The output will be "a b"
.
This is the reason for the error message
curl: (26) couldn't open file "/var/log/tyk/2018-October-18-10.csv""
curl
is trying to open a path with an actual quote inside. Such a path does not exist on your system.
To get around this issue, you could write eval "$cmd"
, then the command would be executed as if you typed it in directly. However, you really shouldn't. I'd rewrite the script instead to not store commands in variables.
$cmd
:cmd='echo something'
$cmd
This would print something
.
However, your script doesn't stop there, because you enclosed $cmd
in $()
. Therefore the output something
gets executed too.
cmd='echo something'
$($cmd)
results in
bash: something: command not found
Upvotes: 6