LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-28-2012, 09:44 AM   #1
slufoot80
Member
 
Registered: Nov 2011
Posts: 61

Rep: Reputation: Disabled
Exclamation 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
 
Old 08-28-2012, 09:55 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,192
Blog Entries: 1

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Missing ;;

Code:
n|N) exit
;;
esac
fi
 
Old 08-28-2012, 10:06 AM   #3
slufoot80
Member
 
Registered: Nov 2011
Posts: 61

Original Poster
Rep: Reputation: Disabled
missing from where

it is right in the bottom of my script
 
Old 08-28-2012, 10:11 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
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?
 
Old 08-28-2012, 10:14 AM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,192
Blog Entries: 1

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Quote:
Originally Posted by slufoot80 View Post
it is right in the bottom of my script
You don't have a double semi-colon after "exit"
 
Old 08-28-2012, 10:22 AM   #6
slufoot80
Member
 
Registered: Nov 2011
Posts: 61

Original Poster
Rep: Reputation: Disabled
Exclamation 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'
 
Old 08-28-2012, 11:12 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
Try placing set -xv as second line in file and see what the interpreter is doing.
 
Old 08-28-2012, 03:39 PM   #8
slufoot80
Member
 
Registered: Nov 2011
Posts: 61

Original Poster
Rep: Reputation: Disabled
Exclamation 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
 
Old 08-28-2012, 06:01 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,280

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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
 
Old 08-29-2012, 02:08 PM   #10
slufoot80
Member
 
Registered: Nov 2011
Posts: 61

Original Poster
Rep: Reputation: Disabled
Angry 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
 
Old 08-29-2012, 02:33 PM   #11
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,841

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
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...
 
Old 08-29-2012, 03:10 PM   #12
slufoot80
Member
 
Registered: Nov 2011
Posts: 61

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-29-2012, 04:52 PM   #13
slufoot80
Member
 
Registered: Nov 2011
Posts: 61

Original Poster
Rep: Reputation: Disabled
Exclamation 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

Last edited by slufoot80; 08-29-2012 at 04:53 PM. Reason: code
 
Old 08-29-2012, 05:51 PM   #14
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,280

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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.
 
Old 08-30-2012, 12:38 AM   #15
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,841

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
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.
 
  


Reply

Tags
shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
How to search for missing files and pass their names on to another shell script djslothario Linux - Newbie 3 08-07-2009 12:59 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM


All times are GMT -5. The time now is 05:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration