LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script at bottom of script - what am I missing (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-at-bottom-of-script-what-am-i-missing-4175424413/)

slufoot80 08-28-2012 09:44 AM

shell script at bottom of script - what am I missing
 
Below is the error I get when I run the program
=======================================================
/usr/sbin/adduser: line 111: syntax error near unexpected token `)'
/usr/sbin/adduser: line 111: ` n|N) exit'
=======================================================
Below this is a copy of my program
=======================================================
#Script to add a user to this Linux system
#!/bin/bash
clear

if [ $(id -u) -eq 0 ]; then

function security
{
chown $username:ftp $sourcedir
chmod 775 $sourcedir
}

function userinfo
{
read -p "Enter User Name : " username

while [ -z $username ]|| egrep "^$username" /etc/passwd 1>/dev/null;
do
echo -ne "Either user exists or you entered a blank, enter username again: ";read -e username
done

echo -ne "Enter your password: ";read -s password

while [ -z $password ];
do
echo -ne "\nEnter your password again: ";read -s -e password
done

echo -ne "\nPlease Enter your User ID Number: ";read -ern5 uid
while [[ ! $uid =~ ^[0-9]+$ ]]||egrep $uid /etc/passwd >/dev/null; do
echo -ne "Please re-enter your uid positive intergers only: ";read -ern5 uid
done

read -p "Enter a Comment : " comment
read -p "Enter Users Source Directory : " sourcedir
while [ ! -d "$sourcedir" ];
do
echo -ne "\n$sourcedir Directory Not Found! Please re-enter: "; read homedir
done
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) # passing the password entered
}

function shellsel
{
echo ""
echo "Select the type of shell you will be using"
echo""
echo -e "1) Bash Shell - SFTP Secure\n" # Shell selection statement
echo -e "2) False Shell - FTP Unsecure\n"
echo -ne "Enter choice: ";read shell;
case $shell in
1)
shell=/bin/bash # case statment for shell selection.
commentstatic="Internal SFTP Account"
useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
echo -e "Copying System Files ...."
cd /nas_ftp5/Customer/Troy/T_Skel
cp -Rp `ls` $sourcedir
echo -e "Finished Copying System Files ..."
tail -1 /etc/passwd > $sourcedir/etc/passwd
echo "$username" >> /etc/ftpusers
echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
2)
shell=/bin/false
commentstatic="Internal FTP Account"
useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
esac

}

echo -ne "\nIndividual Account or Environment Account\n\n"
echo -e "1) Individual Account\n"
echo -e "2) Environment Account\n"
echo -ne "Enter choice: ";read acctchoice;

case $acctchoice in
1)
userinfo
shellsel
security
;;
2)
userinfo
shellsel
cd /forms
ln -s $sourcedir /forms/$username"sa"
security

echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm
case $confirm in
y|Y) echo -ne "\nEnter ftp directory: ";read -e ftpdir

while [ ! -d "$ftpdir" ];
do
echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
done

usernameftp=$username"ftp"
useridftp=$userid
let $useridftp++
commentstatic2="Internal Secondary FTP Account"
shell=`egrep $username /etc/passwd| cut -d: -f7`
useradd -u $useridftp -c "$comment $commentstatic2" -d $ftpdir -s $shell $usernameftp

egrep $usernameftp /etc/passwd >> /etc/ftpusers
echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
;;
n|N) exit
esac
fi

szboardstretcher 08-28-2012 09:55 AM

Missing ;;

Code:

n|N) exit
;;
esac
fi


slufoot80 08-28-2012 10:06 AM

missing from where
 
it is right in the bottom of my script

grail 08-28-2012 10:11 AM

If you place your code in [code][/code] tags it will be much easier to read and help find your issue.

And to add to the above, you appear to be missing an 'esac' for one of your case statements at the bottom.

I am also curious as to why you would only create the functions if root is running the script?

szboardstretcher 08-28-2012 10:14 AM

Quote:

Originally Posted by slufoot80 (Post 4766505)
it is right in the bottom of my script

You don't have a double semi-colon after "exit"

slufoot80 08-28-2012 10:22 AM

ok I fixed the case issue
 
by adding a "esac" to the bottom of the file

=======================================================================================
108 egrep $usernameftp /etc/passwd >> /etc/ftpusers
109 echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
110 egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
111 n|N) exit
112 ;;
113 esac
114 esac
115 fi
=======================================================================================
but I still get this error:

/usr/sbin/adduser: line 111: syntax error near unexpected token `)'
/usr/sbin/adduser: line 111: ` n|N) exit'

grail 08-28-2012 11:12 AM

Try placing set -xv as second line in file and see what the interpreter is doing.

slufoot80 08-28-2012 03:39 PM

script fails at: " echo -ne "Would you like to create"
 
the script exit's when asked the question to create secondary account around line 92, and doesn't read the rest of the file.
==================================================================================================== ============================================
#Script to add a user to this Linux system
#!/bin/bash
clear

if [ $(id -u) -eq 0 ]; then

function security
{
chown $username:ftp $sourcedir
chmod 775 $sourcedir
}

function userinfo
{
read -p "Enter User Name : " username

while [ -z $username ]|| egrep "^$username" /etc/passwd 1>/dev/null;
do
echo -ne "Either user exists or you entered a blank, enter username again: ";read -e username
done

echo -ne "Enter your password: ";read -s password

while [ -z $password ];
do
echo -ne "\nEnter your password again: ";read -s -e password
done

echo -ne "\nPlease Enter your User ID Number: ";read -ern5 uid
while [[ ! $uid =~ ^[0-9]+$ ]]||egrep $uid /etc/passwd >/dev/null; do
echo -ne "Please re-enter your uid positive intergers only: ";read -ern5 uid
done

read -p "Enter a Comment : " comment
read -p "Enter Users Source Directory : " sourcedir
while [ ! -d "$sourcedir" ];
do
echo -ne "\n$sourcedir Directory Not Found! Please re-enter: "; read homedir
done
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) # passing the password entered
}

function shellsel
{
echo ""
echo "Select the type of shell you will be using"
echo""
echo -e "1) Bash Shell - SFTP Secure\n" # Shell selection statement
echo -e "2) False Shell - FTP Unsecure\n"
echo -ne "Enter choice: ";read shell;
case $shell in
1)
shell=/bin/bash # case statment for shell selection.
commentstatic="Internal SFTP Account"
useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
echo -e "Copying System Files ...."
cd /nas_ftp5/Customer/Troy/T_Skel
cp -Rp `ls` $sourcedir
echo -e "Finished Copying System Files ..."
tail -1 /etc/passwd > $sourcedir/etc/passwd
echo "$username" >> /etc/ftpusers
echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
2)
shell=/bin/false
commentstatic="Internal FTP Account"
useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
esac

}

echo -ne "\nIndividual Account or Environment Account\n\n"
echo -e "1) Individual Account\n"
echo -e "2) Environment Account\n"
echo -ne "Enter choice: ";read acctchoice;

case $acctchoice in
1)
userinfo
shellsel
security
;;
2)
userinfo
shellsel
cd /forms
ln -s $sourcedir /forms/$username"sa"
security

echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm
case $confirm in
[y|Y]* ) echo -ne "\nEnter ftp directory: ";read -e ftpdir

while [ ! -d "$ftpdir" ];
do
echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
done

usernameftp=$username"ftp"
useridftp=$userid
let $useridftp++
commentstatic2="Internal Secondary FTP Account"
shell=`egrep $username /etc/passwd| cut -d: -f7`
useradd -u $useridftp -c "$comment $commentstatic2" -d $ftpdir -s $shell $usernameftp

egrep $usernameftp /etc/passwd >> /etc/ftpusers
echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
;;
[n|N]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
esac
fi

chrism01 08-28-2012 06:01 PM

1. use code tags as requested above
https://www.linuxquestions.org/quest...do=bbcode#code

2. change top of file to
Code:

#!/bin/bash
set -xv

as requested above; it will show you what the parser is doing and help you debug

3.
Code:

# don't use this concat technique
$username"sa"

# use
${username}sa


slufoot80 08-29-2012 02:08 PM

fails at second case statement
 
Below is where my script fails it works until it get to this point, when I hit the case statement it fails why
==================================================================================================== ============================================
Would you like to create a Secondary ftp account? (y/n):y
Please answer yes or no.
==================================================================================================== ============================================
here is the code



echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm
case $confirm in
[y|Y]* ) echo -ne "\nEnter ftp directory: ";read -e ftpdir

while [ ! -d "$ftpdir" ];
do
echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
done

