Sponsored Content
Homework and Emergencies Homework & Coursework Questions Create script to add user and create directory Post 302506513 by m1xram on Monday 21st of March 2011 04:24:30 AM
Old 03-21-2011
groupadd useradd

Couple of thoughts. Once you find that a department doesn't exist and create the directory for, it you may wish to create a group for it with 'groupadd'. Then set the GID to the newly created group.

A similar situation should be done for the user directory although you should create the directory with 'useradd'. 'useradd -g GROUP' will make sure the directory has the correct GID. Use the '-K UMASK=026' to make sure newly created files have the correct permissions. You'll have to look up how UMASK works. In this environment you may prefer 027 for more security.

A better way to handle the departments would be to create a list of valid existing departments. It would reduce spelling errors. The last option in the list could be "(N)ew department".

Code:
#!/bin/ksh
# root check code below
#deptroot=/home
deptroot=/home/wolverines
i=0
dept=()
echo "Department:"
find $deptroot -maxdepth 1 -type d | sort | while read x ;do
    bn=$(basename "$x");
    dept[$i]="$bn"
    i=$(($i + 1))
    echo "$i) $bn"
done
echo ""
echo -n "Enter number of department or 'n' for new department: "
read ans
echo "${dept[*]}"
if [[ "$ans" =~ ^[0-9]+$ ]]; then
    echo "Numeric $ans $i"
    if [ $ans -lt $i ]; then 
       echo "In range"
       d="${dept[$(($ans - 1))]}"
       echo "Department: $d"
    fi
elif [ "$ans" == "n" ]; then
    echo "Prompt for new department"
    # assign d=DEPARTMENT
    # make sure it doesn't exist.. if exists exit
    # groupadd $d
    # mkdir "$depthome/$d"
    # chgrp $d "$depthome/$d"
else
    echo "bad input"
    exit 1
fi

Output Example:
Code:
$ ./test.sh 
Department:
1) History
2) Math
3) PhysEd
4) Science
5) Zoology

Enter number of department or 'n' for new department: 3
History Math PhysEd Science Zoology
Numeric 3 5
In range
Department: PhysEd

Sorry but I had to use KSH as BASH has a bug with pipes and while loops spawning subprocesses. BASH can't see what happened in the loop after the loop.

Lastly I'd add a check for the user ID at the beginning of your script.
Code:
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

Only people with root access should be able to run any of this.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create file in each user directory

Hi, Im newbie, I wanna to create file in each user directory, how to make that script, maybe someone can give me example, im confusing coz i have to change form one user directory to other Thank U. (8 Replies)
Discussion started by: cleks
8 Replies

2. UNIX for Dummies Questions & Answers

how to write script to create directory

Please help. I am the beginner. Don't understand about archive file. How to create a directory for the files from each archive with name of directory which equivalent to the base name of the archive. eg I have file abc.txt. How can I create a directory name abc. Thank you (1 Reply)
Discussion started by: snail
1 Replies

3. Shell Programming and Scripting

create a new directory from cgi script

hello. i hav accepted name of directory from user through a form.now i need to create a directory under cgi-bin of that name.I am not able to do so.n help is required (12 Replies)
Discussion started by: raksha.s
12 Replies

4. Shell Programming and Scripting

How to create a directory inside root as different user

Hi All, I have directory under /opt/test. The ownership of the test directory is root:root. I have login to the server as test user. I need to have some script to create a directory inside /opt/test. This script will be called as test user. When I try to execute... (4 Replies)
Discussion started by: kalpeer
4 Replies

5. Solaris

create user with RWX access to a specific directory in Solaris 10

I need to create a user account for a developer that will allow him rwx access to all resources in a directory. How can I do that? Thanks (5 Replies)
Discussion started by: gsander
5 Replies

6. AIX

How to create new user and add group

Hello, I am new in AIX please tell how can i create user and add group in this user for example, i want to create user umair and want to add this user primanry group DBA and secondary group ORACLE,how can i do this please tell in detail Thanks, Umair (1 Reply)
Discussion started by: umair
1 Replies

7. Solaris

Unable to create or delete a directory in /usr with root user

Hi All, I am trying to uninstall jdk 1.5 from my Solaris 10 64 bit but some how was not successful.so tried to delete the folder of jdk from /usr but its throughing error as: Unable to remove directory jdk: Read-only file system Even I tried to create a dir in /usr but its not allowing me... (4 Replies)
Discussion started by: Pshah
4 Replies

8. Shell Programming and Scripting

Create a folder under different user directory

Hello All, I have to write a shell script and use it in informatica. The script has to perform below actions: The script gets executed from edw user. Through the script, a DT folder has to be created under edw_sca user. Is this scenario possible through a SHELL script or not. ... (2 Replies)
Discussion started by: bghosh
2 Replies

9. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies
SHTOOL-ECHO.TMP(1)					      GNU Portable Shell Tool						SHTOOL-ECHO.TMP(1)

NAME
shtool-echo - GNU shtool echo(1) extensional command SYNOPSIS
shtool echo [-n|--newline] [-e|--expand] string DESCRIPTION
shtool echo is an echo(1) style command which prints string to stdout and optionally provides special expansion constructs (terminal bold mode, environment details, date, etc) and newline control. The trick of this command is that it provides a portable -n option and hides the gory details needed to find out the environment details under option -e. OPTIONS
The following command line options are available. -n, --newline By default, output is written to stdout followed by a "newline" (ASCII character 0x0a). If option -n is used, this newline character is omitted. -e, --expand If option -e is used, string can contain special "%x" constructs which are expanded before the output is written. Currently the following constructs are recognized: %B switch terminal mode to bold display mode. %b switch terminal mode back to normal display mode. %u the current user name. %U the current user id (numerical). %g the current group name. %G the current group id (numerical). %h the current hostname (without any domain extension). %d the current domain name. %D the current day of the month. %M the current month (numerical). %m the current month name. %Y the current year. EXAMPLE
# shell script shtool echo -n -e "Enter your name [%B%u%b]: "; read name shtool echo -e "Your Email address might be %u@%h%d" shtool echo -e "The current date is %D-%m-%Y" HISTORY
The GNU shtool echo command was originally written by Ralf S. Engelschall <rse@engelschall.com> in 1998 for Website META Language (WML) under the name buildinfo. It was later taken over into GNU shtool. SEE ALSO
shtool(1), echo(1). 18-Jul-2008 shtool 2.0.8 SHTOOL-ECHO.TMP(1)
All times are GMT -4. The time now is 08:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy