C shell script to implement utility junk

October 2, 2010 at 13:01:22
Specs: Windows XP
Hello

I am very new to Unix environment.I am trying to write a C shell script to implement a utility called "junk" that satisfies the following specification:
junk -lp {pathname}*
junk is a replacement for the rm command. Rather than removing one or more files/directories, it moves them into a ~/.junk directory, which should be created if it does not exist. The -l option lists the current contents of the ~/.junk directory, and the -p option purges ~/.junk directory. junk could be called with no options (at least one pathname is required), with either option, or with both options (one or more pathname(s) are optional in the latter two cases).

I did some coding but I am not sure if I am on right track. Please advise me to the right direction.
Thank you.

[code]
#!bin/csh

if [ $# != 1 ] then
echo "junk: One argument required" >&2
endif
if [ ! -d $HOME/.junk ] then
mkdir $HOME/.junk
endif
foreach 1(-l-p*)
switch ($1)
case -l:
ls -l $HOME/.junk | tail +2
breaksw
case -p:
rm -r $HOME/.junk
breaksw
default:
echo "junk: No write permission for file $1" >&2
endsw
endif
mv "$1" $HOME/.junk
exit 0 ;;
end

[/code]


See More: C shell script to implement utility junk

Report •

#1
October 3, 2010 at 00:11:44
Only a beginner c shell coder myself, but I can offer a bit of advice. There's some syntax errors which I've tried to correct. I don't guarantee I've got all of them, so be sure to test it. I've also included some coding solutions as comments that I think should be there.

#! /bin/csh

if ( $#argv != 1 ) then
echo "junk: One argument required" >&2
endif

if ( ! -d ~/.junk ) then
mkdir ~/.junk
endif

foreach 1 (-l-p*)
switch ($1)
case -l:
ls -l ~/.junk | tail +2
breaksw
case -p:
rm -r ~/.junk
breaksw
default:
echo "junk: No write permission for file $1" >&2
endsw
endif
# I would recommend adding in code that appends a unique number or the date of deletion to the filename when it is moved to .junk, that way you don't accidentally overwrite other files with the same name.
mv "$1" ~/.junk
exit 0 ;;
end

I've tried to implement something similar myself, but I haven't been overly happy with the results, so I'd be interested to see what your final version is like.
My code is only on my work computer so if you still need help by Tuesday, I can give you a copy of the relevant parts of my code.

Also, there is a major difference between this code and the rm command, so you either need to fix this or just keep in mind that they won't behave in the same way. Essentially the rm command can only delete files. The above code can also delete folders.
So if you have a folder containing: Folder1, Folder2, one.txt, two.txt, three.txt, four.txt
the command rm f* will just remove four.txt
but junk f* will (re)move Folder1, Folder2 and four.txt

I would suggest you change the mv bit to:
if (! -d $1)
mv "$1" ~/.junk
endif

You could also add in a -f option which allows folder deletion.

guest


Report •
Related Solutions


Ask Question