Difficulty with Bourne shell script...

December 17, 2011 at 20:26:35
Specs: Linux - Ubuntu 10.04 LTS

How do I write a Bourne shell script that takes a directory as an argument and removes all the ordinary files under it that have .o,.ps, and .jpg extensions. If no argument is specified, the current directory is used.


See More: Difficulty with Bourne shell script...

Report •

#1
December 17, 2011 at 23:38:07
No script necessary; just use "rm".

Report •

#2
December 18, 2011 at 00:01:23
I will do the hard part for you. This find command deletes all files with "o", "ps" or "jpg" extension from the current directory:

#!/bin/sh

find . -type f \( -name "*.o" -o -name "*.ps" -o -name "*.jpg" \) -print|xargs rm

All you have to do is replace the period above with the argument passed.


Report •

#3
December 18, 2011 at 00:17:42
I'm probably missing something, but wouldn't

rm *.o *.ps *.jpg

be a more elegant and efficient way to do the same thing?


Report •

Related Solutions

#4
December 18, 2011 at 10:59:38
Here's what I've come up with so far, but I still can't get it to work. Damn.

#!/Bin/sh
if [ $ # = 0 ]
then
directory = "."
elif [ $ # = 1 ]
then
directory = "$1"
else
echo "Usage: $ 0 [ directory ]"
exit 1
fi
if [ -d "$ directory" ]
then
rm-i "$ directory" / *. o
rm-i "$ directory" / *. ps
rm-i "$ directory" / *. jpg
exit O
else
echo "Usage: $ O [ directory ]"
exit 1
fi
$


Report •

#5
December 18, 2011 at 12:26:53
The last two "O's" should be zeros. Sorry about that.

Report •

#6
December 18, 2011 at 14:04:33
When you accessing shell variables, it is:

$directory # no spaces

not:

$ directory

And no spaces in the rm command:

rm-i "$directory"/ *. o


Report •

Ask Question