title: Pass root user & password in useradd shell script
body: <p>I have created a shell script to add ubuntu sftp user, and from root terminal i am able to create user successfully but i want to pass root user &amp; password in this script so that from anywhere i can execute this command. I have set root password on my server.
  So can anyone help me that how can i add if $USER root and $PASSWORD option in my script?</p>
  
  <p>Below is my script:</p>
  
  <pre><code>#!/bin/bash
  SFTPONLY_GROUP="sftponly"
  SFTP_BASE_FOLDER="/srv/sftp"
  UPLOAD_DIR="htdocs"
  
  USERID="$1"
  
  # Check parameters
  if [ "$1" = "" ]
  then
  echo "Usage: $0 &lt;new username&gt;" 1&gt;&amp;2
  exit 1
  fi
  
  # Check if root
  #if [ "$(id -u)" != "0" ]; then
  #   echo "This script must be run as root" 1&gt;&amp;2
  #   exit 1
  #fi
  
  # Make sure the group exists
  /bin/egrep  -i "^${SFTPONLY_GROUP}" /etc/group
  if [ $? -eq 0 ]; then
      echo "Nice, group $SFTPONLY_GROUP already exists in /etc/group"
  else
      echo "Group $SFTPONLY_GROUP does not exist, creating..."
      groupadd $SFTPONLY_GROUP
  fi
  
  # Make sure the user does not exist
  /bin/egrep  -i "^${USERID}" /etc/passwd
  if [ $? -eq 0 ]; then
      echo "User $USERID exists in /etc/passwd, aborting..."
      exit 1
  else
      echo "Good, $USERID is a new user."
  
      if [ -d "$SFTP_BASE_FOLDER/$USERID" ]; then
              echo "Folder $SFTP_BASE_FOLDER/$USERID already exists, 
  aborting..."
              exit 1
      else
              echo "Adding user..."
              useradd $USERID
  
              echo "Adding password..."
              passwd $USERID
  
              echo "Creating folder $SFTP_BASE_FOLDER/$USERID..."
              mkdir $SFTP_BASE_FOLDER/$USERID
  
              echo "Setting home directory of the new user..."
              usermod -d / $USERID
  
              echo "Assigning $USERID to $SFTPONLY_GROUP..."
              usermod -G $SFTPONLY_GROUP $USERID
  
              echo "Setting necessary permissions for chroot folder..."
              chmod -R 755 $SFTP_BASE_FOLDER/$USERID
  
  
              echo "Creating directory structure..."
              mkdir $SFTP_BASE_FOLDER/$USERID/$UPLOAD_DIR
              chown $USERID:$SFTPONLY_GROUP 
  $SFTP_BASE_FOLDER/$USERID/$UPLOAD_DIR
  
              echo "Done."
      fi
  fi
  </code></pre>