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 the bash script Write a script compress_large_files.sh. This script accepts one or more command line...

    write the bash script Write a script compress_large_files.sh. This script accepts one or more command line arguments. The first argument has to be an integer; let’s call it size. If this is the only command line argument, compress_large_files.sh inspects all files in the current working directory and compresses every file of size at least size. If there is more than one command line argument, all arguments except the first one must be valid directories. In this case, compress_large_files.sh inspects the...

  • 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...

  • Write a script in MATLAB that prompts the user to do a, b, & c. a.)script...

    Write a script in MATLAB that prompts the user to do a, b, & c. a.)script that prompts the user to enter the day of the week using the input function. If the information the user enters is equal to “Saturday” or “Sunday”, display “have a nice weekend” in the Command Window. Otherwise, display “Good luck in your class lectures and your studies.” b.)script that prompts the user to input the following array by providing an example in the prompt:...

  • 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...

  • In LINUX Q1 A) Create a user with name “user1” from root directory. B) Change “user1”...

    In LINUX Q1 A) Create a user with name “user1” from root directory. B) Change “user1” shell to “/bin/bash” from root directory. C) Delete the user “user1” from root directory. D) Set a password for “user2” from local user directory. E) Add “user2” to group “java”. Q2: Write a menu script that has 3 option: a) Option 1: list all file in current directory. b) Option 2: ask user enter a number n, and print 1 to n to screen...

  • 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...

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