今天写个脚本,发现一个诡异的问题,今天写个脚本,发现一个诡异的问题,主要是目录或文件中有空格导致脚本报错,具体案例如下:
错误示范1:
1 2 3 4 5 6 7 8 9 |
#!/bin/bash file="/Users/$(whoami)/Library/Application\ Support/Beyond\ Compare/registry.dat" #if [ -e $file ] if [ -e "$file" ] then echo "文件存在"; else echo "文件不存在"; fi |
错误示范2:
1 2 3 4 5 6 7 8 9 |
#!/bin/bash file=/Users/$(whoami)/Library/Application\ Support/Beyond\ Compare/registry.dat #if [ -e $file ] if [ -e "$file" ] then echo "文件存在"; else echo "文件不存在"; fi |
正确示范:
1 2 3 4 5 6 7 |
#!/bin/bash if [ -e /Users/$(whoami)/Library/Application\ Support/Beyond\ Compare/registry.dat ] then echo "文件存在"; else echo "文件不存在"; fi |
具体原因,暂时不得而知,先放着吧,权当mark。