943,672 Members | Top Members by Rank

Ad:
Jul 20th, 2008
0

Trying to rewrite srm (safely remove): problem with spaces in filenames

Expand Post »
Hi all.
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
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
Shell Scripting Syntax (Toggle Plain Text)
  1. if [ $# -gt 1 ]
  2. then
  3. filename="'$@'"
  4. echo "$filename"
  5. 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

p.s.
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 ^^"

p.p.s.
as usual sorry for my english
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Jul 20th, 2008
0

Re: Trying to rewrite srm (safely remove): problem with spaces in filenames

Try putting the filename variable in quotes in other places as well.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 20th, 2008
0

Re: Trying to rewrite srm (safely remove): problem with spaces in filenames

I made these changes after your suggestion (red lines had quotes added around $filename)

#!/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

Now it doesn't complains, but it's malfunctioning: it enters the last else case even if "just a try" actually exists as a file :?

I can't imagine neither the cause nor the solution of this problem...

Actual situation:
Shell Scripting Syntax (Toggle Plain Text)
  1. ludovico@ludovico-laptop:~/Desktop$ srm just a try
  2. 'just a try'
  3. 'just a try' not found. Nothing done.

Ok, putting single quotes ' ' around the file name when calling the script made it work properly. I still don't understand why... I thought that
...
filename="'$@'"
...
would have done the job. Any explanation? Btw, thank you all, I'm just curious now ^^
Last edited by mrboolf; Jul 20th, 2008 at 9:06 am. Reason: Solved, but still not understanding how
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Jul 20th, 2008
1

Re: Trying to rewrite srm (safely remove): problem with spaces in filenames

Hey There,

Inside the double quotes, in your test statement, the single quotes are being treated as literal characters in the value of the filename variable:

If you just change

Quote ...
if test -f filename="'$@'"
to

Quote ...
if test -f filename="$@"
you should be all set

Hope that helps

, Mike
Last edited by eggi; Jul 20th, 2008 at 11:48 pm. Reason: typo
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Jul 21st, 2008
0

Re: Trying to rewrite srm (safely remove): problem with spaces in filenames

It helped
Thank you both!
Last edited by mrboolf; Jul 21st, 2008 at 4:14 am.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Jul 21st, 2008
0

Re: Trying to rewrite srm (safely remove): problem with spaces in filenames

Awesome

Glad to help out
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: checking argument wheather is empty
Next Thread in Shell Scripting Forum Timeline: tar issues...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC