1

I have a shell script I am trying to get working. I need the output to be a certain way and I know I'm just not escaping the characters correctly.

script:

#!/bin/bash
set -x
DATE=$(date +%Y-%m-%d-%M)
ELEMENTS="ele1,ele2,ele3"
TOPIC="dogs cats"
FILE="./$DATE.csv"
COMMAND="python /home/script.py"
$COMMAND $ELEMENTS "$TOPIC" | tee -a $FILE

What happens from set in the terminal when I run it is:

++ date +%Y-%m-%d-%M
+ DATE=2016-02-01-21
+ ELEMENTS=a list of elements
+ TOPIC='dogs cats'
+ FILE=./2016-02-01-21.csv
+ COMMAND='python /home/script.py'
+ python /home/script.py ele1,ele2,ele3 'dogs cats'
+ tee -a ./2016-02-01-21.csv

What I need it to be is "dogs cats" vs 'dogs cats'. The python command requires double quotes for multi word strings.

This is wheezy running on a raspberry pi.

  • 1
    I think dogs cats is correctly passed to the script as a multi word string. Single quotes shown by set -x are always added to arguments with spaces just for clarification, they're not actually passed to the script. How do you treat command line args in script.py? – yaegashi Feb 1 '16 at 2:51
  • 1
    the py script being called is not my own, but when given multiple strings without quotes, it treats each one as an or, with quotes it treats them as and. – RoninUTA Feb 1 '16 at 3:04
0

Change

TOPIC="dogs cats"

to

TOPIC='"dogs cats"'

to have " embedded inside the variable.

  • that yields:+ python /home/pi/script.py ele1,ele2,ele3 '"dogs cats"' + tee -a ./2016-02-01-57.csv – RoninUTA Feb 1 '16 at 2:58
  • Isn't that what you want, given your comment above? – glenn jackman Feb 1 '16 at 3:32
  • @RoninUTA You are confusing what you "see" from set -x as what "it is". The ' are used to express the concept of "exactly this string", not that those single quotes are sent to the executed command. – user79743 Feb 1 '16 at 16:09
  • You are absolutely correct! Thank you everyone !!!!! – RoninUTA Feb 2 '16 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.