检查具有完整路径名的文件是否存在时的二元运算符预期错误 [英] binary operator expected error when checking if a file with full pathname exists
问题描述
pathname=$(cat $HOME/.rm.cfg)
if [ ! -z $pathname/$1 ]
.rm.cfg
是一个包含以下目录的文件
.rm.cfg
is a file that contains the following directory
/home/用户名/deleted1
/home/username/deleted1
$1
是文件名,例如.玻璃
$1
is the name of a file eg. glass
如果 [ !-z $pathname/$1 ]
给出二元运算符预期错误.
why does the line if [ ! -z $pathname/$1 ]
give a binary operator expected error.
推荐答案
看起来您的 $pathname 包含不止一个词..rm.cfg 文件中可能有多行,或者路径可能包含空格.无论如何,你最终得到
Looks like your $pathname includes more than one word. Could be multiple lines in your .rm.cfg file, or perhaps the path includes spaces. Anyway, you end up with
if [ ! -z word word word/$1 ]
这不好.如果您只需要一条路径并希望防止路径包含空格,请将 if 行更改为
which is no good. If you're just expecting a single path and want to protect against the path containing whitespace, change your if line to
if [ ! -z "$pathname/$1" ]
这篇关于检查具有完整路径名的文件是否存在时的二元运算符预期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!