usernameftp=$username"ftp"
useridftp=$userid
let $useridftp++
commentstatic2="Internal Secondary FTP Account"
shell=`egrep $username /etc/passwd| cut -d: -f7`
useradd -u $useridftp -c "$comment $commentstatic2" -d $ftpdir -s $shell $usernameftp

egrep $usernameftp /etc/passwd >> /etc/ftpusers
echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
;;
[n|N]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
esac
fi

pan64 08-29-2012 02:33 PM

case 2) is not closed by ;; (of the outer case).
so you need ;; in between the two esac lines (at the end of the script)

and please try to use [code][/code] to insert your code to keep formatting...

slufoot80 08-29-2012 03:10 PM

I inserted that and it did the same thing

Code:

#!/bin/bash -xv

clear

if [ $(id -u) -eq 0 ]; then

function security
{
chown $username:ftp $sourcedir
chmod 775 $sourcedir
}

function userinfo
{
read -p "Enter User Name : " username

while [ -z $username ]|| egrep "^$username" /etc/passwd 1>/dev/null;
do
echo -ne "Either user exists or you entered a blank, enter username again: ";read -e username
done

echo -ne "Enter your password: ";read -s password

while [ -z $password ];
do
echo -ne "\nEnter your password again: ";read -s -e password
done

echo -ne "\nPlease Enter your User ID Number: ";read -ern5 uid
while [[ ! $uid =~ ^[0-9]+$ ]]||egrep $uid /etc/passwd >/dev/null; do
echo -ne "Please re-enter your uid positive intergers only: ";read -ern5 uid
done

read -p "Enter a Comment : " comment
read -p "Enter Users Source Directory : " sourcedir
while [ ! -d "$sourcedir" ];
do
echo -ne "\n$sourcedir Directory Not Found! Please re-enter: "; read homedir
done
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) # passing the password entered
}

function shellsel
{
echo ""
echo "Select the type of shell you will be using"
echo""
echo -e "1) Bash Shell - SFTP Secure\n" # Shell selection statement
echo -e "2) False Shell - FTP Unsecure\n"
echo -ne "Enter choice: ";read shell;
case $shell in
1)
shell=/bin/bash # case statment for shell selection.
commentstatic="Internal SFTP Account"
useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
echo -e "Copying System Files ...."
cd /nas_ftp5/Customer/Troy/T_Skel
cp -Rp `ls` $sourcedir
echo -e "Finished Copying System Files ..."
tail -1 /etc/passwd > $sourcedir/etc/passwd
echo "$username" >> /etc/ftpusers
echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
2)
shell=/bin/false
commentstatic="Internal FTP Account"
useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
esac

}

echo -ne "\nIndividual Account or Environment Account\n\n"
echo -e "1) Individual Account\n"
echo -e "2) Environment Account\n"
echo -ne "Enter choice: ";read acctchoice;

case $acctchoice in
1)
userinfo
shellsel
security
;;
2)
userinfo
shellsel
cd /forms
ln -s $sourcedir /forms/$username"sa"
security

echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm
case $confirm in
[y|Y]* ) echo -ne "\nEnter ftp directory: ";read -e ftpdir

while [ ! -d "$ftpdir" ];
do
echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
done

usernameftp=$username"ftp"
useridftp=$userid
let $useridftp++
commentstatic2="Internal Secondary FTP Account"
shell=`egrep $username /etc/passwd| cut -d: -f7`
useradd -u $useridftp -c "$comment $commentstatic2" -d $ftpdir -s $shell $usernameftp

egrep $usernameftp /etc/passwd >> /etc/ftpusers
echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
;;
[n|N]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
;;
esac
fi


slufoot80 08-29-2012 04:52 PM

now I get a different error
 
/usr/sbin/adduser: line 98: syntax error near unexpected token `-ne'
/usr/sbin/adduser: line 98: `echo -ne "\nEnter ftp directory: ";read -e ftpdir'

Code:

#Script to add a user to this Linux system
#!/bin/bash

clear

if [ $(id -u) -eq 0 ]; then

function security
{
chown $username:ftp $sourcedir
chmod 775 $sourcedir
}

function userinfo
{
read -p "Enter User Name : " username

while [ -z $username ]|| egrep "^$username" /etc/passwd 1>/dev/null;
do
echo -ne "Either user exists or you entered a blank, enter username again: ";read -e username
done

echo -ne "Enter your password: ";read -s password

while [ -z $password ];
do
echo -ne "\nEnter your password again: ";read -s -e password
done

echo -ne "\nPlease Enter your User ID Number: ";read -ern5 uid
while [[ ! $uid =~ ^[0-9]+$ ]]||egrep $uid /etc/passwd >/dev/null; do
echo -ne "Please re-enter your uid positive intergers only: ";read -ern5 uid
done

read -p "Enter a Comment : " comment
read -p "Enter Users Source Directory : " sourcedir
while [ ! -d "$sourcedir" ];
do
echo -ne "\n$sourcedir Directory Not Found! Please re-enter: "; read sourcedir
done
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) # passing the password entered
}

function shellsel
{
echo ""
echo "Select the type of shell you will be using"
echo""
echo -e "1) Bash Shell - SFTP Secure\n" # Shell selection statement
echo -e "2) False Shell - FTP Unsecure\n"
echo -ne "Enter choice: ";read shell;
case $shell in
1)
shell=/bin/bash # case statment for shell selection.
commentstatic="Internal SFTP Account"
useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
echo -e "Copying System Files ...."
cd /nas_ftp5/Customer/Troy/T_Skel
cp -Rp `ls` $sourcedir
echo -e "Finished Copying System Files ..."
tail -1 /etc/passwd > $sourcedir/etc/passwd
echo "$username" >> /etc/ftpusers
echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
2)
shell=/bin/false
commentstatic="Internal FTP Account"
useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
esac

}

echo -ne "\nIndividual Account or Environment Account\n\n"
echo -e "1) Individual Account\n"
echo -e "2) Environment Account\n"
echo -ne "Enter choice: ";read acctchoice;

case $acctchoice in
1)
userinfo
shellsel
security
;;
2)
userinfo
shellsel
cd /forms
ln -s $sourcedir /forms/${username}sa
security

while true; do
echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm
case $confirm in
[Yy]* )
;;
echo -ne "\nEnter ftp directory: ";read -e ftpdir

while [ ! -d "$ftpdir" ];
do
echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
done

usernameftp=${username}ftp
useridftp=$userid
let $useridftp++
commentstatic2="Internal Secondary FTP Account"
shellftp=`egrep $username /etc/passwd| cut -d: -f7`
useradd -u $useridftp -c "$comment $commentstatic2" -d $ftpdir -s $shellftp $usernameftp

egrep $usernameftp /etc/passwd >> /etc/ftpusers
echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
;;
[Nn]* ) exit;;
esac
;;
esac
done
fi


chrism01 08-29-2012 05:51 PM

I strongly suggest you bookmark & read
http://rute.2038bug.com/index.html.gz

and refer to
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Also use the
Code:

set -xv
as advised (twice) and look at what its telling you.

You'll also find it easier to read/debug by indenting code within fns and case - esac, if - fi etc.

pan64 08-30-2012 12:38 AM

no, you have got another error message. Probably similar, but surely not the same. At this moment you have ;; on line 97 (the line before the error message), and that causes this error. that ;; line should not be there.

konsolebox 08-30-2012 01:08 AM

This might fix things. I think. Please try.
Code:

#!/bin/bash

#Script to add a user to this Linux system

clear

if [ $(id -u) -eq 0 ]; then
        function security {
                chown $username:ftp $sourcedir
                chmod 775 $sourcedir
        }

        function userinfo {
                read -p "Enter User Name : " username

                while [ -z $username ]|| egrep "^$username" /etc/passwd 1>/dev/null; do
                        echo -ne "Either user exists or you entered a blank, enter username again: ";read -e username
                done

                echo -ne "Enter your password: ";read -s password

                while [ -z $password ]; do
                        echo -ne "\nEnter your password again: ";read -s -e password
                done

                echo -ne "\nPlease Enter your User ID Number: "; read -ern5 uid
                while [[ ! $uid =~ ^[0-9]+$ ]]||egrep $uid /etc/passwd >/dev/null; do
                        echo -ne "Please re-enter your uid positive intergers only: ";read -ern5 uid
                done

                read -p "Enter a Comment : " comment
                read -p "Enter Users Source Directory : " sourcedir
                while [ ! -d "$sourcedir" ]; do
                        echo -ne "\n$sourcedir Directory Not Found! Please re-enter: "; read sourcedir
                done
                pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) # passing the password entered
        }

        function shellsel {
                echo ""
                echo "Select the type of shell you will be using"
                echo ""
                echo -e "1) Bash Shell - SFTP Secure\n" # Shell selection statement
                echo -e "2) False Shell - FTP Unsecure\n"
                echo -ne "Enter choice: ";read shell;
                case "$shell" in
                1)
                        shell=/bin/bash # case statment for shell selection.
                        commentstatic="Internal SFTP Account"
                        useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
                        echo -e "Copying System Files ...."
                        cd /nas_ftp5/Customer/Troy/T_Skel
                        cp -Rp `ls` $sourcedir
                        echo -e "Finished Copying System Files ..."
                        tail -1 /etc/passwd > $sourcedir/etc/passwd
                        echo "$username" >> /etc/ftpusers
                        echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
                        ;;
                2)
                        shell=/bin/false
                        commentstatic="Internal FTP Account"
                        useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
                        echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
                        ;;
                esac
        }

        echo -ne "\nIndividual Account or Environment Account\n\n"
        echo -e "1) Individual Account\n"
        echo -e "2) Environment Account\n"
        echo -ne "Enter choice: "; read acctchoice;

        case "$acctchoice" in
        1)
                userinfo
                shellsel
                security
                ;;
        2)
                userinfo
                shellsel
                cd /forms
                ln -s $sourcedir /forms/${username}sa
                security

                while true; do
                        echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm
                        case $confirm in
                        [Yy]* )
                                echo -ne "\nEnter ftp directory: ";read -e ftpdir

                                while [ ! -d "$ftpdir" ]; do
                                        echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
                                done

                                usernameftp=${username}ftp
                                useridftp=$userid
                                let $useridftp++
                                commentstatic2="Internal Secondary FTP Account"
                                shellftp=`egrep $username /etc/passwd | cut -d: -f7`
                                useradd -u $useridftp -c "$comment $commentstatic2" -d $ftpdir -s $shellftp $usernameftp

                                egrep $usernameftp /etc/passwd >> /etc/ftpusers
                                echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
                                egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
                                ;;
                        [Nn]* )
                                exit
                                ;;
                        esac
                done
                ;;
        esac
fi

Note: I wonder if you had to add space between the patterns and ')' within case statements e.g. '[Yy]* )' and '[Nn]* )'. Also please consider possible IFS-based variable expansion within unquoted variables. e.g. 'case $var in' and '[ $(...) ... ]'.

pan64 08-30-2012 01:21 AM

you should not put functions inside if.

instead of writing if (root user) then do this you would better write:
if (not root user) print error message and exit.
do the job.

konsolebox 08-30-2012 01:54 AM

Quote:

Originally Posted by pan64 (Post 4767988)
you should not put functions inside if.

Do you mean in general?

pan64 08-30-2012 02:04 AM

yes, in general it is not a good practice.

konsolebox 08-30-2012 02:41 AM

Quote:

Originally Posted by pan64 (Post 4768018)
yes, in general it is not a good practice.

Oh well I disagree. It depends on the purpose :)
What's the general problem that would occur if you do that?

slufoot80 08-31-2012 11:14 AM

this code errors out at the done line at the bottom, why?
usr/sbin/adduser: line 42: syntax error near unexpected token `done'
/usr/sbin/adduser: line 42: ` done'

Code:

yesNo()
{
echo -n "$* (Y/N)? "
 read yn
case $yn in
        yes|Yes|YES|y|Y)
            echo -ne "\nEnter ftp directory: ";read -e ftpdir

        while [ ! -d "$ftpdir" ];
        do
        echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
        done

        usernameftp=$username"ftp"
        useridftp=$userid
        let $useridftp++
        commentstatic2="Internal Secondary FTP Account"
        shellftp=`egrep $username /etc/passwd| cut -d: -f7`
        useradd -u $useridftp -c "$comment $commentstatic2" -d $ftpdir -s $shellftp $usernameftp

        egrep $usernameftp /etc/passwd >> /etc/ftpusers
        echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
        egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
            ;;
        no|No|n|N|NO)
            exit
            ;;
      esac
  done
}


slufoot80 08-31-2012 11:41 AM

I changed thing around and added an if statement
 
