Re: shell scripting with solaris, help.
From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 11/29/04
- Next message: Lisa Koma: "Re: Solaris 10 and Solaris Service Manager"
- Previous message: Rich Teer: "Re: old_procfs.h and large file environment"
- In reply to: Christo: "shell scripting with solaris, help."
- Next in thread: Nick Maclaren: "Re: shell scripting with solaris, help."
- Reply: Nick Maclaren: "Re: shell scripting with solaris, help."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 29 Nov 2004 17:13:51 +0000
2004-11-29, 15:43(-00), Christo:
[...]
> #!/bin/sh
Note that it's always preferable to separate options from other
arguments in shell scripts (with "--" or sometimes "-") so that
a normal argument is not taken as an option. So, to be pedantic,
the above should be:
#! /bin/sh -
(note that the space between ! and / is not necessary but it
makes the path to the interpreter easier to read).
Now, the system will run /bin/sh - <the path to the script>
which will work OK even if <the path to the script> starts with
a "-" or a "+".
Moreover, on Solaris, /bin/sh is the old deprecated sh (the
Bourne shell), so, if you want the script to be interpreted by a
shell that recognizes the same syntax as the sh of other Unices,
you'd better use:
#! /usr/xpg4/bin/sh -
(only on Solaris) and as the first line of the script:
PATH=$(command -p getconf PATH)
export PATH
# so that the standard versions of the utilities are used
>
> dcount=0
> fcount=0
>
> for fn in *
There's a problem with that above syntax. If there's no file in
the current directory, the * will not get expanded so you'll get
one pass into the loop with fn being equal to "*".
Also note that it will not expand the dot files (.profile, ..,
etc)
> do
>
> dflag=`ls -ld $fn | cut -c1`
As said above, it should be:
ls -ld -- "$fn"
(note the quotes around $fn, they are necessary in Bourne like
shells because otherwise the variable is taken as a list of file
patterns (while here you want it to be taken as a normal
stringa)).
Also note, you want the first character of the first line of the
output of ls only (a filename can contain any character
including a newline character).
ls -ld -- "$fn" | head -1 | cut -b1
or:
ls -ld -- "$fn" | dd count=1 bs=1 2> /dev/null
But there are easier ways to test wether a file is a directory:
[ -d "$file" ] && [ ! -L "$file" ]
You need the additional -L because "[ -d" would return true for
a symlink to a directory.
> name=`ls -ld $fn | cut -c55-75`
a filename maybe more than 21 characters wide and may contain
newline characters. For symbolic links "-> target" is included
and the output of ls is dependant on the locale and the columns
starting position may vary (for example if the filesize or the
number of links are above a certain value).
Moreover the filename is already in $fn.
> if [ $dflag = 'd' ]
if [ d = "$dflag" ]
> then
> dcount=`expr $dcount + 1
dcount=`expr "$dcount" + 1`
> echo "$name (DIR)"
echo can't be passed variable arguments because it expands its
arguments recognizing ANSI C like escape sequences.
For instance, a file named "some\text" would be displayed as
"some ext" (\t expanded to a <Tab>).
For variable output, you should use the "printf" utility.
> else
> fcount=`expr $fcount + 1
That means you exclude directories from the count of files. But
you don't exclude the sockets, symlinks, named pipes...
Depending on what one means by "file", I would either count all
the files (regardless of their type) or just the regular files
(those for which the ls -l output starts with "-").
[...]
> howeverer this script must only display and count files that are writeable
> by group and other
So, you'd need to use the test (aka "[") utility.
See the test(1) manpage, you may also want to check the manpage
of the shell as that utility is generally built in the shell.
-- Stephane
- Next message: Lisa Koma: "Re: Solaris 10 and Solaris Service Manager"
- Previous message: Rich Teer: "Re: old_procfs.h and large file environment"
- In reply to: Christo: "shell scripting with solaris, help."
- Next in thread: Nick Maclaren: "Re: shell scripting with solaris, help."
- Reply: Nick Maclaren: "Re: shell scripting with solaris, help."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|