语法错误操作数预期(错误标记"-" )



在脚本中有一个错误,我得到的syntax error operand expected (error token is "-")在第109行

#!/bin/ksh
..............
while read file
do 
upd_time=$((`date +%s`-`stat -c %Y ${file}`))     #At this line getting error
file_nm=`basename "${file}"`
..................

上面的live get error为syntax error operand expected (error token is "-")

file没有值时,您正在尝试调用stat:

$ unset file
$ stat -c %Y $file
stat: missing operand
Try 'stat --help' for more information.

如果你正确地引用$file,你会得到一个稍微好一点的错误信息:

$ stat -c %Y "$file"
stat: cannot stat '': No such file or directory

如果您对输入文件的内容不确定,请在调用stat:

之前尝试验证$file实际上包含一个现有文件。
while IFS= read -r file
do 
[ -e "$file" ] || continue
upd_time=$(( $(date +%s) - $(stat -c %Y "$file") ))
file_nm=$(basename "$file")
...
done

我已经复制了您的错误:

tmp.txt:

/bin/sh
/bin/bash

test.sh:

while read file; do
upd_time=$((`date +%s`-`stat -c %Y ${file}`))
echo $upd_time
done < tmp.txt

输出:

44421445
stat: missing operand
Try 'stat --help' for more information.
-bash: 1671714632-: syntax error: operand expected (error token is "-")

你的输入文件中有一个空白行

最新更新