0

Having trouble with this problem, i could use some help ....

create a shell script, fileType.sh, 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.

For example:

% ./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
%

Your script should not produce any other output when given a legal path to an existing file. (It may produce anything you like if given an incorrect path to a non-existent file.)

If the output of the file command contains the phrase “ASCII text” and the phrase “CRLF”, then the file is Windows ASCII text.

New to Unix; I have some C++ experience.

This is what I have in my script.

#!/bin/sh
line='file -b "$1"`

win=o
for i in $line; do
    if test `expr match "$1" "CRFL" | expr match "$1" "ASCII"` -gt 0 ; then
            win =1
            break
    fi
done

if test "$win" -eq 1; then
      printf "Windows ASCII\n"
else
      printf "Something else\n"
fi

I am using tcsh and basically I am trying to search for two varribles at the same time. In C++ I would use the "&&" operator. I don't know if unix has such an operator so I have tried to "pipe" them together.... with no luck.

I am getting "Something else" when I should be getting "Windows ASCII"

3
  • Eerily similar to stackoverflow.com/questions/27459148/… but definitely a better question. Commented Dec 13, 2014 at 20:05
  • 1
    Note that your script is a Bourne-shell style script, not a tcsh script, so your tagging is inaccurate since the problem is not the shell you run your commands from but the shell that runs this script. The use of the | in the if is dubious; test doesn't generate output on standard output and expr doesn't read anything from standard input. You may be thinking of the || operator. Using $1 instead of $i in the tests is also probably a mistake. Consider using the case operator too. Commented Dec 13, 2014 at 20:09
  • The | is part of the command substitution that provides the arguments for test (which would be easier to see if the OP weren't using backquotes rather than $(...)). Commented Dec 14, 2014 at 16:21

1 Answer 1

1

You have some odd quoting and spelling errors in your script. Here is a fixed version.

#!/bin/sh
line=`file -b "$1"`  # backticks fixed

win=o
for i in $line; do  # fixed CRLF pro CRFL; fix bogus expr | expr
    if test `expr match "$1" "CRLF"` -gt 0 || test `expr match "$1" "ASCII"` -gt 0 ; then
            win=1   # cannot have space before equals sign
            break
    fi
done

if test "$win" -eq 1; then
      printf "Windows ASCII\n"
else
      printf "Something else\n"
fi

An immediate improvement would be to avoid expr, and to use the modern syntax for process substitution -- $(file ...) instead of `file ...` as well as fold the multiple conditionals into a single case statement. This also avoids the loop over the tokens in line which seems rather unidiomatic and wasteful.

#!/bin/sh
case $(file -b "$1") in
    # I guess the message has ASCII before CRLF always, but cover all bases
    *CRLF*ASCII* | *ASCII*CRLF*)
        echo "Windows ASCII" ;;
    *)
        echo "Something else" ;;
esac
Sign up to request clarification or add additional context in comments.

2 Comments

both of those solutions appear to work but when I submit it, the auto checker says it fails to read a .cpp file ... Failed when running: ./fileType.sh 'aardavark.cpp'
Without access to this test file, it's hard to tell what could cause it. Do you have any additional information?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.