Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
> Shell Script
netcom88
post Aug 20 2008, 11:24 AM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 1
Joined: 20-August 08
Member No.: 13,623



I'm still in the process of learning how to write shell scripts. I have a script for adding a user to the system ... Could anyone help me break down this script and explain what each lines does? I would greatly appreciate it!

if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies
michaelk
post Aug 31 2008, 02:25 AM
Post #2


Its GNU/Linuxhelp.net
*******

Group: Support Specialist
Posts: 1,796
Joined: 23-January 03
Member No.: 360



A good place to start is to read advanced bash programming. http://tldp.org/LDP/abs/html/ It will explain the fundamentals of bash programming.
search for conditional statements i.e. if then else,
input statements i.e. read
and
environment variables i.e. #?

In a nutshell.
adduser is a utility to add a user. It can only be run as root and root's id is always 0 (zero).
egrep is a pattern matching console utility. In this case it is used to find if a user already exists.
perl is a shell language and crypt is a perl built in DES encryption function. In this case it is encrypting the password.
Go to the top of the page
 
+Quote Post

Posts in this topic
- netcom88   Shell Script   Aug 20 2008, 11:24 AM
- - michaelk   A good place to start is to read advanced bash pro...   Aug 31 2008, 02:25 AM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 17th October 2017 - 03:22 PM