Write a shell script to remove

sh/bash/dash/ksh/zsh等Shell脚本
回复
SimonCCC
帖子: 2
注册时间: 2011-04-08 22:30

Write a shell script to remove

#1

帖子 SimonCCC » 2011-04-08 22:40

Task
Write a shell script (to run on the Bourne shell) that can be used to remove
some old C programs you no longer wish to keep. When the script is run with
no arguments supplied, it picks up each C program from the current directory
and lists the first 10 lines (hint: research the head command). It then prompts
for deletion of the file (the user can then choose to delete or not to delete the
file). When the user supplies arguments (C program file names) with the
script, it works on those files only.
Your script for this task must be named delC.sh. In designing your script
you should consider the following scenarios:
• There is no C program file stored under the current directory;
• There is at least one C program file stored under the current directory;
• The user-supplied arguments list contains names of existing C
programs stored under the current directory. We can assume that all
the C programs have been correctly named as *.c, and that there is no
special character (such as space) in any of these filenames;
• The user-supplied arguments list contains both existing and missing
names of C programs stored under the current directory. Missing
names refer to the files which no longer exist under the current
directory.
Make sure your script is user-friendly and follows common sense (for
example, when there is no C program stored under the current directory your
script should display a message and then exit). The following is a sample
output of the script. The $ is the shell prompt.
$ delC.sh
This script removes C files which you no longer want to keep.
Here are the C file(s) under the current directory:
No C files found.
$ delC.sh
This script removes C files which you no longer want to keep.
Here are the C file(s) under the current directory:
f1.c f2.c
Displaying first 10 lines of f1.c:
/* A C program for handing arrays
Written by John Jones, July 2009
Updated April 2010
*/
#include <stdio.h>
int main()
{
int x;
Delete file f1.c?(y/n):n
File f1.c NOT deleted.
Displaying first 10 lines of f2.c:
/* Another C program for handing arrays
Written by John Jones, August 2009
Updated May 2010
*/
#include <stdio.h>
int main()
{
int x, y, z;
Delete file f2.c? (y/n): y
File f2.c deleted.
$ delC.sh f1.c
This script removes C files which you no longer want to keep.
The file(s) you want to delete is/are:
f1.c
Displaying first 10 lines of f1.c:
/* A C program for handing arrays
Written by John Jones, July 2009
Updated April 2010
*/
#include <stdio.h>
int main()
{
int x;
Delete file f1.c? (y/n):y
File f1.c deleted.
$ delC.sh f2.c f3.c
This script removes C files which you no longer want to keep.
The file(s) you want to delete is/are:
f2.c f3.c
Displaying first 10 lines of f2.c:
/* Another C program for handing arrays
Written by John Jones, August 2009
Updated May 2010
*/
#include <stdio.h>
int main()
{
int x, y, z;
Delete file f2.c? (y/n): y
File f2.c deleted.
Displaying first 10 lines of f3.c:
File f3.c does not exist.
$ delC.sh f3.c f4.c
This script removes C files which you no longer want to keep.
The file(s) you want to delete is/are:
f3.c f4.c
Displaying first 10 lines of f3.c:
File f3.c does not exist.
Displaying first 10 lines of f4.c:
File f4.c does not exist.
$
头像
tenzu
论坛版主
帖子: 36924
注册时间: 2008-11-21 20:26

Re: Write a shell script to remove

#2

帖子 tenzu » 2011-04-08 22:45

bot?
SimonCCC
帖子: 2
注册时间: 2011-04-08 22:30

Re: Write a shell script to remove

#3

帖子 SimonCCC » 2011-04-08 23:09

tenzu 写了:bot?
写个脚本文件 删除里面的C文件
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: Write a shell script to remove

#4

帖子 eexpress » 2011-04-09 1:36

#include <stdio.h>
int main()
{
int x, y, z;
Delete file f2.c? (y/n): y
File f2.c deleted.

这啥。
● 鸣学
头像
goldfox_79
帖子: 2004
注册时间: 2005-10-16 8:26
来自: 地球

Re: Write a shell script to remove

#5

帖子 goldfox_79 » 2011-04-09 13:59

#include <stdio.h>
int main()
{
int x, y, z;
显示f2.c的前三行代码
Delete file f2.c? (y/n): y
询问用户如何处理这个文件
File f2.c deleted.
打印处理结果
Joevy
帖子: 1
注册时间: 2011-04-08 0:51

Re: Write a shell script to remove

#6

帖子 Joevy » 2011-04-10 13:59

本人新手,写了个delC.sh,运行貌似没有问题,有些边缘问题没去解决,代码如下,如有错误,请大神纠正:

#!/bin/bash
if [ $# -eq 0 ];then
n=0
declare -a s
for file in `ls`
do
a=`printf $file | sed 's/.*\(\.c$\)/\1/'`
if [ $a = ".c" ];then
s[$n]=$file
n=`expr $n + 1`
fi
done
if [ $n -gt 0 ];then
echo "Here are the C file(s) under the current directory:"
echo ${s[*]}
i=0
while [ $i -lt $n ]
do
echo "Displaying first 10 lines of ${s[$i]}"
head -n 10 ${s[$i]}
printf "Delete file ${s[$i]}? (y/n):"
read yon
case $yon in
y)
rm ${s[$i]}
echo "File ${s[$i]} deleted."
;;
n)
echo "File ${s[$i]} NOT deleted."
;;
*)
echo "Illegal option.File ${s[$i]} NOT deleted."
;;
esac
i=`expr $i + 1`
done
else
echo "No C file under the current directory."
fi
else
declare -a s=($@)
echo "The file(s) you want to delete is/are:"
echo ${s[*]}
i=0
while [ $i -lt $# ]
do
if [ ! -f ${s[$i]} ];then
echo "File ${s[$i]} does not exist."
else
echo "Displaying first 10 lines of ${s[$i]}"
head -n 10 ${s[$i]}
printf "Delete file ${s[$i]}? (y/n):"
read yon
case $yon in
y)
rm ${s[$i]}
echo "File ${s[$i]} deleted."
;;
n)
echo "File ${s[$i]} NOT deleted."
;;
*)
echo "Illegal option.File ${s[$i]} NOT deleted."
;;
esac
fi
i=`expr $i + 1`
done
fi
fnan
帖子: 919
注册时间: 2009-07-01 22:04

Re: Write a shell script to remove

#7

帖子 fnan » 2011-04-10 19:17

就是以.c结尾的文件询问要不要删除?
for i in *.c; do head -10 $i; rm -i $i; done
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
回复