0

lsof command is used fot the open files in linux.Which command is used for checking if file is not open.I want to use in my script

my condition is

do
        if [[ 'lsof | grep $r_error_file' ]]
        then
                error_text=$error_text$(tail -n +1 $r_error_file | grep 'Error\')
                mv $r_error_file $(dirname ${r_error_file})/BkError/$(filename ${r_error_file})
        fi
done
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
user3559774
  • 115
  • 1
  • 1
  • 3

2 Answers2

1

Use fuser command

fuser $filename
if [ $? ne 0 ]
then
    # file is open, Add your code here
fi
Sanket Parmar
  • 1,477
  • 12
  • 9
0

You need the case where the lsof statement is false. There's a useful list of methods for manipulating truth in if statements here; but to ruin your fun serarching, you're looking for

if [[ ! `lsof | grep $r_error_file` ]]
then
        ...
fi
FarmerGedden
  • 1,162
  • 10
  • 23