Shell script to find file type

  1. The problem statement, all variables and given/known data:
    Write a shell script that takes a single command line parameter, a file path (might be relative or absolute). The script should examine that file and print a single line consisting of the phrase:
    Windows ASCII
    if the files is an ASCII text file with CR/LF line terminators, or
    Something else
    if the file is binary or ASCII with �Unix� LF line terminators.

  2. Relevant commands, code, scripts, algorithms:

Output should look like this:
./fileType.sh ~cs252/Assignments/ftpAsst/d3.dat
Windows ASCII
./fileType.sh /bin/cat
Something else
./fileType.sh fileType.sh
Something else
./fileType.sh /usr/share/dict/words
Something else

  1. The attempts at a solution (include all code and scripts):
    The first attempt I tried worked on files, but when I ran it on folders it outputted Windows ASCII. The second attempt I tried works, but only outputs Windows ASCII.
#!/bin/sh 
file=$1 
if grep -q "\r\n" $file;then 
echo Windows ASCII 
else 
echo Something else 
fi

and I have tried this:

#!/bin/sh
if test -f $file;
then
echo "Windows ASCII"
else
echo "Something else"
fi

Third attempt.

#!/bin/sh
file=$1
case $(file $file) in
*"ASCII test, with CRLF lin terminators")
echo "Windows ASCII"
;;
*)
echo "Something else"
;;
esac
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Old Dominion University, Norfolk VA USA, Professor Steven Zeil, CS 252

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

How about combining your two first attempts?
And, your third attempt should work, too, if the pattern were formulated carefully.

grep works on LF-separated lines, and the LF character is not part of its line buffer.
So you cannot find \n. Further \n and \r do not have a special meaning in grep (it searches for n and r as if they were unquoted).

In recent shells, try using

grep $'\r'"$" file

, then, combining a shell representation of <CR> and grep 's EOL indicator.

It would help if I could spell wouldn't it...:o Once I fixed the spelling errors it worked.