Append Loop

How to append an existing file?
The existing file in the mywastebasket will have the version number zero appended to it and the newly deleted file will have version number 1 appended to it.
How the loop looks like?

My code be like

 #!/bin/sh
read file 
mv $file mywastebasket
rm -i /home/himeose/mywastebasket/

So how am i gonna proceed?

Welcome on board!

So far in your script I see no loop...

I don't see how in your script you are giving version numbers
Now things unclear:

  1. you read file, OK but from what?
  2. you move $file to a file or a directory?
  3. are we to understand we are in /home/himeose ?
  4. where are you appending version number?
1 Like
  1. Read file from home
    At /home/himeose/mywastebasket
    I will code this after the bash done
touch home.txt
bash delete.sh
home.txt
Are you sure to delete or not? y
  1. Move the file (at home) to directory (mywastebasket is dir)

  2. Yes we are at /home/himeose/

  3. After rm - i /home/himeose/mywastebasket/

The loop
Mybe i should use

If [ conditional expression1 ]
then
	statement1
	statement2
	.
elif [ conditional expression2 ]
then
	statement3
	statement4
	.
.
.
else
	statement5
fi

After the file name appended

I can clear all the file in the mywastebasket dir.
By using

alias rm - c = "rm -v"

read file
if [ ! -e "$file" ]
then
  echo "$file does not exist"
  exit
fi

nfile=$HOME/mywastebasket/$file
# ensure that $nfile does not exist
nn=0
while [ -e "$nfile" ]
do
  # append a number and check again
  nfile=${nfile}_$nn
  nn=$((nn+1))
done

mv "$file" "$nfile"
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.