LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-16-2008, 06:25 PM   #16
yao_man
LQ Newbie
 
Registered: May 2008
Posts: 7

Original Poster
Rep: Reputation: 0

GOTIT. I replaced both block1 and block2 with a single block1
Code:
#start of block 1
	for file in $arg/* $arg/.*;do
		if [[ ${file:(${#file}-1):1} == "." || ${file:(${#file}-2):2} == ".." ]];then
			continue
		elif [ -d "$file" ];then
			delete "$file"
		elif [ -r "$file" ];then
			echo "/bin/rm \"$file\"" >> delete_tmp
		fi
	done
#end of block1
 
Old 05-18-2008, 08:09 AM   #17
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
hmm.. another mistake of mine.. since you already got it.. please ignore this message

--------

i love squeezing logics in a single for loop but i can't explain how to do it anyway
..so perhaps make it simpler like a for ((;;)) statement..
make a few lines that processes the first arg then do the other processes that appends arg with the old arg in the for loop like:
Code:
arg=$1
...
shift
oldarg=$arg
for arg in "$@"; do
   arg=$arg/$oldarg
   ...
   $oldarg=$arg
done

some suggestions:

you can make arg not parse spaces by doing:
Code:
while [ $# -eq 1 ]; do
   arg=$1
   ...
   shift
done
or
Code:
for arg in "$@"; do
   ...
done
it's also simpler to do IFS=$'\n' in newer shells like bash

Last edited by konsolebox; 05-18-2008 at 08:12 AM.
 
Old 05-19-2008, 06:31 AM   #18
calraith
Member
 
Registered: Apr 2008
Location: Gray, TN, USA
Distribution: UbuntuStudio, Linux Mint
Posts: 36

Rep: Reputation: 15
Quote:
Originally Posted by yao_man View Post
I aliased rm and rmdir so that they move the files to the trash instead of removing them completely by using a "trash" function I wrote. I am trying to create a delete function that checks for approval before completely removing the given file and anything within that file (even if it is a directory) so I can easily remove files and directories when I need to.
Both of those have troubles if the file has a space in it.
BTW, this is more of an attempt to teach myself programming problem solving and logic as well as overall programming ability then an attempt at efficiency.
If you're using find, you don't need a loop. Just use xargs, and it should handle spaces just fine while still being efficient.

Code:
find -maxdepth 1 -type f -print0 | xargs -0 -i -t mv {} /tmp/Trash
from the "find" man page:
Quote:
-print0
True; print the full file name on the standard output, followed by a null character (instead of the newline character that `-print' uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the `-0' option of xargs.
(:
 
Old 05-19-2008, 07:17 AM   #19
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,038

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Quote:
Originally Posted by jschiwal View Post
You could either use
for file in $(ls -A)
or
for file in $(find . -maxdepth 1 -type f)

The latter will return only files.

Rather than aliasing rm and rmdir you might want to name the command something like "trash" so that the name describes what you want to do. You might also might want to only "trash" the files that you give in arguments instead of all of the contents in the current directory.
This is your original problem: when you have a file name (or any other argument for that matter) which has an space or any other character that has any special meaning for your shell, then you need to quote or scape it.

So, if

Code:
file="my file"
Then,

Code:
rm $file
Will be substituted as

Code:
rm my file
And it will search for two different files, called "my" and "file".

But if you quote the thing adequately, then the thing is much different.

Code:
rm "$file"
Remember to quote the things adequately if you don't know what a given variable is going to hold.

I'm in a hurry now, if I can I will review the rest of the thread later just in case.

cheers.
 
  


Reply

Tags
bash, directory, file, folder, iterate, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script question noisewater Programming 3 11-14-2005 08:39 PM
Bash shell script moodupani Linux - General 2 09-01-2005 06:14 AM
bash shell script globeTrotter Linux - Newbie 5 06-03-2004 05:07 AM
bash/shell script mikis Programming 2 10-14-2003 11:39 AM
bash shell script MaryM Linux - Newbie 0 02-15-2002 11:45 PM


All times are GMT -5. The time now is 03:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration