Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi!
I have two questions. It's already 1.15 AM :( can't solve it. May be somebody will help?1)
Write a sript caled smartrm which will accept an unlimited number pf filename extensions and will remove all file in the curent dir with thouse extentsions. For example:
smartrm o c i
will remove all files in the curent dir with .o .c .i extentions. If no arguments provided print error.
I have this, but it only removes files with first extention
*************
#!/bin/kshinteger cn[$#]
integer iif(($# == 0))
then
print "Error"
exit 1
fi
IFS='.'
for number in $*
do
rm *.$*"done
*******
2)Second problem is harder:
write a script called backup that will backup a dir containing C files. This script will accept the name of dir. If the dir already exitst (with _bak extention) print error. Use the curent dir as a defaut dir. Given the name of a dir the script will crate a new dir with '_bak' appendet to the name of the dir.
Thanx in advance,
oXide

Problem 1 solution:
#! /bin/ksh
if [[ $# -eq 0 ]] ; then
print "Error"
exit 1
fifor type in "$@" ; do
rm *.$type
done-jim

Thanks Jim!!
It realy works :)
May be you could help me to solve the second problem?
That would be very nice of you.
oXide

Problem 2 solution (untested)
#! /bin/ksh
# Test for an argument
if [[ $# == 0 ]] ; then
print "Argument needed: directory name"
exit 1
fi# Strip the ending "/" on the directory
case $1 in
*/ ) DIR=${1%*/} ;;
* ) DIR=$1 ;;
esac# Test that entered directory exists
if [[ ! -d "$DIR" ]] ; then
print "Directory does not exist."
exit 1
fi# Check for a backup of the entered directory
if [[ -d "${DIR}_bak" ]] ; then
print "Backup already exists."
fi# Make the backup directory and copy files to it
mkdir ${DIR}_bak
cp ${DIR}/*.c ${DIR}_bak-jim

Forgot the "exit 1" after the Check for a backup of the entered directory:
if [[ -d "${DIR}_bak" ]] ; then
print "Backup already exists."
exit 1
fi-jim

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |