help using getopts in Solaris

help using getopts in Solaris

Post by hAWK » Sun, 29 Apr 2001 11:13:35



Hi all,

I wish to write a script using getopts to allow me to change permissions,
owner & group on a file or multiple files by supplying different options and
arguments to it.

I'm a newbie, so take it easy on me...haha ;-)

so far my code is as such:

I've actually called this script 'mychmod', same as the function name and
FPATH is set in my .profile file.

mychmod()
{
while getopts p:o:g: option            # p=permissions o=owner g=group
do
   case ${option} in
        p )    chmod_opt=${OPTARG};p_opt=yes;;
        o )    chown_opt=${OPTARG};o_opt=yes;;
        g )    chgrp_opt=${OPTARG};g_opt=yes;;
   esac
done

shift `expr ${OPTIND} - 1`


do
  test "${p_opt} = "yes" && chmod "${chmod_opt}" $file
  test "${o_opt} = "yes" && chown "${chown_opt}" $file
  test "${g_opt} = "yes" && chgrp "${chgrp_opt}" $file
done

Quote:}

if I test the script for syntax errors using 'ksh -n mychmod', there aren't
any errors.

#echo "this is a dummy file" > dummy

When I run the function, it spits out the following error:-
#mychmod -p 777 dummy
usage: chmod [-fR] <absolute-mode> file ...
           chmod [-fR] <symbolic-mode-list> file ...
where <symbolic-mode-list> is a comma-separated list of [ugoa] {+|-|=}
[rwxXlstugo]
Cannot open file

What am I missing? Or what does the above error mean?  Your help is very
much appreciated.
Thanks

 
 
 

help using getopts in Solaris

Post by Logan Sh » Sun, 29 Apr 2001 13:22:06



>mychmod()
>{
>while getopts p:o:g: option            # p=permissions o=owner g=group
>do
>   case ${option} in
>        p )    chmod_opt=${OPTARG};p_opt=yes;;
>        o )    chown_opt=${OPTARG};o_opt=yes;;
>        g )    chgrp_opt=${OPTARG};g_opt=yes;;
>   esac
>done

>shift `expr ${OPTIND} - 1`


>do
>  test "${p_opt} = "yes" && chmod "${chmod_opt}" $file
>  test "${o_opt} = "yes" && chown "${chown_opt}" $file
>  test "${g_opt} = "yes" && chgrp "${chgrp_opt}" $file
>done
>}

>if I test the script for syntax errors using 'ksh -n mychmod', there aren't
>any errors.

>#echo "this is a dummy file" > dummy

>When I run the function, it spits out the following error:-
>#mychmod -p 777 dummy
>usage: chmod [-fR] <absolute-mode> file ...
>           chmod [-fR] <symbolic-mode-list> file ...
>where <symbolic-mode-list> is a comma-separated list of [ugoa] {+|-|=}
>[rwxXlstugo]
>Cannot open file

Well, I can see one obvious thing wrong with your script.  You've
got unmatched quotes.  For example, instead of this:

        test "${p_opt} = "yes" && chmod "${chmod_opt}" $file

you should have this:

        test "${p_opt}" = "yes" && chmod "${chmod_opt}" $file

Although actually I would just do this instead:

        [ -n "$chmod_opt" ] && chmod "$chmod_opt" "$file"

It's not harmful, but it's not really necessary to use the braces in
this case -- you only need to use those as a separator if it's not
obvious where the variable ends, and to me it's sort of visual clutter
to use them when they're not necessary.

Also note the quotes around "$file".

Anyway, if the quoting issue isn't it (and I bet it's not), then one
easy way to proceed is to use the trace feature of the shell.  Do
this:

        set -x
        mychmod -p 777 dummy
        set +x

Then you'll see what the shell is doing at each step, and you can
figure out why it's creating a chmod command with bad syntax.

  - Logan
--
my  your   his  her   our   their   _its_
I'm you're he's she's we're they're _it's_

 
 
 

help using getopts in Solaris

Post by hAWK » Mon, 30 Apr 2001 09:38:42


Thanks for your help Logan.  The first double quotes missing was just a typo
in the post.  It was there in my script.  The problem was that I didn't
quote the $file variable.  It's working fine now.  Thanks again!!!
 
 
 

help using getopts in Solaris

Post by hAWK » Mon, 30 Apr 2001 10:50:29



>do
>  [ -n "$chmod_opt" ] && chmod "$chmod_opt" "$file"
>  [ -n "$chown_opt" ] && chown "$chown_opt" "$file"
>  [ -n "$chgrp_opt" ] && chgrp "$chgrp_opt" "$file"
>done

Actually guys, even the above does not work.  Do this instead.


words, change the above to:




 
 
 

1. passing args to getopts when getopts is embedded in functions

hello,

I hope that the subject line is descriptive enough.  What I'm trying
to do is basically this, parse command line arguments passed to the
script by getopts while getopts is nested two functions deep.  The
script is called with something similar to:

script -n arg_here -f "arg here too"

Parsing the options is easy if the call to getopts is NOT embedded in
functions.  Also, if I don't use multi-word arguments getopts has no
trouble regardless of where it sets.  Below is a stripped down version
of the script:

#! /usr/bin/ksh

main () {
  ParseArgs $*

ParseArgs () {
  while getopts d:t: FOO
  do case in $FOO
    d) do stuff ;;
    t) do more stuff ;;
  esac
  done

main $*

I'm sure all of you can see the problem already.  I've tried various
things like calling ParseArgs, and main, like so, main "$*" ParseArgs

Using the -x option to korn I was able to determine that my problem is
how the arguments are being passed, or at least seen, by the
functions.  Before the args are processed by the functions, they are
seen properly in the global area, if you will, of the script.
However, after being passed to the functions, the integrity of the
arguments is lost and, depending on how I pass them, they either
become one singe argument, or every word becomes and argument.

(This might be second nature to all of you, but it seems odd to me
that these functions should be passed arguments like this when their
not defined as taking arguments.)

Any help would be appreciated.  I'm sure the answer is simple, but
it's escaping me.  How do I prevent this from happening?

Andy

2. routing admin question for sol7

3. getopt Vs getopts

4. Slow NATD with FreeBSD4.0

5. getopts or getopt

6. ES40 Quad CPU 667Mhz Unix Unlimited for only 39000 USD

7. Compare file modification date using tcsh built-ins

8. mutt opens mailboxes in readonly mode

9. Stabbed US preacher flees INDIA state (The same INS tactics used against the stabbed preacher)

10. Plug-Ins using C++

11. How to test for mutually exclusive options using getopts (in shell)

12. Q: Using ksh93 getopts, can one do a string test against a regular expression?

13. spaces inside arguments while using getopt