我对使用单括号或双括号感到困惑。看看这段代码:
dir="/home/mazimi/VirtualBox VMs"
if [[ -d ${dir} ]]; then
echo "yep"
fi
Run Code Online (Sandbox Code Playgroud)
尽管字符串包含空格,但它工作得很好。但是当我将其更改为单括号时:
dir="/home/mazimi/VirtualBox VMs"
if [ -d ${dir} ]; then
echo "yep"
fi
Run Code Online (Sandbox Code Playgroud)
它说:
./script.sh: line 5: [: /home/mazimi/VirtualBox: binary operator expected
Run Code Online (Sandbox Code Playgroud)
当我将其更改为:
dir="/home/mazimi/VirtualBox VMs"
if [ -d "${dir}" ]; then
echo "yep"
fi
Run Code Online (Sandbox Code Playgroud)
它工作正常。有人可以解释发生了什么吗?什么时候应该在变量周围分配双引号,"${var}"以防止空格引起的问题?
我想在执行任务后测试 cron 的邮件内容和类似的工作。有没有更优雅的方法来测试这个,然后设置一个 cron 作业每隔几秒钟运行一次?那么,有什么方法可以模拟 cron 作业的执行/手动启动 cron 作业,但其行为与 cron 自动运行时的行为相同?
我有以下脚本:
#!/bin/bash
if test -f "/path/pycharm.sh"; then sh ./pycharm.sh;
fi
Run Code Online (Sandbox Code Playgroud)
我正在尝试从 bash 文件运行pycharm.sh,并且我仔细查看了该文件所需的所有权限。不幸的是,每次我运行它时,我都会得到这个:
无法打开 ./pycharm.sh