Bash Shell Script
No comment
Download Count: 318
Date Added: Sunday, 03-Jul-11 15:51:32 CDT
Tags: bash shell script, add user, samba, samba user, add samba user, linux, command line
Bash shell script to add a system user and Samba user from the command line.
This is a scaled down bash script that I have found useful over the years.
Script will prompt you for the user name, check if it already exists, and if not, will ask for password, then add the system user and samba user to the machine. Also creates home directory.
Must be run as root!!
Can easily be extended to do other initial user setup chores.
Make sure the file is executable (chmod 755 add_user.sh).
File Name: add_user.sh - Code Type: Bash
#!/bin/bash # Script to add a user to Linux system and set up samba user. basedir=/home usershell=/bin/bash ## Declare all the functions first ## function to check if user already exists on system ifUserExits(){ grep $1 /etc/passwd > /dev/null [ $? -eq 0 ] && return $TRUE || return $FALSE } ## function to create the user with default shell and home directory passed from below createNewUser(){ if /usr/sbin/useradd "$@" then echo "User $6 Added" fi } ## function to set the user password createPassword(){ if echo -e "$1\n$1\n" | /usr/bin/passwd $2 then echo "Password Created for User $2" fi } ## function to setup samba user createNewSambaUser(){ if (echo $1; echo $1) | /usr/bin/smbpasswd -as $2 then echo "Samba Account for User $2 Added" fi } ## make sure root is running script if [ $(id -u) -ne 0 ] then echo "You must be root to run this script!" exit 2 fi ## get username from input read -p "Enter username : " username ## if user doesn't exist, add user if ( ! ifUserExits $username ) then ## get password from input read -p "Enter password : " password createNewUser -m -b $basedir -s $usershell $username createPassword $password $username createNewSambaUser $password $username else ## oops, username already being used echo "Username \"$username\" already exists" exit 3 fi
Parsed in 0.063 seconds - Rate: 20.91 KB/s - GeSHi version: 1.0.8.10
Hope you find this helpful! If you have any questions, please leave a comment.