View previous topic :: View next topic |
Author |
Message |
tdm Tux's lil' helper
Joined: 06 May 2002 Posts: 117 Location: The Netherlands
|
Posted: Mon Jan 20, 2003 12:11 pm Post subject: Functions/Procedures with BASH? |
|
|
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 |
|
|
kutsuya Retired Dev
Joined: 21 Oct 2002 Posts: 189
|
Posted: Mon Jan 20, 2003 1:38 pm Post subject: Re: Functions/Procedures with BASH? |
|
|
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.
Hope that helps,
--Kutsuya |
|
Back to top |
|
|
tdm Tux's lil' helper
Joined: 06 May 2002 Posts: 117 Location: The Netherlands
|
Posted: Mon Jan 20, 2003 1:48 pm Post subject: |
|
|
Thanks, I gonna play with it
Edit: Awsome, it works perfectly |
|
Back to top |
|
|
kutsuya Retired Dev
Joined: 21 Oct 2002 Posts: 189
|
Posted: Mon Jan 20, 2003 3:54 pm Post subject: The joys of BASH |
|
|
tdm wrote: | Thanks, I gonna play with it
Edit: Awsome, it works perfectly |
You are welcome. Glad it worked. Have fun. |
|
Back to top |
|
|
|