简体   繁体   中英

Linux Shell Script Stuck

I have a University coursework to complete and I am kind of stuck on this part:

del - This script should move the file called to the dustbin directory in a manner that will allow the file to be restored to its original location later if necessary.

I have made an attempt as shown bellow but It's not working:

#!/bin/bash
echo "Do you want to delete this file?"
echo "Y/N"
read ans
case "$ans" in
  Y) echo "`readlink -f $1`" >>/TAM/store & mv $1 /~/dustbin ;;
  N) echo "File not deleted" ;;
esac

When I run it I get this:

./Del: line 8: /TAM/store: No such file or directory
MV: missign destination file operand after '/~/dustbin'

Also How can use User input to enter the name of the file? or can you not do that.

PS ~ is the root directory, TAM is my Directory, store is the file and dustbin is the dustbin directory in the root . Del is the name of the script

Since you said it's course work I won't give you a complete solution, but a pretty simple(simplified) start:

#!/bin/sh

if [ $# -eq 0 ]; then
    printf "You didn't give an argument, please input file name: \n"
    filename=READ_FILE_NAME_HERE
elif [$# -eq 1 ]; then
    filename=$1
else
    printf "Error: You gave to many parameters!\n"
    exit 1
fi

# Does the file exist (and is a regular file)?
[ -f "$filename" ] || {
    printf "Error: File doesn't exist or isn't a regular file.\n"
    exit 2
    }

Do_you_really_want_to_delete_the_file?
Do_the_remove_magic

That should get you started on the "either take parameter input, or if none, allow user to input filename"-problem.

If you get past the checks, you know that filename contains a valid file name, so you could remove the readlink call, (although it won't give you the full path then), but you could use printf "$filename" >>DEST etc.

There is a lot of good information to read in the manual for bash . (Try: man bash )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM