|
Home > Archive > Unix Shell > December 2007 > Script to find all soft links problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Script to find all soft links problem
|
|
| larryalk 2007-12-09, 1:35 am |
| I have written a small script to display all soft links in a
subdirectory but the script _also_ displays the name of the
executable.
How can I show only the soft links?
The script is:
#! /bin/bash
# name: saysoftlinks - display soft links in a directory
for x in *; do
if [ -L $x ]; then
echo $x
fi
done
This is the result of running the script:
snapshot.daily
snapshot.weekly
startx.old
subnetdel
sync1WRITE
syncdirwrt
syncrWRITE
/usr/local/bin/saysoftlinks: line 4: [: this: binary operator expected
thunderbird
tkdiff
umtiso
umtlinda
umtmirror
winsetup
I do not wish the line that contains "binary operator expected" to
appear.
Larry
--
My real sig is much better.
| |
| Steffen Schuler 2007-12-09, 1:35 am |
| On Sat, 08 Dec 2007 22:08:49 -0600, larryalk wrote:
> I have written a small script to display all soft links in a
> subdirectory but the script _also_ displays the name of the executable.
>
> How can I show only the soft links?
>
> The script is:
>
> #! /bin/bash
> # name: saysoftlinks - display soft links in a directory for x in *; do
> if [ -L $x ]; then
> echo $x
> fi
> done
>
> This is the result of running the script:
>
> snapshot.daily
> snapshot.weekly
> startx.old
> subnetdel
> sync1WRITE
> syncdirwrt
> syncrWRITE
> /usr/local/bin/saysoftlinks: line 4: [: this: binary operator expected
> thunderbird
> tkdiff
> umtiso
> umtlinda
> umtmirror
> winsetup
>
> I do not wish the line that contains "binary operator expected" to
> appear.
>
> Larry
Don't forget quoting and use printf instead of echo.
I assume that you have a filename in your directory with whitespace
inside, which leads in the unquoted case to your error message.
Besides, your script is a valid POSIX shell script --- no need of bash.
#!/bin/sh
# name: saysoftlinks - display soft links in a directory
for x in *
do
[ -L "$x" ] && printf "%s\n" "$x"
done
--
Steffen
| |
| Stephane Chazelas 2007-12-09, 7:34 am |
| On 9 Dec 2007 05:04:04 GMT, Steffen Schuler wrote:
[...]
> #!/bin/sh
> # name: saysoftlinks - display soft links in a directory
> for x in *
> do
> [ -L "$x" ] && printf "%s\n" "$x"
> done
[...]
find . ! -name . -prune -type l -print
Or with zsh:
print -rl -- *(D@)
--
Stephane
| |
| Steffen Schuler 2007-12-09, 7:34 am |
| On Sun, 09 Dec 2007 08:59:31 +0000, Stephane Chazelas wrote:
> find . ! -name . -prune -type l -print
Stephane, I don't understand your usage of -prune:
IMHO one prohibits descending the tree from a certain directory:
usage: find . -name dir -prune -o -print
Please, could you explain your code.
--
Steffen
| |
| Stephane Chazelas 2007-12-09, 7:34 am |
| On 9 Dec 2007 10:19:59 GMT, Steffen Schuler wrote:
> On Sun, 09 Dec 2007 08:59:31 +0000, Stephane Chazelas wrote:
>
>
> Stephane, I don't understand your usage of -prune:
> IMHO one prohibits descending the tree from a certain directory:
>
> usage: find . -name dir -prune -o -print
>
> Please, could you explain your code.
[...]
It says:
for every file or directory not (!) named (-name) ".", prune it
(don't descend into it) (-prune) and (-a implicit) if it's of
type (-type) symlink (l), print its path (-print).
You could have done:
find . \( -name . -o -prune \) -type l -print
That would have given the same result, but that would have
unnecessarily checked whether "." was a symlink.
GNU find and some BSDs find have the -mindepth and -maxdepth
predicate (non-standard).
So, the first one could also be written with those find
implementations:
find . ! -name . -prune -type l -print
find . -mindepth 1 -maxdepth 1 -type l -print
and the second one:
find . \( -name . -o -prune \) -type l -print
find . -maxdepth 1 -type l -print
--
Stephane
| |
| Steffen Schuler 2007-12-09, 7:34 am |
| On Sun, 09 Dec 2007 11:06:04 +0000, Stephane Chazelas wrote:
[...]
[...][vbcol=seagreen]
>
> It says:
>
> for every file or directory not (!) named (-name) ".", prune it (don't
> descend into it) (-prune) and (-a implicit) if it's of type (-type)
> symlink (l), print its path (-print).
>
> You could have done:
>
> find . \( -name . -o -prune \) -type l -print
>
> That would have given the same result, but that would have unnecessarily
> checked whether "." was a symlink.
>
> GNU find and some BSDs find have the -mindepth and -maxdepth predicate
> (non-standard).
>
> So, the first one could also be written with those find implementations:
>
> find . ! -name . -prune -type l -print find . -mindepth 1 -maxdepth 1
> -type l -print
>
> and the second one:
>
> find . \( -name . -o -prune \) -type l -print find . -maxdepth 1 -type l
> -print
Thank you very much for explaining this trick. You rock!!!
Shortly before I got your response, I did understand it by myself.
--
Steffen
| |
| larryalk 2007-12-09, 7:21 pm |
| On 9 Dec 2007 05:04:04 GMT, Steffen Schuler
<schuler.steffen@googlemail.com> wrote:
>
>Don't forget quoting and use printf instead of echo.
>
>I assume that you have a filename in your directory with whitespace
>inside, which leads in the unquoted case to your error message.
>
>Besides, your script is a valid POSIX shell script --- no need of bash.
>
>
>
>#!/bin/sh
># name: saysoftlinks - display soft links in a directory
>for x in *
>do
> [ -L "$x" ] && printf "%s\n" "$x"
>done
You were right on the money Steffen.
My practice is to not allow spaces in executable names but I looked
found an unexpected one. After changing that filename, the "binary
operator expected" line disappeared.
I understand the need to quote in the
echo "$x"
but could you tell me why you advise printf instead of echo?
I'm not new to scripting but rarely use printf.
Larry
--
My real sig is much better.
| |
| Steffen Schuler 2007-12-10, 1:42 am |
| On Sun, 09 Dec 2007 14:26:59 -0600, larryalk wrote:
[...]
> but could you tell me why you advise printf instead of echo?
>
> I'm not new to scripting but rarely use printf.
[...]
There are a lot of problems with echo. No unique way in handling options
of echo between different OS. Look at
http://www.unix.org/single_unix_specification/
Search for "echo" and read the part under "Application Use".
There "printf" is recommend as a portable form of "echo".
--
Steffen
|
|
|
|
|