第一次在这里问一个问题。 编程的新手,如果我的术语有点偏离,请不要好。
我在OSX工作
我有一个bash脚本,其中有一个名为pythonb的函数,它执行python文件。
我试图使这个可移植,但除非我在函数调用中放入python脚本的完整路径,否则我无法使其工作。
我认为,因为主bash.sh与python文件在同一目录中,所以我不需要说明python脚本的目录。 我已经尝试在函数中使用python ./foo.py和python foo.py但我仍然得到一个python:无法打开文件'foo.py':[Errno 2]没有这样的文件或目录。 我怎么做这个便携式? 谢谢大家。
#!/bin/bash function dothis { filename=$(date +"%m-%d-%y|||%H%M%S") dothis -x $filename.png curl -T $filename.png -u beast:password ftp } function sayit { say " I wish I knew more about all this" } function pythonb { python /volumes/FUN/sexy/morefun/foo.py } dothis sayit pythonb exitFirst time asking a question here. New to programing so please be nice if my terminology is a bit off.
I am working in OSX
I have a bash script that has a function in it called pythonb that executes a python file.
I am trying to make this portable, but I can not get this to work unless I put the full path of the python script in the function call.
I thought that since the main bash.sh was in the same directory as the python file I would not need to state the directory of the python script. I have tried using python ./foo.py and python foo.py in the function but I still get a python: can't open file 'foo.py': [Errno 2] No such file or directory. How do I made this portable? Thank you all in advance.
#!/bin/bash function dothis { filename=$(date +"%m-%d-%y|||%H%M%S") dothis -x $filename.png curl -T $filename.png -u beast:password ftp } function sayit { say " I wish I knew more about all this" } function pythonb { python /volumes/FUN/sexy/morefun/foo.py } dothis sayit pythonb exit最满意答案
您可以从$ 0学习它 - 而不是硬编码完整路径 - 这是当前脚本的名称:
python "$(dirname "$0")/foo.py"是的,你应该使用所有这些报价
Instead of hardcoding the full path, you can learn it from $0 -- that's the name of the current script:
python "$(dirname "$0")/foo.py"Yes, you should use all those quotes
更多推荐
发布评论