the error I get now is "/usr/sbin/adduser: line 140: syntax error near unexpected token `fi'
/usr/sbin/adduser: line 140: `fi'"

Code:

#Script to add a user to this Linux system
#!/bin/bash

clear

if [ $(id -u) -eq 0 ]; then

function security
{
chown $username:ftp $sourcedir
chmod 775 $sourcedir
}

yesNo()
{
  #repeat if yes or no option not valid
  while true
  do
      #$* read ever parameter giving to the yesNo function which will be the message
      echo -n "$* (Y/N)? "
      #junk holds the extra parameters yn holds the first parameters
      read yn junk
      #check for difference cases
      case $yn in
        yes|Yes|YES|y|Y)
            return 0
            ;;
        no|No|n|N|NO)
            return 1
            ;;
      esac
  done 
}

function userinfo
{
        read -p "Enter User Name : " username

        while [ -z $username ]|| egrep "^$username" /etc/passwd 1>/dev/null;
        do
        echo -ne "Either user exists or you entered a blank, enter username again: ";read -e username
        done

        echo -ne "Enter your password: ";read -s password

        while [ -z $password ];
        do
        echo -ne "\nEnter your password again: ";read -s -e password
        done

        echo -ne "\nPlease Enter your User ID Number: ";read -ern5 uid
        while [[ ! $uid =~ ^[0-9]+$ ]]||egrep $uid /etc/passwd >/dev/null; do
        echo -ne "Please re-enter your uid positive intergers only: ";read -ern5 uid
        done

        read -p "Enter a Comment : " comment
        read -p "Enter Users Source Directory : " sourcedir
        while [ ! -d "$sourcedir" ];
        do
        echo -ne "\n$sourcedir Directory Not Found! Please re-enter: "; read sourcedir
        done
        pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) # passing the password entered
}

function shellsel
{
        echo ""
        echo "Select the type of shell you will be using"
        echo""
        echo -e "1) Bash Shell - SFTP Secure\n" # Shell selection statement
        echo -e "2) False Shell - FTP Unsecure\n"
        echo -ne "Enter choice: ";read shell;
case $shell in
1)
        shell=/bin/bash # case statment for shell selection.
        commentstatic="Internal SFTP Account"
        useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
        echo -e "Copying System Files ...."
        cd /nas_ftp5/Customer/Troy/T_Skel
        cp -Rp `ls` $sourcedir
        echo -e "Finished Copying System Files ..."
        tail -1 /etc/passwd > $sourcedir/etc/passwd
        echo "$username" >> /etc/ftpusers
        echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
2)
        shell=/bin/false
        commentstatic="Internal FTP Account"
        useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
        echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
esac

}

        echo -ne "\nIndividual Account or Environment Account\n\n"
        echo -e "1) Individual Account\n"
        echo -e "2) Environment Account\n"
        echo -ne "Enter choice: ";read acctchoice;

case $acctchoice in
1)
        userinfo
        shellsel
        security
;;
2)
        userinfo
        shellsel
        cd /forms
        ln -s $sourcedir /forms/${username}sa
        security
esac
        while true; do
        echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm

if yesNo
then
  echo -ne "\nEnter ftp directory: ";read -e ftpdir

        while [ ! -d "$ftpdir" ];
        do
        echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
        done

        usernameftp=$username"ftp"
        useridftp=$userid
        let $useridftp++
        commentstatic2="Internal Secondary FTP Account"
        shellftp=`egrep $username /etc/passwd| cut -d: -f7`
        useradd -u $useridftp -c "$comment $commentstatic2" -d $ftpdir -s $shellftp $usernameftp

        egrep $usernameftp /etc/passwd >> /etc/ftpusers
        echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
        egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
else
  # echo 'You choose not to continue'
        exit
fi
fi


grail 08-31-2012 12:37 PM

Maybe if you indented to your script you might be able to see where your errors are? Also, if the #! is not the first line the script will not work.
And as has been mentioned several times, set -xv would show you the execution of the script and where your errors may be coming from.

slufoot80 08-31-2012 12:47 PM

output with set -xv
 
Code:

linux-bl9t:~ # adduser
clear
+ clear
































if [ $(id -u) -eq 0 ]; then

function security
{
chown $username:ftp $sourcedir
chmod 775 $sourcedir
}

yesNo()
{
  #repeat if yes or no option not valid
  while true
  do
      #$* read ever parameter giving to the yesNo function which will be the message
      echo -n "$* (Y/N)? "
      #junk holds the extra parameters yn holds the first parameters
      read yn junk
      #check for difference cases
      case $yn in
        yes|Yes|YES|y|Y)
            return 0
            ;;
        no|No|n|N|NO)
            return 1
            ;;
      esac
  done 
}

function userinfo
{
        read -p "Enter User Name : " username

        while [ -z $username ]|| egrep "^$username" /etc/passwd 1>/dev/null;
        do
        echo -ne "Either user exists or you entered a blank, enter username again: ";read -e username
        done

        echo -ne "Enter your password: ";read -s password

        while [ -z $password ];
        do
        echo -ne "\nEnter your password again: ";read -s -e password
        done

        echo -ne "\nPlease Enter your User ID Number: ";read -ern5 uid
        while [[ ! $uid =~ ^[0-9]+$ ]]||egrep $uid /etc/passwd >/dev/null; do
        echo -ne "Please re-enter your uid positive intergers only: ";read -ern5 uid
        done

        read -p "Enter a Comment : " comment
        read -p "Enter Users Source Directory : " sourcedir
        while [ ! -d "$sourcedir" ];
        do
        echo -ne "\n$sourcedir Directory Not Found! Please re-enter: "; read sourcedir
        done
        pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) # passing the password entered
}

function shellsel
{
        echo ""
        echo "Select the type of shell you will be using"
        echo""
        echo -e "1) Bash Shell - SFTP Secure\n" # Shell selection statement
        echo -e "2) False Shell - FTP Unsecure\n"
        echo -ne "Enter choice: ";read shell;
case $shell in
1)
        shell=/bin/bash # case statment for shell selection.
        commentstatic="Internal SFTP Account"
        useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
        echo -e "Copying System Files ...."
        cd /nas_ftp5/Customer/Troy/T_Skel
        cp -Rp `ls` $sourcedir
        echo -e "Finished Copying System Files ..."
        tail -1 /etc/passwd > $sourcedir/etc/passwd
        echo "$username" >> /etc/ftpusers
        echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
2)
        shell=/bin/false
        commentstatic="Internal FTP Account"
        useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
        echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
;;
esac

}

        echo -ne "\nIndividual Account or Environment Account\n\n"
        echo -e "1) Individual Account\n"
        echo -e "2) Environment Account\n"
        echo -ne "Enter choice: ";read acctchoice;

case $acctchoice in
1)
        userinfo
        shellsel
        security
;;
2)
        userinfo
        shellsel
        cd /forms
        ln -s $sourcedir /forms/${username}sa
        security
esac
        while true; do
        echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm

if yesNo
then
  echo -ne "\nEnter ftp directory: ";read -e ftpdir

        while [ ! -d "$ftpdir" ];
        do
        echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
        done

        usernameftp=$username"ftp"
        useridftp=$userid
        let $useridftp++
        commentstatic2="Internal Secondary FTP Account"
        shellftp=`egrep $username /etc/passwd| cut -d: -f7`
        useradd -u $useridftp -c "$comment $commentstatic2" -d $ftpdir -s $shellftp $usernameftp

        egrep $usernameftp /etc/passwd >> /etc/ftpusers
        echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
        egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
else
  # echo 'You choose not to continue'
        exit
fi
fi
/usr/sbin/adduser: line 139: syntax error near unexpected token `fi'
/usr/sbin/adduser: line 139: `fi'


slufoot80 08-31-2012 01:28 PM

in the code it seems not to be grabbing the userid it sees it as "1" in stead of applying or adding 1 to the previous userid, error message "useridftp=$userid"

Code:

useridftp=$userid
let useridftp++


konsolebox 08-31-2012 07:54 PM

I no longer follow where this goes. As grail suggested it's best if you properly indent your code. This is helpful so that you would easily see which part of your code makes a error with respect to syntax. When posting the code, it would also be easy for others to read it. Also, as I've said, you should close your open variables around double quotes (those that could be expanded with IFS).

slufoot80 08-31-2012 08:44 PM

I found the problem,
 
I was calling the wrong variable, but now I have a new problem when asked

"echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm", it works just fine but when finished it ask the same question again it doesn't exit when done, what do I do about that?

Code:

#!/bin/bash

#Script to add a user to this Linux system

clear

if [ $(id -u) -eq 0 ]; then
        function security {
                chown $username:ftp $sourcedir
                chmod 775 $sourcedir
        }

        function userinfo {
                read -p "Enter User Name : " username

                while [ -z $username ]|| egrep "^$username" /etc/passwd 1>/dev/null; do
                        echo -ne "Either user exists or you entered a blank, enter username again: ";read -e username
                done

                echo -ne "Enter your password: ";read -s password

                while [ -z $password ]; do
                        echo -ne "\nEnter your password again: ";read -s -e password
                done

                echo -ne "\nPlease Enter your User ID Number: "; read -ern5 uid
                while [[ ! $uid =~ ^[0-9]+$ ]]||egrep $uid /etc/passwd >/dev/null; do
                        echo -ne "Please re-enter your uid positive intergers only: ";read -ern5 uid
                done

                read -p "Enter a Comment : " comment
                read -p "Enter Users Source Directory : " sourcedir
                while [ ! -d "$sourcedir" ]; do
                        echo -ne "\n$sourcedir Directory Not Found! Please re-enter: "; read sourcedir
                done
                pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) # passing the password entered
        }

        function shellsel {
                echo ""
                echo "Select the type of shell you will be using"
                echo ""
                echo -e "1) Bash Shell - SFTP Secure\n" # Shell selection statement
                echo -e "2) False Shell - FTP Unsecure\n"
                echo -ne "Enter choice: ";read shell;
                case "$shell" in
                1)
                        shell=/bin/bash # case statment for shell selection.
                        commentstatic="Internal SFTP Account"
                        useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
                        echo -e "Copying System Files ...."
                        cd /nas_ftp5/Customer/Troy/T_Skel
                        cp -Rp `ls` $sourcedir
                        echo -e "Finished Copying System Files ..."
                        tail -1 /etc/passwd > $sourcedir/etc/passwd
                        echo "$username" >> /etc/ftpusers
                        echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
                        ;;
                2)
                        shell=/bin/false
                        commentstatic="Internal FTP Account"
                        useradd -u $uid -p $pass -c "$comment $commentstatic" -d /forms/$username"sa" -s $shell $username
                        echo -e "$username" '\t' "$sourcedir" >> /etc/security/chroot.conf
                        ;;
                esac
        }

        echo -ne "\nIndividual Account or Environment Account\n\n"
        echo -e "1) Individual Account\n"
        echo -e "2) Environment Account\n"
        echo -ne "Enter choice: "; read acctchoice;

        case "$acctchoice" in
        1)
                userinfo
                shellsel
                security
                ;;
        2)
                userinfo
                shellsel
                cd /forms
                ln -s $sourcedir /forms/${username}sa
                security

                while true; do
                        echo -ne "Would you like to create a Secondary ftp account? (y/n):"; read -e confirm
                        case $confirm in
                        [Yy]* )

                                echo -ne "Enter your password: ";read -s passwordftp

                                while [ -z $password ]; do
                                echo -ne "\nEnter your password again: ";read -s -e passwordftp echo -ne "\nEnter ftp directory: ";read -e ftpdir
                                done
                                passftp=$(perl -e 'print crypt($ARGV[0], "passwordftp")' $passwordftp) # passing the password entered

                                echo -ne "\nEnter ftp directory: ";read -e ftpdir
                                while [ ! -d "$ftpdir" ]; do
                                        echo -ne "\n$ftpdir Directory Not Found! Please re-enter: "; read ftpdir
                                done

                                usernameftp=${username}ftp
                                useridftp=$uid
                                let useridftp++
                                commentstatic2="Internal Secondary FTP Account"
                                shellftp=`egrep $username /etc/passwd | cut -d: -f7`
                                useradd -u $useridftp -p $passftp -c "$comment $commentstatic2" -d $ftpdir -s $shellftp $usernameftp

                                egrep $usernameftp /etc/passwd | cut -d: -f1 >> /etc/ftpusers
                                echo -e "$usernameftp" '\t' "$ftpdir" >> /etc/security/chroot.conf
                                egrep $usernameftp /etc/passwd >> $ftpdir/etc/passwd
                                ;;
                        [Nn]* )
                                exit
                                ;;
                        esac
                done
                ;;
        esac
fi


konsolebox 08-31-2012 10:03 PM

Perhaps you don't have to place them in a "while true; do ...; done" block?


All times are GMT -5. The time now is 03:45 PM.