Results 1 to 4 of 4
I am currently doing a task for creating users from the command prompt. it is a bash script task with menus etc. now i have created the users and groups ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 03-14-2006 #1
- Join Date
- Aug 2005
If statment help
I am currently doing a task for creating users from the command prompt. it is a bash script task with menus etc. now i have created the users and groups fine and it works but here is my problem.
when it prompts me to enter a username and a group and to create the home directory i need to write an if statement that basically says if it exists then don't worry just use it, but if not then create it. here is my script below.
# Tests to see if root is logged on, if not then message displayed that root muct be logged on
if [ $whoami != "root" ]; then
echo "You must be root to add new users"
echo "Add Users Menu"
echo "1) Add User From File"
echo "2) Add User Manually"
echo "3) Exit to Main Menu"
echo -n "Please select a option (1,2,3): "
case "$startmenu" in
echo "Enter Code"
# Variables entered to create user details
echo -n "Please Enter Persons Fullname: "
echo -n "Please Enter Year: "
echo -n "Please Enter Group: "
echo -n "Please Enter Username: "
# Creates a directory from the Year variable
# Creates a directory from the Group variable
# Uses the Groupadd variable to create the group from the group variable
# Uses the Useradd variable to create the user and directories
$USERADD -d "/home/$Year/$Group/$Username" -g "$Group" -m $Username
# Exits back to main menu
# Any key other then 1,2,3 will return invalid option
echo "Invalid Option"
- 03-14-2006 #2
First things first: next time, please but your code in a code section, like this:
There are two ways you can go about this.
You can try something like this:
if [ -e /home/$username]; then ...
mkdir /home/$username 2> /dev/nullFlies of a particular kind, i.e. time-flies, are fond of an arrow.
Registered Linux User #408794
- 03-15-2006 #3
I would use "else"
Here is an example of an "else" and "if" statement using true and false.
if (true) then
echo "true is true"
echo "true is false"
You can also use the elif command ... but only in the Bourne(sh) shell.
- 03-15-2006 #4Code:
mkdir /home/$username 2> /dev/null
if [-e /home/$username]; then <code> else <code> fi OR if [! -e /home/$username]; then <code> fi (depending on what was needed...)