初学者 Unix shell 脚本问题 [英] Beginner Unix shell script issues

查看:30
本文介绍了初学者 Unix shell 脚本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一项由两部分组成的作业.首先,我们被要求创建一个名为 fileType.sh 的 shell 脚本,它能够判断文件是Windows ASCII"类型还是其他类型".我已经完成了那部分,并将在下面展示一个它应该做什么的例子.

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

它有效,这里是 fileType.sh 脚本的代码

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

此作业的第二部分希望我们:在同一个目录中,编写一个脚本 checkFiles.sh,该脚本可以从命令行获取多个文件路径并对每个文件路径进行相同的分析,打印一行包含文件名、一个冒号、一个空格,然后是来自第 1 阶段的两个输出字符串之一,对于命令行中的每个文件.

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.

例如

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

这是我编写的 checkFiles.sh 脚本.

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

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

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

done

这是我运行时的输出.

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

我从硬件检查器中得到的错误是:运行时失败:./checkFiles.sh 'aardvark.cpp' 'bongo.dat' 'cat.dog.bak'.我将测试一个 .ccp 和一个 .bak,看看它是否会破坏我的程序或其他什么.有没有其他人有任何想法?我已经为此工作了很长一段时间,任何帮助都会很棒.谢谢!

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

但这不正确!应该说Windows ASCII"而不是别的东西".问题出在我的 checkFiles.sh 脚本中.现在正在改变一些东西,但这就是我目前所处的位置.

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.

推荐答案

您的主力缺少您要分析的文件名.

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

这篇关于初学者 Unix shell 脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