Question

Problem 1 Write a BASH script to create a user account from the Linux system on...

Problem 1
Write a BASH script to create a user account from the Linux system on which your script is run. The script should process two positional parameters.

  • First positional parameter is supposed to be a string with a user name (e.g., user_name)
  • Second positional parameter is supposed to be a string with a user password (e.g., user_password)

In your script:

  • Check if two positional parameters were passed to your script when it was invoked
    • If NOT, print an appropriate message and terminate your script with error (i.e., use exit 1)
    • If YES, continue processing both positional parameters
  • Create a new user on the Linux computer/system your script is run (you may have to run your script with root privileges or using sudo command to do it)
    • User’s account should have name denoted by the first positional parameter (e.g., user_name)
    • User’s account should have password denoted by the second positional parameter (e.g., user_password)
  • Check if a new directory (e.g., user_name) was crated for the user under /home/ directory
    • If NOT, print an appropriate message and terminate your script with error
    • If YES, continue to the next step
  • Check if the new user was added to the list of the users by checking /etc/passwd file (use grep command with user_name and find out if the line in it for the user was created)
    • If NOT, print an appropriate message and terminate your script with error
    • If YES, print an appropriate message that the user was successfully added and terminate your script with success (i.e., use exit 0)

Problem 2
Write a BASH script to delete a user account from the Linux system on which your script is run. The script should process one positional parameter.

  • The positional parameter is supposed to be a string with a user name (e.g., user_name)

In your script:

  • Check if a positional parameter was passed to your script when it was invoked
    • If NOT, print an appropriate message and terminate your script with error (i.e., use exit 1)
    • If YES, continue processing both positional parameters
  • Check if a user account for the user name that was passed to your scrip as a positional parameter has an account on the Linux computer/system your script is run by checking /etc/passwd file (use grep command with user_name and find out if the line in it for the user was created)
    • If NOT, print an appropriate message and terminate your script with error (i.e., use exit 1)
    • If YES, continue processing both positional parameters
  • Delete the user whose name was passed to your script as a positional parameter (e.g., user_name) from the Linux computer/system on which your script runs (you may have to run your script with root privileges or using sudocommand to do it)
  • Check if a user’s directory (e.g., user_name) was deleted by checking if it still is under /home/ directory
    • If YES, print an appropriate message and terminate your script with error
    • If NOT, continue to the next step
  • Check if a user’s account (e.g., user_name) was removed from the /etc/passwd file (use grep command with user_name and find out if the line in it for the user was created)
    • If YES, print an appropriate message and terminate your script with error
    • If NOT, print an appropriate message that the user was successfully removed and terminate your script with success (i.e., use exit 0)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#!/bin/bash

user_name=$1
user_password=$2

if [ -z "$user_name" ] || [ -z "$user_password" ]
then
   echo "usage: sudo bash adduser.sh <username> <password>"
   exit 1

elif [ $(id -u) -eq 0 ]; then
   egrep "^$user_name" /etc/passwd >/dev/null
   if [ $? -eq 0 ]; then
       echo "$user_name exists!"
       exit 1
   else
       pass=$(perl -e 'print crypt($ARGV[0], "password")' $user_password)
       useradd -m -p $pass $user_name
       [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
   fi
else
   echo "Only root may add a user to the system"
   exit 2
fi

homedir=$(getent passwd "$USER" | cut -d: -f6)
if [ $homedir != "/home/"$user_name ]
then
   echo "Home directory exists"
   exit 3
else
   grep $user_name /etc/passwd
fi

Let me know if its working for you so i create part 2 for account deletion script. thanks

Add a comment
Know the answer?
Add Answer to:
Problem 1 Write a BASH script to create a user account from the Linux system on...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command...

    LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command line arguments and does NOT use the read command. I will give you no credit if your bash script includes a read command. Also, you need to have at least one for..in loop in your code even if there may be other ways to do this problem. The script will go through a list of entries in the current directory (do not process subdirectories):...

  • Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment...

    Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment to tell us what you did, why and how. 3. allows a user to enter their name and a number between 1 than 100 (this must be prompted so the user knows what to do) 4. creates a random number between 1 and 100 for you to guess. The command to create a random number is shown below. (if you find a better one...use...

  • Write a bash script to protect a directory. Your script should notify a user of the...

    Write a bash script to protect a directory. Your script should notify a user of the following: -New file was created -Permission changes -Ownership changes -File size changes -Modification date changes Your script should have two operation modes, protect and check. Use command line flags, -p <directory> and -c <directory> to decide which mode to operate in. In protect mode, your script should create a file called .protect inside the directory you want to watch. You will use this file...

  • (In Linux) 1. Create a file to write to from your script and save it as...

    (In Linux) 1. Create a file to write to from your script and save it as “.txt”. 2. Write the following information to the file that you created in Step 1: Today's date Your full name Your student ID The name of your course What directory you are in File permissions of the file that you created that allow everyone to read, write, and execute The long list of files in your current directory, providing all items and their process...

  • using lubuntu ce the following activities on your Linux virtual machine. Then complete estions. Write a script file which when executed will perform the following: First display today's date in d...

    using lubuntu ce the following activities on your Linux virtual machine. Then complete estions. Write a script file which when executed will perform the following: First display today's date in dd.mm.YYYY format (e.g. 20.04.2019). Ask user to enter a name to create a directory. Create a directory by the given directory name above. . . Ask user to enter a name to create a file. Copy the file /etc/passwd to the given filename above inside the newly created directory Copy...

  • Login to Suse Linux as root. Complete the task working as the root user. 1. What...

    Login to Suse Linux as root. Complete the task working as the root user. 1. What is your current directory. What did you enter at the command prompt to determine your current working directory? 2. Change directories if needed to find your foods (or food) file. Use vi to make the file contain 10 food items. Save your file and quit. Use the cat command, and option, and the argument food to display the food file with numbered lines. Use...

  • Im trying to create a craps game on linux mint but im definitely not proficient with...

    Im trying to create a craps game on linux mint but im definitely not proficient with it. I'm a new student to this class and its done online. I seen a post that was already made with the exact question and requirements that I need to use for my script, but when I run the command it processes wrong, it goes like this... Name the script craps.sh The script will get 2 random numbers between 1-6. The script will display...

  • In some cases, people who are not strong in Linux may need to run specific Linux...

    In some cases, people who are not strong in Linux may need to run specific Linux commands. To make this easier, you can create a menu-driven program that runs these Linux commands. Such a program allows users to execute the commands without having to actually know about how to execute them. For this scenario, create a menu-driven program that has the following options: 1. List users who are logged in (using the who command) 2. List system information (using the...

  • Create a Windows batch script. The batch script will contain the following: Ask the user interactively:...

    Create a Windows batch script. The batch script will contain the following: Ask the user interactively: Boot drive letter of the computer Computer name Computer Model Check to see if the boot drive in #1 exists with the folder newfolder (#1 above) If yes cd BootDrive\newfolder If no, make the directory BootDrive\newfolder Check to see if casefile.txt exists: If yes goto the label DONE Tell the user that data already exists in the newfolder folder and exit the program Output...

  • In this exercise, you will create a script called file_ops.sh that uses the various file operators...

    In this exercise, you will create a script called file_ops.sh that uses the various file operators The file_ops.sh script will Use one command line argument Use four IF THEN statement to compare the first command line argument Determine which file operators to use that produces the following output when the script run with the arguments shown WARNING: In the IF THEN statements, to avoid syntax error messages, there must be a space before and after each bracket and also between...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT