39984

Create/delete users from text file using Bash script

Question:

I know this question has been asked a lot but I just can't seem to get it working and I'm kinda on the clock, okay so.

I'm trying to make a bash script with a 4 option menu: 1 Add single user 2 Add from list 3 Delete single user 4 Delete from list

Adding and deleting single users I've got working it's just from list that keep defeating me.

This is the code I've got so far

#!/bin/bash choice=5 # Main display echo "Enter number to select an option" echo echo "1) Add Single User" echo "2) Add Users from list" echo "3) Delete Single User" echo "4) Delete users from list" echo while [ $choice -eq 5 ]; do read choice if [ $choice -eq 1 ] ; then echo -e "Enter Username" read user_name echo -e "Enter Password" read user_passwd sudo useradd $user_name -m -p $user_passwd cat /etc/passwd else if [ $choice -eq 2 ] ; then x=0 created=0 echo -e "Enter file name for users:" read user_list sudo useradd "$user_list" -m -p "$user_list" else if [ $choice -eq 3 ] ; then cat /etc/passwd echo echo -e "User to be deleted:" read del_user sudo userdel -r $del_user cat /etc/passwd echo else if [ $choice -eq 4 ] ; then echo "Not yet finished" else echo "#####################" echo "# Stop being a noob #" echo "#####################" echo "1) Add Single User" echo "2) Add Users from list" echo "3) Delete Single User" echo "4) Delete users from list" echo choice=5 fi fi fi fi done

The file to get the users from is just

User20 User19 User18 User17 User16 User15 User14 User13 User12 User11 User10 User9 User8 User7 User6 User5 User4 User3 User2 User1

It's sloppy as hell I know but I just need it for a demo from a presentation assignment, any help is appreciated.

Thanks.

Answer1:

After you read in the name of the file you have to iterate through that file and create or delete user for each line of the file.

Example :

for user in `cat user_list` do sudo useradd "$user" -m -p "$user" done

Recommend

  • How to find duplicate field combo
  • Call a function that is in 'content_script.js' within the webpage
  • Should authentication be in a separate service for wcf?
  • Rails - Use a class method from a another class method with one attribute of the second class
  • Cannot select Infragistics controls in WinForms designer
  • Qt WebView and WebChannel over WebSockets in QML
  • R SparkR - equivalent to melt function
  • Error: packet sequence number wrong after upgrading RDS instance
  • svnserve and SASL problem
  • Javascript getting the value of an input in a table [duplicate]
  • Authorization Policies/Gates for Laravel 5.3 web app consuming own API w/ Passport
  • Migrating MOSS 2007 from SQL 2000 to SQL 2005 [closed]
  • Customize jQuery submenu positioning
  • Retrieving Application's DisplayName/UninstallString from Registry Using Powershell
  • Winston logger not write to file
  • How to add multiple legend titles (columns) in ggplot
  • jQuery how to translate livequery to on?
  • Identifying dates in strings using NLTK
  • Get name of days between two date in ios?
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • Does hibernate load two seprate copies of same instance if they are loaded twice from database?
  • How to redirect into different page by user type in php and mysql
  • Generate and export point cloud from Project Tango
  • Multiplying polynomials/simplifying like terms
  • DataTables move rows between tables
  • Terminal run dalvikvm with am.jar
  • How to handle div that is created dynamically in a table
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Make checkout phone field optional for specific countries in WooCommerce
  • Can someone explain this Java code (formatting the output using System.out.format) to me?
  • Codeigniniter insert data through models and controller
  • Sign a Pdf using custom digital signature in Java
  • Jersey serializes character value to ASCII equivalent numeric string
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • ReferenceError: TextEncoder is not defined
  • XSLT Transformation to validate rules in XML document