0

I have to write a script that can do this:

For each argument:

  1. If the argument is the name of an empty file (has 0 bytes in it), the script removes the file.

  2. If the argument is not a name of an empty file, the script should ignore that argument and process any remaining arguments.

  3. If there are no arguments, the script should print out a warning message to the screen and exit.

But I receive this error

script8.sh: line 9: [: missing `]'

#!/bin/bash

if [ ! -s $1 ]
then
rm -rf $1
elif [ -f -s $1 ]
then
continue
else [ "$1" -eq 0 ] then  #this is line 9
printf "Null"
exit
fi
cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31
  • 3
    http://shellcheck.net/ will automatically detect the issue at hand and suggest a fix (also to some other bugs -- this code won't work correctly with filenames with spaces, for example). – Charles Duffy Dec 24 '18 at 17:16
  • Note that `[ "$1" -eq 0 ]` is comparing *the filename itself* to the number `0`, not checking *the number of bytes in the file*. If you want to detect an empty file, you could do that with `if [ -f "$1" ] && [ ! -s "$1" ]; then echo "File $1 exists but is empty"; fi` – Charles Duffy Dec 24 '18 at 18:08

0 Answers0