TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:2361] script problems



> are left. One is that if I run 
>
> deltest "john's data"
> is tries to move john's and then data -

Is because the shell removes the "" and gives the deltest script the
arguments.
When the deltest script passes the above to "mv" the "s have already been
stripped and mv sees john's and data as 2 separate arguments.

So you will have to add the "" before you pass the file name to "mv".

It is not as trivial as it seems because you are creating the whole argument
list as one string and passing it to "mv".  While creating the argument list
by appending each string to it at each step the "s in "John's Data" will get
removed.

I think you can manipulate the $IFS variable in someway to get this to work
the way you want. Or an easier option is to feed the "mv" command 1 filename
at a time.

i.e. 
Get list of arguments that are file names and then go. The following should
also fix your
problem of deleting a file with same name more than once.

I am not to sure about the "while" syntax but that
should explain what we are trying to do.

for file $*
do
 #if the file/directory find an empty slot for this file
  count=0
  target="/home/delete/$LOGNAME/$file.0"
  while [ -e "$target" ]; 
  do
         #this file exists, try another one.
         let count=$count+1
   done
 
   #when we get to here we have a unique filename for this trashed file. The
   # "s are important they ensure that the filenames donot break apart in 
   #case of spaces/newlines in between.
   mv -f $NEWARGS  "$file"  "$target"
done

Some more changes that I can suggest to the script are as :

> RECURE1=`echo $FLAGS |grep "^-" |grep r`
> RECURE2=`echo $FLAGS |grep "^-" |grep R`

> if [ ! -z $RECURE1 ]; then
> NEWFLAGS="$NEWFLAGS `echo $FLAGS |sed -e "s/r//"`"  
> elif [ ! -z $RECURE2 ]; then
> NEWFLAGS="$NEWFLAGS `echo $FLAGS |sed -e "s/R//"`"

Can be replaced with a composite search/replace
RECURE=`echo $FLAGS | grep "^-" | grep "[rR]"`
if [ ! -z $RECURE ]; then
   NEWFLAGS="$NEWFLAGS `echo $FLAGS | sed -e "s/[Rr]//"`"
fi

The [rR] will make the grep and sed look for either r or R.

> FLAGS=`echo $FLAGS |sed -e "s/--/dumbgrep5664/"`
> DASH=`echo $FLAGS |grep dumbgrep5664`
> if [ ! -z $DASH ]; then
> if [ $FLAGS != dumbgrep5664recursive ]; then

Confuses me, is it not the same thing as :

if [ $FLAGS != "--recursive" ]; then 
.
.

And here is my attempt at the same problem with standard disclaimers !
You will have to modify the "doyourstuff" to use the "mv" command at the end.
And the major problem is that you cannot do stuff like :
deltest   foo.c  -options.
It breaks out of the loop when it gets the first non-option flag. This is just
so that
the list of files is still available as distinct arguments so as to be able to
add the "s around it.

--
#initialize the new flags to blank.
newflags=""

for arg
do
	#remove one - sign
	postfix=${arg#-}

	#remove another - sign 
	#if there is no - sign then it will return just postfix.
	postfix=${postfix#-}

	#See how many "-" signs we have. 
	#This will leave us with the "-" signs only.
	dashes=${arg%$postfix}

	if [ -z $dashes ]; then

	   #No Dashes at all.
	   #Time to break out of here.
	   break;

	elif [ $dashes = '-' ]; then

	  #Look for a "r/R" option in there and remove as many r,R as you can find.
	  postfix=`echo $postfix | sed -e 's/[rR]//g'`

	  #if postfix is null then r/R is the only thing there so ignore it.
	  if [ ! -z $postfix ]; then
		newflags="$newflags -$postfix"
	  fi

	elif [ $arg != "--recursive" ]; then
		#Ignore the recursive flag if it is anything else.
		newflags="$newflags $arg"
	fi

	#shift the value out of the argument list.
	shift
done

#now execute the move/command on each file in the command line.
#If you want to append all the command line arguments and do it then
#it loses the original the "s in "part1 part2" and the following
#program sees them as 2 separate files.
#
for file
do
	#You need the "" otherwise a file name like "Temporary Files" will end
	#up as 2 arguments to douyourstuff instead of one.
	doyourstuff $newflags "$file"
done

-- End of that ---

Or you can store the deleted files with random/uniq names. I am sure there is
a system call that generates a unique file name. Then maintain a table of
deleted original filenames and locations and deletion dates to the random
filenames.  This will kind of do what the Windows "Recycle Bin" does. Provide
a nice little command line interface to this for listing deleted files and
restoring deleted files and it will be neat thing to have.

Since the file name you are moving it to is garaunteed to be unique you donot
have any problems moving.

I think the "kfm" file manager from the KDE project does this but I donot use
KDE all that much so I cannot say for sure.

Hope this helps.
--
sandipan