Operating System - HP-UX
1834771 Members
3318 Online
110070 Solutions
New Discussion

Shell Scripts & Functions()

 
SOLVED
Go to solution
Amit Dixit_2
Regular Advisor

Shell Scripts & Functions()

Hi,
I have written a script which checks whether
a group exists or not if not then
create it

#Script
if ! grep "^informix:" /etc/group
> /dev/null 2>&1
then
groupadd informix
else
echo "\n'informix' group already exists"
fi

If the above script is executed as it is
then it runs fine but if i write
the same script under a function

#Script
grp_chk()
{
if ! grep "^informix:" /etc/group
> /dev/null 2>&1
then
groupadd informix
else
echo "\n'informix' group already exists"
fi
}

The above script does not do any
anything can somebody please help me
with this.

Thanks
Amit
10 REPLIES 10
Geoff Wild
Honored Contributor

Re: Shell Scripts & Functions()

You have to call the function:

function grp_chk
{
if ! grep "^informix:" /etc/group
> /dev/null 2>&1
then
groupadd informix
else
echo "\n'informix' group already exists"
fi
}

grp_chk

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Amit Dixit_2
Regular Advisor

Re: Shell Scripts & Functions()

Geoff,
Ok, I did that still not doing anything.

How to call the function.

Thanks,
Amit
Geoff Wild
Honored Contributor
Solution

Re: Shell Scripts & Functions()

Works for me:

# ./grp_chk

'informix' group already exists


# cat grp_chk
#!/bin/sh
function grp_chk
{
if ! grep "^informix:" /etc/group > /dev/null 2>&1
then
groupadd informix
else
echo "\n'informix' group already exists"
fi
}

grp_chk


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Bharat Katkar
Honored Contributor

Re: Shell Scripts & Functions()

Amit,
Just call the function with it's name in your script e.g.

grp_chk

Make sure you call the function after function definition.

hope that works.
Regards,
You need to know a lot to actually know how little you know
Bill Hassell
Honored Contributor

Re: Shell Scripts & Functions()

A couple of things:

First, always start your scripts with the required interpreter, in your case:

#!/usr/bin/sh

That will ensure that the script will always be run by the correct shell interpreter. Second, when a script doesn't work, always trace it with set -x. Since you have a function, the set -x must also be placed inside all the functions too. It's a good idea to put an explicit return at the end of a function even though it is implied with the closing }. Also, grep has the ability to just return OK or not found by using the -q option (quiet) so your script would look like this:

#!/usr/bin/sh


grp_chk()
{
set -x
if ! grep -q "^informix:" /etc/group
then
groupadd informix
else
echo "\n'informix' group already exists"
fi
return
}

set -x
grp_chk

You call functions by simply using their names.


Bill Hassell, sysadmin
Amit Dixit_2
Regular Advisor

Re: Shell Scripts & Functions()

Hi,
Thanks to all of you

One more query
Now, When I am adding user informix
I want my script to automatically set
password for the user rather asking

what should I do.

Thanks,
Amit
RAC_1
Honored Contributor

Re: Shell Scripts & Functions()

User /usr/lbin/usermod-.sam -p `echo "123abctexy"|/usr/bin/makekey` user_id

This will set password 123abcte for user user_id (the chars - xy are salt chars required for encryption.)

Anil
There is no substitute to HARDWORK
Amit Dixit_2
Regular Advisor

Re: Shell Scripts & Functions()

Hi,
Is there any other way to set user passwd
using shell script..

Thanks,
Amit
Amit Dixit_2
Regular Advisor

Re: Shell Scripts & Functions()

Hi,
How can I show a progress-bar when my
script is extracting tar file any ready
code for the same ??

Thanks
Amit
Bill Hassell
Honored Contributor

Re: Shell Scripts & Functions()

The password must be generated for usermod.sam -p. You can use the following code to create an encrypted password:

# Create the encrypted password. We'll use awk to generate
# two random characters for SALT. makekey requires exactly
# 8 characters for password and 2 salt characters. For a
# password less than 8, pad with nulls using typeset -L8
# for PASSWD and tr to change spaces to nulls.

SALT=$( awk '
BEGIN {
CHARS="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
srand()
printf("%s%s", substr(CHARS,1 + int(rand() * 64),1), \
substr(CHARS,1 + int(rand() * 64),1))
} ')

# makekey requires exactly 8 characters with shorter passwords
# filled with nulls. We'll use tr to perform this task by assigning
# the password to a typeset -L8 field and replacing all spaces with
# nulls. Note that because a null signals the end of a string, the
# null-filled password must be passed directly to makekey--it will
# not survive an assignment to a variable.

ENCRYPTED=$(echo "$PASSWD$SALT" | tr -A " " "\000" | makekey)

Then pass $ENCRYPTED to usermod.sam -p $ENCRYPTED user-login

For tar, always use the -v option (as in tar xvf /someFile) to watch the files being extracted. There is no way to add a progress bar for large files (hundreds of megs) without modifying the tar source code.


Bill Hassell, sysadmin