Beginner Unix shell script issues

2019-07-21 02:33发布

I'm working on an assignment that is a two part assignment. First we were asked to create a shell script called fileType.sh that is able to tell if a file is "Windows ASCII" type or "something else." I have completed that part and will display an example of what it's supposed to do below.

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

It works and here is the code for the fileType.sh script

#!/bin/sh                                                                       
file="$1"
case $(file "$file") in
    *"ASCII text, with CRLF line terminators" )
        echo "Windows ASCII"
    ;;
    * )
        echo "Something else"
    ;;
esac

Part two of this assignment wants us to: In that same directory, write a script checkFiles.sh that can take multiple file paths from the command line and carries out the same analysis on each one, printing a line containing the file name, a colon, a blank, and then one of the two output strings from Stage 1, for each file in the command line.

For example,

./checkFiles.sh /usr/share/dict/words fileType.sh /home/cs252/Assignments/ftpAsst/d3.dat
/usr/share/dict/words: Something else
fileType.sh: Something else
/home/cs252/Assignments/ftpAsst/d3.dat: Windows ASCII 

Here's the checkFiles.sh script that I have written.

#!/bin/sh                                                                     
for i  in "$@"; do

    echo $i":" "`sh ./fileType.sh`" 

done

and here's my output when I run it.

./checkFiles.sh /usr/share/dict/words fileType.sh /home/cs252/Assignments/ftpAsst/d3.dat
/usr/share/dict/words: Something else
fileType.sh: Something else
/home/cs252/Assignments/ftpAsst/d3.dat: Something else

The error I get from my hw checker is: Failed when running: ./checkFiles.sh 'aardvark.cpp' 'bongo.dat' 'cat.dog.bak'. I'm going to test a .ccp and a .bak to see if it blows up my program or something. Does anyone else have any ideas? I have been working on this for quite sometime, any help would be great. Thanks!

Update: Ok, so after rerunning this command I get:

./checkFiles.sh /home/cs252/Assignments/ftpAsst/d3.dat
/home/cs252/Assignments/ftpAsst/d3.dat: Something else

But that's not correct! instead of saying "Something else" it should say "Windows ASCII." The problem is somewhere in my checkFiles.sh script. Changing somethings around now, but this is where I'm at so far.

标签: shell unix
1条回答
我只想做你的唯一
2楼-- · 2019-07-21 03:00

Your workhorse is missing the file name that you want to analyze.

#!/bin/sh
for i  in "$@"; do
    echo $i":" "$(sh ./fileType.sh $i)"
done

HTH

查看更多
登录 后发表回答