避免:bash 中意外标记附近的语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35533473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoiding: syntax error near unexpected token in bash
提问by Perplexing Pies
I have a shell that calls a python script like so...
我有一个像这样调用python脚本的shell......
Shell code:
外壳代码:
#!/bin/sh
python PyTest.py
print("FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM")
5 + 4
print("No semicolons here!")
Python code:
蟒蛇代码:
#This is my test Python script running from bash
print("FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM")
print(5 + 4)
print("No semicolons here!")
And here is the output.
这是输出。
74-85-81-234:Desktop Cody$ ./PyTest.sh
FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM
9
No semicolons here!
./PyTest.sh: line 5: syntax error near unexpected token `"FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM"'
./PyTest.sh: line 5: `print("FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM")'
I've looked up the "syntax error near unexpected token" error and it looks like this is expected. Is there a better way to do this, and is there a way to call the function in this manner without the Bash errors?
我查找了“意外标记附近的语法错误”错误,看起来这是意料之中的。有没有更好的方法来做到这一点,有没有办法以这种方式调用函数而不会出现 Bash 错误?
Thanks in advance.
提前致谢。
回答by cdarke
If you want to run a bash
script, then specify bash
at the top of your file, not sh
:
如果要运行bash
脚本,请bash
在文件顶部指定,而不是sh
:
#!/bin/bash
To run a python script from bash then all you have to do is:
要从 bash 运行 python 脚本,那么您所要做的就是:
python PyTest.py
If you want to embedpython in a script then you can use a here document:
如果你想在脚本中嵌入python,那么你可以使用这里的文档:
python << END
print("FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM")
print(5 + 4)
print("No semicolons here!")
END
Or write it in bash:
或者用bash写:
#!/bin/bash
echo "FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM"
echo $((5 + 4))
echo "No semicolons here either!"
But why? Bash can't do anything that python can't do, yet python can do an awful lot more than bash. Write everything in python! Just put something like #!/usr/bin/env python
at the top of your python script.
但为什么?Bash 不能做 python 不能做的任何事情,但是 python 可以做的比 bash 多得多。用python写一切!只需将类似 #!/usr/bin/env python
的内容放在 python 脚本的顶部即可。
回答by jlliagre
You can use one of:
您可以使用以下之一:
#!/usr/bin/env python
print("FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM")
5 + 4
print("No semicolons here!")
or
或者
#!/bin/sh
python <<%
print("FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM")
5 + 4
print("No semicolons here!")
%
Note that 5 + 4
won't produce any output in either cases as this is not an interactive python session:
请注意,5 + 4
在任何一种情况下都不会产生任何输出,因为这不是交互式 python 会话:
FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM
No semicolons here!
Using the -i
option will make it work:
使用该-i
选项将使其工作:
#!/bin/sh
python -i <<%
print("FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM")
5 + 4
print("No semicolons here!")
%
output:
输出:
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> FE FI FO FUM I'M GOING TO MAKE IT TUMBLE TUM TUM
>>> 9
>>> No semicolons here!
>>>