unix script to opena valid file

March 15, 2011 at 12:45:28
Specs: unix
currently i am prompting a user for a file to edit in vi
however, it will open anything. I dont want to be able to opena binary file or a
directory. How could i use the command file with grep to accomplish this or i there a better way
ths is what i have

if [ 'grep " $file"' ]


echo "found file"
vi $file
echo "creating a valid .txt file for editing"

vi $file.txt

See More: unix script to opena valid file

Report •

March 15, 2011 at 14:20:25
A way using the korn shell: check out the unix file command which determines the file type. I decided that any output from the file command containing 'ascii' or 'script' is a valid file - might be different in your world:


if [[ -a "$file" ]]
then # file exists
   if  [[ ( -r "$file" ) && ( -w "$file") ]]
   then # read and writable
       file "$file" |egrep "ascii|script"
       if [[ $? -eq 0 ]]
       then # ascii or script
          vi "$file"
          echo "probably binary"
   echo "creating $file text file"
   vi "$file"

Report •
Related Solutions

Ask Question