Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Functions/Procedures with BASH?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
tdm
Tux's lil' helper
Tux's lil' helper


Joined: 06 May 2002
Posts: 117
Location: The Netherlands

PostPosted: Mon Jan 20, 2003 12:11 pm    Post subject: Functions/Procedures with BASH? Reply with quote

I've created a little BASH script which makes it possible to let a user shutdown or reboot the PC, oke, that's easy and it works great.

But now I want to make it possible that the root user can set some settings like:
- users of which group should be able to open the menu (by groupname)
- which users should be able to open the menu (by username)

I'm a newbie in BASH, but I've worked a lot with Pascal and Delphi.
Now my question, is it possible in BASH to make a function/procedure which results a true or false?

I tried it like this:
Code:
#!/bin/bash
grp="users wheel"

mygroups=`groups`

ingroup(var) {
   for i in $grp
   do
        if [ $i = $var ]
        then
              result true
        fi
   done

for x in $mygroups
do
      if [ ingroup($x) = true ]
      then
           echo "You're a member of the $x group, and have access"
      else
           echo "You could be a member of the $x group, but for now you don't have access"
      fi


But when I execute the script I get an error like:
.. line 9: syntax error near unexpected token `var'
.. line 9: `ingroup(var) {'

What am I doing wrong?

Thanks
Back to top
View user's profile Send private message
kutsuya
Retired Dev
Retired Dev


Joined: 21 Oct 2002
Posts: 189

PostPosted: Mon Jan 20, 2003 1:38 pm    Post subject: Re: Functions/Procedures with BASH? Reply with quote

tdm wrote:


I tried it like this:
Code:
#!/bin/bash
grp="users wheel"

mygroups=`groups`

ingroup(var) {
   for i in $grp
   do
        if [ $i = $var ]
        then
              result true
        fi
   done

for x in $mygroups
do
      if [ ingroup($x) = true ]
      then
           echo "You're a member of the $x group, and have access"
      else
           echo "You could be a member of the $x group, but for now you don't have access"
      fi


But when I execute the script I get an error like:
.. line 9: syntax error near unexpected token `var'
.. line 9: `ingroup(var) {'


Hi,

I would suggest the following changes:

Code:

...
ingroup() {
   for i in $grp
   do
        if [ $i = $1 ]
        then
              result true
        fi
   done
...


A online reference is 'info bash' . Here is a short example using function parameters:

Code:

#!/bin/bash

myfunc()
{
    echo $1 $2
    return 5
}


myfunc $1 $2 $3
echo "return is $?"
shift ; shift
myfunc $1 $2


Code:

kutsuya tmp # chmod o+x test
kutsuya tmp # ./test "This" "Is" "a" "test"
This Is
return is 5
a test


I had similar problems when I first started bash also. :lol:

Hope that helps,
--Kutsuya
Back to top
View user's profile Send private message
tdm
Tux's lil' helper
Tux's lil' helper


Joined: 06 May 2002
Posts: 117
Location: The Netherlands

PostPosted: Mon Jan 20, 2003 1:48 pm    Post subject: Reply with quote

Thanks, I gonna play with it :D

Edit: Awsome, it works perfectly :lol:
Back to top
View user's profile Send private message
kutsuya
Retired Dev
Retired Dev


Joined: 21 Oct 2002
Posts: 189

PostPosted: Mon Jan 20, 2003 3:54 pm    Post subject: The joys of BASH Reply with quote

tdm wrote:
Thanks, I gonna play with it :D

Edit: Awsome, it works perfectly :lol:


You are welcome. Glad it worked. Have fun. :D
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum