Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

shell script crud

#! /bin/bash
 
GREP=/bin/grep
 
createUser()
{
	echo "Enter Username: "
	read name
 
	$GREP -i $name /etc/passwd
 
	if [ $? == 0 ]
	then
		echo "$name, is already a user"
	else
		echo "Preparing to create..."
		sleep 1
		clear
		echo "Enter a comment for $name"
		read comment
		sudo useradd -c "$comment" -m -s /bin/bash $name
		echo "User created!"
		less /etc/passwd | grep $name 
		echo "Please supply a password for $name"
		sudo passwd $name
	fi
}
 
modUser()
{
	clear
	echo "Which user would you like to modify?"
	read modName
 
	$GREP -i $modName /etc/passwd
 
	if [ $? == 0 ]
	then
		echo "Would you like to:"
		echo "1) Add a comment for $modName"
		echo "2) Modify home directory"
		echo "3) Add an expiration date (yyyy-mm-dd)"
		echo "4) Exit"
 
		read answer
		case "$answer" in
			1) echo "Enter comment: ";
			read comment
			sudo usermod -c "$comment" $modName;
			less /etc/passwd | grep $modName;;
			2) echo "Enter new home directoy [path]";
			read homeDir
			sudo usermod -d $homeDir $modName;
			less /etc/passwd | grep $modName;;
			3) echo "Enter expiration date";
			read expiration
			sudo usermod -e $expiration $modName;
			sudo chage -l $modName;;
			4) exit;;
		esac
	else
		echo "$modName does not exist!"
	fi
}
 
deleteUser()
{
	clear
	echo "Which user would you like to delete?"
	read delName
 
	$GREP -i $delName /etc/passwd
 
	if [ $? == 0 ]
	then
		echo "Are you sure you want to delete $delName ? [y/n]"
		read answer
		if [ $answer == "y"]
		then
			sudo userdel -r $delName
			echo "deleting..."
			sleep 1
			echo "$delName deleted!"
			exit 0
		fi
	else
		echo "$delName does not exist!"
	fi
 
}
 
clear
echo "MENU"
echo ""
echo "1) Create User"
echo "2) Modify User"
echo "3) Delete User"
echo "4) Exit"
 
read answer
 
case "$answer" in
	1) createUser;;
	2) modUser;;
	3) deleteUser;;
	4) exit;;
esac
Comment

PREVIOUS NEXT
Code Example
Shell :: linux cut all but last field 
Shell :: lldb for mac 
Shell :: Filter in terminal 
Shell :: run complex commands with sudo 
Shell :: rollup scss .d.ts 
Shell :: passphrase 
Shell :: login message on mac 
Shell :: How to Install and Configure doctl Github Download (Linux, MacOS) 
Shell :: installing appwrite on docker 
Shell :: Install heroicons npm install @headlessui 
Shell :: aws cli last successful deploy 
Shell :: pinch on linux 
Shell :: msysgit display end 
Shell :: aws php install sendmail fedora 
Shell :: linux .filename meaning 
Shell :: how to install jupyter notebook in windows 10 
Shell :: details of a long format in linux and unix based OS 
Shell :: sls install plugin 
Shell :: cut command in powershell windows 
Shell :: ta-lib linux 
Shell :: prevent mysql from running on every boot or restart 
Shell :: exec format error heroku dockerfile M1 chip 
Shell :: Installing Atom in Ubuntu OS 
Shell :: push to an existing branch 
Shell :: install nheko 
Shell :: threshold 
Shell :: install arch.univariate import arch_model 
Shell :: The zip extension must be loaded 
Shell :: insert csv to hive table 
Shell :: Error getting SSL certificate "default/tls-secret": local SSL certificate default/tls-secret was not found. Using default certificate 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =