I have a bash code as follows
python "$TM"
The problem is that $TM can be whatever character, including ` characters. When $TM has `abc`, the bash tries to run abc as a command before giving it a parameter to python.
How can I prevent this? How can I pass the $TM literally without any interpretation?
ADDED
I need more explanation.
I'm using TextMate Bundle Editer so that the bash is called with a buffer ($TM_SELECTED_TEXT or $TM_CURRENT_LINE). The buffer is the selection I made in the TextMate editor. The bash code is as follows.
#!/bin/bash
if [ -n "$TM_SELECTED_TEXT" ]; then
TM="$TM_SELECTED_TEXT"
else
if [ -n "$TM_CURRENT_LINE" ]; then
TM="$TM_CURRENT_LINE"
fi
fi
/usr/bin/python /Users/smcho/smcho/works/prgtask/textmate/repeat.py "$TM"
The repeat.py is as follows
import sys
inputString = sys.stdin.read().decode('utf-8')
inputString = inputString.rstrip().lstrip()
content = inputString[0:-2]
mark = inputString[-1]
r_num = len(content)
string = "%s\n%s" % (content开发者_开发问答, mark * r_num)
sys.stdout.write(string)
sys.exit(0)
If the input is "abc:-", it will convert the string to "abc\n---".
The problem is that if the input contains `` character, bash evaluates it before sending it to python code as parameter.
I think you are getting it wrong. Bash didn't "expand" TM
because it contained backticks (that would be a terrible security breach), the variable already contains the output of the command. You should quote the backticks to prevent the process substitution to occur:
$ TM="`ls`"
$ echo $TM
file1 file2
vs:
$ TM="\`ls\`" # or TM='`ls`'
$ echo $TM
`ls`
Why use bash as intermediary in the first place?
#!/usr/bin/env python
import os
tm = os.environ.get('TM_SELECTED_TEXT', "") or \
os.environ.get('TM_CURRENT_LINE', "")
and so on…
Your repeat.py wouldn't do anything with that argument anyway.
Your question is a bit vague but have you tried to see if quoting $TM
to prevent word splitting solves your problem:
python "$TM"
python "$TM"
精彩评论