0

Inside the script there is one error i am getting as syntax error operand expected (error token is "-") at line 109

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

In the above live getting error as syntax error operand expected (error token is "-").

1
  • 1
    You'll get a possibly clearer error message if you correctly quote the expansion of $file. Also, you should use $(...), not backticks, for command substitutions. Commented Dec 22, 2022 at 14:28

2 Answers 2

1

You are trying to call stat when file doesn't have a value:

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

If you correctly quote $file, you'll get a slightly better error message:

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

If you aren't positive about the contents of your input file, try verifying that $file actually contains an existing file before calling stat:

while IFS= read -r file
do 
    [ -e "$file" ] || continue

    upd_time=$(( $(date +%s) - $(stat -c %Y "$file") ))
    file_nm=$(basename "$file")
    ...
done
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @chepner one of the file was missing
but @chepner in script echo $file & file both were missing so root cause would be both or just only file missing. I just want to confirm
Both would be separate errors resulting from attempting to use an empty string where a non-empty string was expected. That's why I check once, not just to see if file is non-empty, but to see if it actually refers to a file that you can stat. (basename will work on a path whether the name exists or not, but supposedly future uses of file_nm would require the file to exist.)
any idea on below question stackoverflow.com/questions/75007309/…
0

I have reproduced your error:

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

output:

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

You have a blank line in your input file.

5 Comments

Thanks for your reply, means if i remove extra blank line then my issue get resolved ?? Actually in main script file there is no any extra blank line but i haven't use echo $upd_time in shell script.
Not your script file, but the input file to the loop.
I.e., $file is expanding to an empty string.
@chepner $file is a csv file which is in another file directory.
You may be expecting it to be the name of a CSV file, but it's not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.