【发布时间】:2020-01-13 15:13:13
【问题描述】:
假设这是 test.sh
#!/bin/bash
if [ -f "file.sh" ]; then
echo "File found!" # it will hit this line
else
echo "File not found!"
fi
if [ -f "${0%/*}/file.sh" ]; then
echo "File found!"
else
echo "File not found!" # it will hit this line
fi
并且 file.sh 存在于 test.sh 旁边的同一文件夹中 输出将是
+ '[' -f file.sh ']'
+ echo 'File found!'
File found!
+ '[' -f test.sh/file.sh ']'
+ echo 'File not found!'
File not found!
我缺少一些设置吗?
【问题讨论】:
-
你确定
folder存在吗? -
你在哪里运行它?在交互式 shell 中,
$0是-bash。尝试使用set -x来查看您的命令扩展为什么。 -
您使用它的上下文是什么?它是交互式的,还是在脚本中,如果它在脚本中,你是如何运行它的?你想通过使用
${0%/*}来完成什么? -
文件夹确实存在
-
好吧,
echo "${0%/*}/../folder"打印什么?还可以使用 ex 调试您的脚本。set -x并查看(并发布)输出。