如何在OSX上使bash脚本可移植(How to make a bash script portable on OSX)

编程入门 行业动态 更新时间:2024-10-27 01:23:27
如何在OSX上使bash脚本可移植(How to make a bash script portable on OSX)

第一次在这里问一个问题。 编程的新手,如果我的术语有点偏离,请不要好。

我在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 exit

First 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

更多推荐

本文发布于:2023-07-19 01:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   如何在   OSX   bash   script

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!