LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 951|回复: 0

请教两个shell编程题

[复制链接]
发表于 2009-3-15 14:13:29 | 显示全部楼层 |阅读模式
首先 新人报个到!
同学在国外读书,发来两个shell编程题目,挺急的,shell编程我刚接触,向论坛里的高人求助下~ 谢谢

原文:

1.
Write and explain in details a tcsh script which renames all ascii text files in the current directory by adding a number to their names before the “extension” using the following format: <name>_a<number>.<”extension”> .
For example, if the current directory contains three ascii text files (in alphabetical order):
fileA
fileB.txt
fileC.t
your script should rename them in the following way:
fileA_a1
fileB_a2.txt
fileC_a3.t
Please, remember that MS Word changes some characters, like quote marks, automatically and this can make your script inoperable.
You should explain your script in no more than two pages of A4 at 10pt size (i.e., no more than 600 words).
HINTS
To determine the type of a file you can use the UNIX command: file.
There are various ways you can do this, by all means try things out.  One way is to explore the use of :e and :r modifiers, see the csh manual.
        e.g  if you have a variable FILENAME set to hello.txt then
                echo $FILENAME:e will return  txt (i.e the file extension)
        and         echo $FILENAME:r will return hello (i.e the file basename)
You can use this in your logic.
(10 marks)

我的理解:
把当前文件夹下的ascii文件按照如下规则改名,比如当前文件夹下有如下ascii文件(按字母顺序):
fileA
fileB.txt
fileC.t
则你的脚本应将其改名为(按字母顺序):
fileA_a1
fileB_a2.txt
fileC_a3.t
提示:
可以使用file命令你可以获得文件的类型,利用:e 和:r可以分别得到一个文件的扩展名和文件名。

2.
Write a pipeline that updates the content of a file (a file of your choice) by replacing all
matches of the word ‘and’ with the symbol ‘&’ and by replacing the end of each line with
“your name”. If the command completes successfully it should print ‘YES’, otherwise it
should print ‘NO’.
HINTS
& is a special character in UNIX, to use it, you need to escape it with \, i.e preceed it with the  \ character
The “end of line” character in csh shell is $

(10 marks)

我的理解:
写一个管道更新文件的内容,将文件中的'and'替换为'&',将每一行的结尾替换为你的名字(这里暂且用tom代替把),如果命令成功则打印YES,否则打印NO。
提示:
符号&需要用‘\’来转义。
在csh中“the end of line” character (行结束符?) 是$
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表