Re: shell scripting with solaris, help.

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 11/29/04


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


Relevant Pages

  • Re: system command failed
    ... the ^ character means the same thing as the | character -- ... pipe the output of one command into the input of another. ... So your shell ... This allows this Perl script to call myScript.pl directly, ...
    (comp.lang.perl.misc)
  • Chomp Equivalent
    ... there is a chompcommand that will strip the newline ... character off of a variable. ... Is there an equivalent to this in a shell ...
    (alt.linux)
  • Re: detect shell script language
    ... In the 'old' days, when a user typed in a command, the interactive shell would immediately pass it off to 'exec' to execute. ... So, on return from exec with an error status, the shell would fork a copy of itself to try and run the script. ... As a result of the above, it was hard to tell whether the script was a Bourne shell or C shell, so the convention was introduced of using the Bourne shell no op command, as the first line in a Bourne shell script. ...
    (Debian-User)
  • Re: awk here documents
    ... script called from shell to produce some pre-formatted blocks of code ... awk variable: ... or awk quotes with shell quotes ...
    (comp.unix.shell)
  • Re: misc/149335: shell script runs on Linux but not on freebsd
    ... to develop a smarter install script. ... Even if I correct the shell calls ... Looks like the Linux emulator is only 32 bit. ... I would like to see if I can get the x64 to install but if not I think the ...
    (freebsd-questions)