shell - Check if file exists and whether it contains a specific string -
i want check if file2.sh
exists , if specific word, poet
part of file. use grep
create variable used_var
.
#!/bin/ksh file_name=/home/file2.sh used_var=`grep "poet" $file_name`
how can check if used_var
has value?
instead of storing output of grep in variable , checking whether variable empty, can this:
if grep -q "poet" $file_name echo "poet found in $file_name" fi
============
here commonly used tests:
-d file file exists , directory -e file file exists -f file file exists , regular file -h file file exists , symbolic link (same -l) -r file file exists , readable -s file file exists , has size greater 0 -w file file exists , writable -x file file exists , executable -z string length of string 0
example:
if [ -e "$file_name" ] && [ ! -z "$used_var" ] echo "$file_name exists , $used_var not empty" fi
Comments
Post a Comment