- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Shell Scripts & Functions()
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 03:49 AM
06-30-2004 03:49 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 03:52 AM
06-30-2004 03:52 AM
Re: Shell Scripts & Functions()
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 03:58 AM
06-30-2004 03:58 AM
Re: Shell Scripts & Functions()
Ok, I did that still not doing anything.
How to call the function.
Thanks,
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 04:05 AM
06-30-2004 04:05 AM
Solution# ./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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 04:07 AM
06-30-2004 04:07 AM
Re: Shell Scripts & Functions()
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 04:14 AM
06-30-2004 04:14 AM
Re: Shell Scripts & Functions()
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 04:27 AM
06-30-2004 04:27 AM
Re: Shell Scripts & Functions()
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 07:44 AM
06-30-2004 07:44 AM
Re: Shell Scripts & Functions()
This will set password 123abcte for user user_id (the chars - xy are salt chars required for encryption.)
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 07:07 PM
06-30-2004 07:07 PM
Re: Shell Scripts & Functions()
Is there any other way to set user passwd
using shell script..
Thanks,
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 01:56 AM
07-01-2004 01:56 AM
Re: Shell Scripts & Functions()
How can I show a progress-bar when my
script is extracting tar file any ready
code for the same ??
Thanks
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 02:15 AM
07-01-2004 02:15 AM
Re: Shell Scripts & Functions()
# 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