Newbie question: if[command not null]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Newbie question: if[command not null]
# 8  
Old 04-12-2011
@DGPickett:
Quote:
since I can control that with start dir arg(s).
What do you mean by this? How?

Quote:
Here, it messes up the comm, making identical files on identical relative paths show different (all show different).
The construct with xargs -0 works fine on my machine (centos 5.3; GNU bash, version 3.2.25).

Quote:
The xargs -0 option is nice when the input is lines of badly behaved file names
That's exactly why I suggested it. Your original reply failed when I ran it on a dir with files containing spaces.
# 9  
Old 04-12-2011
"find *" is relative paths, "find $PWD" is absolute paths.

Absolute paths means lines never match, dir1 to dir2.

My bro said "file names with spaces, only Microsoft would so something stupid like that!" Actually, they are just about as legal in UNIX, just shunned by the culture. Use a '_', '.', '-' or nothing. I like the -, handy and easy to read past. Smilie
# 10  
Old 04-12-2011
I agree that including spaces in filenames is a very dirty practice, yet it is not uncommon. My version with xargs -0 worked fine -- I ran it with relative path like this:
Code:
comm -3 < ( cd dir1; find . -type f -print0 | xargs -0 -n999 cksum | 
                     sed 's/\(.*\) \(.*\)/\2 \1/' | sort ) 
        < ( cd dir2;  find . -type f -print0 | xargs -0 -n999 cksum | 
                     sed 's/\(.*\) \(.*\)/\2 \1/' | sort ) | 
sed '
  s/^\t/dir2 /
  t 
  s/^/dir1 /
'

You used find * , but the result should be the same

Last edited by mirni; 04-12-2011 at 07:19 PM..
# 11  
Old 04-12-2011
I disagree. I personally see nothing "stupid" or "dirty" about spaces in filenames. If a system has problems with spaces in filenames, I'm more inclined to look unfavorably on the system than on whoever named the file.

Quote:
Originally Posted by Doug McIlroy, the inventor of Unix pipes and one of the founders of the Unix tradition
This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.
The problem, in my opinion, is that since UNIX filenames are allowed to contain '\t' and '\n', the output from find(1) is not guaranteed to be a decipherable text stream where newlines delimit a record/line and tabs delimit fields. (It doesn't help that the portable subset of xargs features is a bit lacking as well.)

Perhaps the text stream has outlived its usefulness. Time for everyone to migrate to Powershell? Smilie

Regards,
Alister
# 12  
Old 04-12-2011
Quote:
Originally Posted by alister
I disagree. I personally see nothing "stupid" or "dirty" about spaces in filenames. If a system has problems with spaces in filenames, I'm more inclined to look unfavorably on the system than on whoever named the file.



The problem, in my opinion, is that since UNIX filenames are allowed to contain '\t' and '\n', the output from find(1) is not guaranteed to be a decipherable text stream where newlines delimit a record/line and tabs delimit fields. (It doesn't help that the portable subset of xargs features is a bit lacking as well.)

Perhaps the text stream has outlived its usefulness.
Don't be hasty. There's one and exactly one character that's not allowed to be in a UNIX filename: NULL. if find can be made to print NULL, and xargs can be made to use NULL as a delimiter, that is a 100% safe to delimit filenames.

As it happens GNU find has the -print0 option to print nulls instead of newlines, and GNU xargs has the --null option to use nulls as separators.

If your argument is just that these arguments aren't portable, perhaps it's time to mandate them in POSIX.

There's always -exec, too, which will always be given a correct filename.
# 13  
Old 04-12-2011
Quote:
Originally Posted by Corona688
As it happens GNU find has the -print0 option to print nulls instead of newlines, and GNU xargs has the --null option to use nulls as separators.

If your argument is just that these arguments aren't portable, perhaps it's time to mandate them in POSIX.

There's always -exec, too, which will always be given a correct filename.
The find/xarg -print0/-0 tandem is useful but it's of no help when the output is coming from ls or stat or lsof or any one of countless tools which output filenames.

I do agree with you though that it would be useful for posix xargs to support some means of specifying a delimiter.

My argument is simply that traditional unix tools were designed to pipe text streams but filenames, a central system abstraction, cannot be part of a text stream without the potential for ambiguity/corruption (Is this newline actually the beginning of a new line or part of a foolish filenaming scheme?). Due to this we often have to choose between a simple, 80% solution (which is often sufficient) or a comparatively cumbersome approach.

The design decision was made long ago; it's not going anywhere; and in practice it isn't usually a problem. Still, I think would have been a good decision to have disallowed \t and \n in filenames. <insert visions of {file,path}names shuttling through pipes without a care in the world>

Regards,
Alister
# 14  
Old 04-13-2011
Run, high priests on rampage ! Smilie

Yes, xargs -0 is nice for spaces in file names, if you have them, and similarly, find . is safer than find *, no meta-in-entry-name vulnerability, if you do not mind the './' prefix, or tell sed to toss it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Newbie PATH command question...

Still trying to pick up speed on the command line in OSX. I have installed Apache, and some other server software, but am having problems getting my install of Perl to work. I feel like it's because my Apache install is looking for the base (built-in) Perl that came with OSX which is 5.10. I... (4 Replies)
Discussion started by: Bridger
4 Replies

2. Shell Programming and Scripting

Question on NULL and zero value of variable

Hi all, I have a stupid question on NULL and zero(0). In a script I've been working with, one of the lines is: if && then The problem I seem to have is when $Current_csm2 is null, this if block is not triggered, and I don't get why because I was under the impression that NULL!=0 Can... (7 Replies)
Discussion started by: spynappels
7 Replies

3. UNIX for Dummies Questions & Answers

newbie question

Hi all, I am sure this is very simple but I cant quite get it. I am trying to search textfile1.txt for a string then take the results of the search and append the result to textfile3.txt So far I have used $ find file1.txt -exec grep "string i am looking for" '{}' \; -print this... (2 Replies)
Discussion started by: radgator
2 Replies

4. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

5. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

6. Programming

Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as Sent mesg: hello Bytes Sent to Client: 6 bytes_received = recv(clientSockD, data, MAX_DATA, 0); if(bytes_received) { send(clientSockD, data, bytes_received, 0); data = '\0';... (2 Replies)
Discussion started by: f.ben.isaac
2 Replies

7. UNIX for Dummies Questions & Answers

Newbie question?

What is the best way to learn UNIX on the web, with out buying books? any link would be much help. Thank you in advance, L (1 Reply)
Discussion started by: lsoria1
1 Replies

8. Shell Programming and Scripting

newbie question

hey all, I have repeatedly seen scripts containing the following syntax, grep "hello" $myfile >> $log 2>&1 can anyone explain exactly what "2>&1" mean? THANK YOU (4 Replies)
Discussion started by: mpang_
4 Replies

9. Shell Programming and Scripting

Newbie question

Hello, I have text file while looks this test1 test2 test3 test4 test5 test6 and if I want to parse it and make new file which would like this test1 test2 test3 test4 test5 test6 How can I do this in korn shell script Thanks (9 Replies)
Discussion started by: peeyush_23
9 Replies

10. UNIX for Dummies Questions & Answers

/dev/null 2>&1 question

Hi, suppose you have the following line at your crontab : 5 * * * * /usr/mine/script > /dev/null 2>&1 now i understood that the " > /dev/null 2>&1 outputs both Standard outpout and Standard Error messages to the /dev/null device or file... the first part , " > /dev/null " transfers... (1 Reply)
Discussion started by: BAM
1 Replies
Login or Register to Ask a Question