pipe to ls with filenames with spaces breaks!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pipe to ls with filenames with spaces breaks!
# 8  
Old 07-25-2008
My version of find permits formating output like in:
Code:
find . -maxdepth 1 ! -name . -and -type d -printf "%f\n"

Much more format directives available in man find.

@era: to explicitly exclude the dot from the list, shoudn't you use the condition
! -name . -a -type d instead of -name . -or -type d ?
# 9  
Old 07-25-2008
Works for me. It's common that "this or that" is used as a shortcut for "if not this then that".
# 10  
Old 07-27-2008
Bug Thanks, and maybe you can clear this up.

Quote:
Originally Posted by era
As a minor quib, you should probably prefer "$@" over $*
This is cool! I'm sure I have something to learn here, because I had thought that outside of double quotes, $@ and $* were identical. Quoting from the Bash Reference Manual:
Quote:
*Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
@Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" .... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
So, please, I've always wondered about this--why should I prefer to use one over the other, and in what circumstances--Does it matter outside of double quotes, and what the heck do the descriptions of the behavior within double quotes mean?

Patrick
# 11  
Old 07-27-2008
Quote:
Originally Posted by phorgan1
I wanted an alias or program, lsd, that would show just the directories in a directory.
Code:
set -- ${*:-.}
for x
do
   [ -d "$x" ] && ls -ld ${x:+$x/}*/ 2>/dev/null
done

Or:
Code:
[ -n "$1" ] && cd "$1" || return 1
printf "%s\n" "$PWD"/*/

Or:
Code:
[ -n "$1" ] && cd "$1" || return 1
ls -ld "$PWD"/*/

Etc....
# 12  
Old 07-27-2008
Follow up to the $@ vs $* conundrum

Quote:
Originally Posted by phorgan1
This is cool! I'm sure I have something to learn here, because I had thought that outside of double quotes, $@ and $* were identical. Quoting from the Bash Reference Manual:

So, please, I've always wondered about this--why should I prefer to use one over the other, and in what circumstances--Does it matter outside of double quotes, and what the heck do the descriptions of the behavior within double quotes mean?

Patrick
So following up, this script:
Code:
#!/bin/bash
export IFS="
"
echo "$@"
echo "$*"
for arg in $@ ; do
    echo $arg
done
for arg in $* ; do
    echo $arg
done
for arg in "$@" ; do
    echo $arg
done
for arg in "$*" ; do
    echo $arg
done

has this output:
Code:
./shtest a b c
a b c
a
b
c
a
b
c
a
b
c
a
b
c
a b c

So, seems the same outside quotes, but what the heck is going on inside the quotes? When would I want the join them all together behavior and when would I want the individual words behavior?

Patrick
# 13  
Old 07-27-2008
Quote:
Originally Posted by phorgan1
So following up, this script:
Code:
./shtest a b c
a b c
a
b
c
a
b
c
a
b
c
a
b
c
a b c

So, seems the same outside quotes, but what the heck is going on inside the quotes?

Try it with:
Code:
./shtest a "b c" d

Quote:
When would I want the join them all together behavior and when would I want the individual words behavior?

Use $* or $@ when you want the individual words as separate parameters.

Use "$*" when you want all the words as a single argument.

Use "$@" when you want each argument (which may contain spaces) as a separate parameter.

# 14  
Old 07-28-2008
Or in simpler terms, historically, $* was what they had originally, but it didn't work right for arguments containing embedded whitespace, and so they had to invent "$@" for passing through quoted arguments correctly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handling filenames with spaces

I'm trying to handle some files with spaces in their name using "" or \ . Like "file 1" or file\ 1. My current confusion can be expressed by the following shell script: #!/bin/bash touch "file 1" "file 2" echo -n "ls: " ; ls echo --- for file in "file 1" "file 2" ; do echo $file... (9 Replies)
Discussion started by: Ralph
9 Replies

2. UNIX for Beginners Questions & Answers

Remove line breaks and extra spaces

Hi, I want to remove all extra spaces, line breaks . Need a new line entry only for term starting"array" For eg: my input is array(), array(), array(), and my expected output is array(), array(), array(), Is it possible using awk? (5 Replies)
Discussion started by: rsi.245
5 Replies

3. Shell Programming and Scripting

Delete line breaks and extra spaces between patterns

Hi, if in between strings "<section" and "</section>" across multiple lines there occurs the string "ole-present", delete all line breaks and replace any tabs or multiple spaces with a single space. Looking for an AWK or SED solution. Thank you. <section ... status = "ole-present" ...... (2 Replies)
Discussion started by: pioavi
2 Replies

4. Shell Programming and Scripting

Remove spaces in filenames

Hi, I have files like below, In files coming as spaces. Before transfering those files into ftp server. I want to remove the spaces and then can transfer the files into unix server. e.g: filenames are 1) SHmail _profile001_20120908.txt 2) SHmail_profile001 _20120908.txt 3) sh... (3 Replies)
Discussion started by: kirankumar
3 Replies

5. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 Replies

6. Shell Programming and Scripting

Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below: FILESSH=$(ls /mysh/*.sh) --- FILESH being used here for some other task --- echo "$FILESSH" |... (3 Replies)
Discussion started by: amicon007
3 Replies

7. Shell Programming and Scripting

spaces in filenames

Hi I hope someone will be able to resolve this little teaser! I am running a script for file in `ls directory` do echo "$file" ...other code here.... done this works fine unless we receive a file with a name which has a space in it ie "filena me" (I know its not good... (8 Replies)
Discussion started by: Bab00shka
8 Replies

8. Shell Programming and Scripting

spaces in filenames, for do

Hi All, I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory. What I have: ... (4 Replies)
Discussion started by: naviztirf
4 Replies

9. Shell Programming and Scripting

Unix filenames and spaces

I have files on my unix boxes that users have created with spaces. Example: /tmp/project plan ls -l "/tmp/project plan" works fine. $/tmp>ls -l "/tmp/project plan" -rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan I created a file called test and put just the... (2 Replies)
Discussion started by: x96riley3
2 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question