User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 455,865 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,649 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 648 | Replies: 5 | Solved
Reply
Join Date: Jun 2008
Location: Rome
Posts: 63
Reputation: mrboolf is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
mrboolf mrboolf is offline Offline
Junior Poster in Training

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

  #1  
Jul 20th, 2008
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
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

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

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

  #2  
Jul 20th, 2008
Try putting the filename variable in quotes in other places as well.
Reply With Quote  
Join Date: Jun 2008
Location: Rome
Posts: 63
Reputation: mrboolf is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
mrboolf mrboolf is offline Offline
Junior Poster in Training

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

  #3  
Jul 20th, 2008
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:
ludovico@ludovico-laptop:~/Desktop$ srm just a try
'just a try'
'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
Reply With Quote  
Join Date: Oct 2007
Posts: 302
Reputation: eggi is on a distinguished road 
Rep Power: 2
Solved Threads: 29
eggi eggi is offline Offline
Posting Whiz

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

  #4  
Jul 20th, 2008
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

if test -f filename="'$@'"

to

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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
Reply With Quote  
Join Date: Jun 2008
Location: Rome
Posts: 63
Reputation: mrboolf is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
mrboolf mrboolf is offline Offline
Junior Poster in Training

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

  #5  
Jul 21st, 2008
It helped
Thank you both!
Last edited by mrboolf : Jul 21st, 2008 at 4:14 am.
Reply With Quote  
Join Date: Oct 2007
Posts: 302
Reputation: eggi is on a distinguished road 
Rep Power: 2
Solved Threads: 29
eggi eggi is offline Offline
Posting Whiz

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

  #6  
Jul 21st, 2008
Awesome

Glad to help out
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 6:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC