【发布时间】:2021-04-29 12:58:18
【问题描述】:
我正在使用 Linux 终端应用程序,它可以完美运行,但我认为我在这里遇到了问题,我不知道它是应用程序还是我的错误
下面是我的 test.sh,#comment 中的所有内容都是我在调用/运行每一行后得到的,但请注意,我猜这通常是 2 个文件,只是将它们放在一起
#!/bin/bash
echo "this is a test"
# this is a test
ls
# code downloads
pwd
# /data/data/com.termux/files/home
ls code
# python
ls code/python
# scripts programs
ls code/python/scripts
# abc.py test.py
cd code/python/scripts
pwd
#/data/data/com.termux/files/home/code/python/scrips
ls
# abc.py test.py
python3 test.py
# hello world
cd
python3 code/python/scrips/abc.py
# python3: can't open file '/data/data/com.termux/files/home/code/python/scrips/abc.py': [Errno 2] No such file or directory
python3 code/python/scrips/test.py
# python3: can't open file '/data/data/com.termux/files/home/code/python/scrips/test.py': [Errno 2] No such file or directory
需要注意的是,如果我运行
python3 code/python/scrips/abc.py 在home 中,但它可以工作。这就是我自己测试和尝试的所有东西,你在上面看到的只是我“调试”
我以前在这里看到过 shell 编程问题,所以我认为这是一个可以的问题。
【问题讨论】:
-
cd命令不带任何参数,将当前目录更改为您的主目录。 -
在最后一个
cd之后尝试pwd和echo $HOME。它们应该是一样的。 -
删除最后一个
cd(单独的那个),你的脚本就可以正常运行了。 -
@accdias - 不,
python3 code/python/scrips/abc.py如果不移回原始目录将无法工作。/data/data/com.termux/files/home更有可能不是主目录。 -
在 bash 脚本中包含
env | grep PATH并查看是否包含python3所在的路径。您实际上是交互式地运行此脚本还是从 cron 之类的东西运行它?如果您不以交互方式运行它,则会丢失所有路径信息,并且必须在 bash 脚本中定义路径。
标签: python linux shell terminal