Restore files by lines number!

Hello every 1

Happy new year :wink:

I have a problem when i've tried to write my shell scripts..

this what i've done

Actually, i would like to restore the files that i have deleted by line number for example :

1 : filename1
2: filename2
3: filename3

then i can restore them by line number not by file name

any idea...

thanks in advance :slight_smile:

Hi,
I have just modified your script,

---------
#!/bin/sh

NOTV=64
menu_file=./menufile.out
trap "rm -f $menu_file" EXIT

count=0
for file in `ls -1 ./trash`
do
count=`expr $count + 1`
echo "$count: $file" >> $menu_file
done

cat $menu_file
echo -n "Please select a number from this menu : "
read num

FILE=`sed "/^\<$num\>/!d" menufile.out | awk -F ":" '{print $NF}'| sed 's/^ //'`
[ -z $FILE ] && echo "Not a valid number" && exit $NOTV
echo "Recovering $FILE"
[ -e "./trash/$FILE" ] && mv "./trash/$FILE" . || echo "$FILE is not present in ./trash dir"

-----

Let me know if it works :slight_smile:

//Jadu

Thank you very much man :slight_smile:

it's working perfectly :b:

[quote=jaduks;302155813]
Hi,
count=0
for file in `ls -1 ./trash`
do
count=`expr $count + 1`
echo "$count: $file" >> $menu_file
done


Take care of files with spaces.  Use shell expansion instead
eg for file in ./trash/* 

. take note it will not list out hidden files. Use while loop if hidden files are required. eg

ls -a | while read file ......

Why do all this manual stuff, when you have "select"?

#!/bin/ksh

trashdir=./trash
PS3='Enter your choice: '
quitstr=exit
select fname in `ls -1 $trashdir/*` $quitstr ; do
        [ "${fname}" = "${quitstr}" ] || [ "${fname}x" = "x" ] && break
        mv $fname . || echo "Error moving file $fname to `pwd`"
        break
done

In action:

$ ls -ltr trash
total 0
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 d
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 c
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 b
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 a

$ clean.sh
1) ./trash/a
2) ./trash/b
3) ./trash/c
4) ./trash/d
5) exit
Enter your choice: 99

$ clean.sh
1) ./trash/a
2) ./trash/b
3) ./trash/c
4) ./trash/d
5) exit
Enter your choice: 5

$ clean.sh
1) ./trash/a
2) ./trash/b
3) ./trash/c
4) ./trash/d
5) exit
Enter your choice: 4
$ ls -ltr d
-rw-r-----   1 sdass    informix       0 Jan  7 02:26 d

It is quite compact and clean, builds and shows menu w/o separate coding and has in-built checks for empty/invalid inputs.

HTH

...
select fname in $trashdir/* $quitstr 
...

There is a problem with that style if trash is empty:

$ ls -ltr trash
total 0
$ clean.sh
1) ./trash/*
2) exit
Enter your choice: 2

Of course we can put some additional checks for empty directories - left as an exercise for OP :wink:

The reason using that style is to take care of file names with spaces. Of course, if the ls method can do that, then that's fine. :slight_smile:

Of course, the ls way cannot deal with spaces. So lets do this:

#!/bin/ksh

trashdir=./trash
PS3='Enter your choice: '
quitstr=exit
[ "$(echo $trashdir/*)" = "${trashdir}/*" ] && echo "No trash" && exit
select fname in $trashdir/* $quitstr ; do
        [ "${fname}" = "${quitstr}" ] || [ "${fname}x" = "x" ] && break
        mv "${fname}" . || echo "Error moving file $fname to `pwd`"
        break
done

Output:

$ ls -ltr trash
total 0
-rw-r-----   1 sdass    informix       0 Jan  7 04:23 file with spaces
-rw-r-----   1 sdass    informix       0 Jan  7 04:23 c
-rw-r-----   1 sdass    informix       0 Jan  7 04:23 b
-rw-r-----   1 sdass    informix       0 Jan  7 04:23 a

$ clean.sh
1) ./trash/a
2) ./trash/b
3) ./trash/c
4) ./trash/file with spaces
5) exit
Enter your choice: 4
$ ls -ltr "file with spaces"
-rw-r-----   1 sdass    informix       0 Jan  7 04:23 file with spaces

$ rm -fr trash/*
$ ls -ltr trash
total 0
$ clean.sh
No trash

:slight_smile: