I am trying to write a "light" version of srm just as an exercise of shell scripting (started today with this ^^" I have to keep it really simple)
Basically it should just search for a file and, if found, ask for a confirmation before deleting it.
Now, it worked fine with /any/directory/you/like/no_spaces_in_the_name.file and I hoped to make it a little less dumb :P
Here's the code:
#!/bin/bash # #Script to safely remove files (asks confirmation) # choice="n" if [ $# -eq 0 ] then echo "srm: missing argument." echo "Syntax: $0 file_to_safely_delete." exit 1 fi filename=$1 if [ $# -gt 1 ] then filename="'$@'" echo "$filename" fi if test -f $filename then echo "$filename found. Are you sure you want to permanently delete it? (y/n)" read choice if [ $choice = "y" ] then `rm $filename` echo "$filename permanently deleted." else echo "$filename untouched." fi else echo "$filename not found. Nothing done." fi
The added part is
if [ $# -gt 1 ] then filename="'$@'" echo "$filename" fi
I put in it
echo "$filename" for debugging and it shows correctly the string
'name of file with spaces' The error I receive is @ this line:
if test -f $filename It prints out this:
line 18: test: too many arguments After I call it with
srm just a try I am just blind on why it doesn't work... any idea?
Anticipate thanks :)
I have not yet find anything abount problems with spacing in shell scripting, but ¿strangely? if I indent the code like I'm used to with c/c++ it complains a lot so I'm not doing it at all. It's not urgent but I think I'm simply missing something obvious, so I'll be very grateful to anyone who'll give a sort of explanation ^^"
as usual sorry for my english :$