LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash scripting (https://www.linuxquestions.org/questions/linux-software-2/bash-scripting-466742/)

drum2jc 07-23-2006 06:27 AM

bash scripting
 
can someone tell me what i'm doing wring? i get an error: "line 35: syntax error: unexpected end of file"

Code:

#!/bin/bash

homedir=/home/$name/public_html

echo -e "\033[31m Enter the user you want to add to the system, ftp and webserver: \033[0m"

echo "Username:"
read name
echo "Password:"
read -s pass1
echo "Confirm Password:"
read -s pass2

pagehtml="<h1>Index page for $name</h1><h5>You should add some content to your public_html directory!</h5>"

if ["$pass1" = "$pass2"]; then

  echo "Passwords match... adding user to system."
  adduser $name<<PASS
  echo $pass1
  echo $pass2
  PASS

  mkdir $homedir
  chown $name $homedir
  chmod 755 $homedir
  echo $pagehtml > $homedir/index.php
  echo -e "\033[31m Webserver directory was created. \033[0m"
  echo -e "\033[31m $name was added to the system(ftp and webserver) \033[0m"
  exit 0
else
  echo "Passwords did not match. Please run the script again."
  exit 0
fi


thanks a bunch,
chris

homey 07-23-2006 07:21 AM

You could try #!/bin/bash -x so you can see what's happening.

This method of adding a user and password does seem to work but, I use a slightly different method.
Code:

  adduser $name<<PASS
  echo $pass1
  echo $pass2
  PASS

Code:

adduser ${NAME} -g ${NAME}
    (echo ${PASS1}; echo ${PASS2}) | passwd --stdin ${NAME} > /dev/null

Code:

#!/bin/bash

echo -e "\033[31m Enter the user you want to add to the system, ftp and webserver: \033[0m"

read -p "UserNAME:" NAME
read -p "Password:" PASS1
read -p "Confirm Password:" PASS2

pagehtml="<h1>Index page for $NAME</h1><h5>You should add some content to your public_html directory!</h5>"

if [ "$PASS1" = "$PASS2" ]; then
  echo "Passwords match... adding user to system."
    groupadd ${NAME} 2> /dev/null ##Change this if user needs a different group
    adduser ${NAME} -g ${NAME}
    (echo ${PASS1}; echo ${PASS2}) | passwd --stdin ${NAME} > /dev/null
    echo -n "Added linux user ${NAME}, "
    mkdir -p /home/$NAME/public_html
    homedir=/home/$NAME/public_html
    chown $NAME $homedir
    chmod 755 $homedir
  echo $pagehtml > $homedir/index.php
  echo -e "\033[31m Webserver directory was created. \033[0m"
  echo -e "\033[31m $NAME was added to the system(ftp and webserver) \033[0m"
  exit 0
else
  echo "Passwords did not match. Please run the script again."
  exit 0
fi


konsolebox 07-23-2006 07:35 AM

next time don't add whtie spaces to your "<<PASS ... PASS"
this should fix it:

Code:

#!/bin/bash

homedir=/home/$name/public_html

echo -e "\033[31m Enter the user you want to add to the system, ftp and webserver: \033[0m"

echo "Username:"
read name
echo "Password:"
read -s pass1
echo "Confirm Password:"
read -s pass2

pagehtml="<h1>Index page for $name</h1><h5>You should add some content to your public_html directory!</h5>"

if ["$pass1" = "$pass2"]; then

  echo "Passwords match... adding user to system."
  adduser $name<<PASS
  echo $pass1
  echo $pass2
PASS

  mkdir $homedir
  chown $name $homedir
  chmod 755 $homedir
  echo $pagehtml > $homedir/index.php
  echo -e "\033[31m Webserver directory was created. \033[0m"
  echo -e "\033[31m $name was added to the system(ftp and webserver) \033[0m"
  exit 0
else
  echo "Passwords did not match. Please run the script again."
  exit 0
fi


konsolebox 07-23-2006 07:41 AM

one more thing i kinda doubt this will work:

Code:

  adduser $name<<PASS
  echo $pass1
  echo $pass2
PASS

why not do this instead?

Code:

echo -e "$pass1\n$pass2" | adduser $name

spooon 07-23-2006 04:33 PM

this is kind of stupid
Code:

  adduser $name<<PASS
  echo $pass1
  echo $pass2
PASS

it will print out
Code:

  echo password
  echo password

including the word echo and two leading spaces

should be
Code:

  adduser $name<<PASS
$pass1
$pass2
PASS


fotoguy 07-23-2006 05:57 PM

Code:

    groupadd ${NAME} 2> /dev/null ##Change this if user needs a different group
    adduser ${NAME} -g ${NAME}
    (echo ${PASS1}; echo ${PASS2}) | passwd --stdin ${NAME} > /dev/null
    echo -n "Added linux user ${NAME}, "
    mkdir -p /home/$NAME/public_html
    homedir=/home/$NAME/public_html
    chown $NAME $homedir
    chmod 755 $homedir

Maybe cut down on a bit of code try something like this

Code:

groupadd ${NAME} 2> /dev/null ##Change this if user needs a different group
mkdir -p /home/$NAME/public_html
useradd ${NAME} -g ${NAME} -s /bin/sh -d /home/${NAME}/public_html -m -k /etc/skel
echo ${NAME}:${PASS1} | chpasswd

Then just place the index.php in the /etc/skel directory and when the user is created it will automatically copy all files in the /etc/skel directory to the users home directory and adjust all permissions to suit.

homey 07-23-2006 06:31 PM

Nice tip fotoguy!
Actually Fedora automatically creates the /home/username folder, grabs input from /etc/skel and adds the user to a group of same name as user.
So, I guess it could be cut down more for my FC5 boxen.
Code:

    useradd ${NAME}
    echo ${NAME}:${PASS1} | chpasswd
    echo -n "Added linux user ${NAME}, "
    mkdir -p /home/$NAME/public_html


fotoguy 07-24-2006 12:47 AM

Quote:

Originally Posted by homey
Nice tip fotoguy!
Actually Fedora automatically creates the /home/username folder, grabs input from /etc/skel and adds the user to a group of same name as user.

Thanks! Yeah most distros do, I know slackware doesn't automatically create the users home directory if you use the useradd by the command line unless you include the -m -k /etc/skel with it.


All times are GMT -5. The time now is 11:06 PM.