Test your Shell Scripting Skills: – Program 5

scmuser created the topic: Test your Shell Scripting Skills: – Program 5

Write a script that behaves both in interactive and noninteractive mode. When no arguments are supplied, it picks up each C program from the current directory and lists the first 10 lines. it then prompts for deletion of the file. if the user supplies arguments with the script, then it works on those files only.

rajeshkumar replied the topic: Re: Test your Shell Scripting Skills: – Program 5

Following script should work….

#!/bin/sh
counter=0
if [ $# -eq 0 ]
then

for file in `find . -name "*.c"`
do
head -10 $file
counter=`expr $counter + 1`
echo "Do you want to delete this file=(Please type Y or N)"
read vote
case $vote in
Y|y) rm -f $file;;
n|N) exit ;;
*) echo "Invalid response"
esac
done
else

head -10 $1
echo "Do you want to delete this file=(Please type Y or N)"
read vote
case $vote in
Y|y) rm -f $1;;
n|N) exit ;;
*) echo "Invalid response"
esac
fi

echo "Number of files found=$counter"

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

rajeshkumar replied the topic: Re: Test your Shell Scripting Skills: – Program 5

#!/bin/sh
counter=0
if [ $# -eq 0 ]
then

for file in `find . -name "*.c"`
do
head -10 $file
counter=`expr $counter + 1`
echo "Do you want to delete this file=(Please type Y or N)"
read vote
case $vote in
Y|y) rm -f $file;;
n|N) exit ;;
*) echo "Invalid response"
esac
done
else

head -10 $1
echo "Do you want to delete this file=(Please type Y or N)"
read vote
case $vote in
Y|y) rm -f $1;;
n|N) exit ;;
*) echo "Invalid response"
esac
fi

echo "Number of files found=$counter"

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x