LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   bash shell script error!!! go to "usernameftp=$username"ftp" (http://www.linuxquestions.org/questions/linux-server-73/bash-shell-script-error-go-to-usernameftp%3D%24username-ftp-4175424278/)

slufoot80 08-27-2012 03:02 PM

bash shell script error!!! go to "usernameftp=$username"ftp"
 
This is the error I get

=======================================================
/usr/sbin/adduser: line 95: syntax error near unexpected token `newline'
/usr/sbin/adduser: line 95: ` usernameftp=$username"ftp"'
=======================================================below is the script
=======================================================
#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
;;
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

MensaWater 08-27-2012 03:14 PM

When appending to variables it is best to encapsulate them with brackets {}.

i.e. instead of:
Code:

usernameftp=$username"ftp"
do:
Code:

usernameftp=${username}ftp

slufoot80 08-27-2012 03:19 PM

now I get
 
/usr/sbin/adduser: line 95: syntax error near unexpected token `newline'
/usr/sbin/adduser: line 95: ` usernameftp=${username}ftp'

MensaWater 08-27-2012 03:37 PM

You're doing a case. The error is saying it didn't expect a newline because it thought you were still doing the first case test.

Remove the ";;" from below the y|Y) line and add it to the end of the line just above n|N.

Also you should remove the ";;" below the n|N) line as the last line of the case before the esac is the end of the case so doesn't need continuation like earlier steps.

Also it is good practice to have *) condition as last one of a case to make it give an error message if any input other than that expected is given. i.e. What happens if the user types W instead of Y or N? If you did add such an option to your case you WOULD still want the ";;" after the n|N line but not after the new *) line.

robertjinx 08-27-2012 03:41 PM

Should be:

Code:

usernameftp="${username}ftp"

slufoot80 08-27-2012 03:49 PM

question
 
how do I write it to accept only y or n or Y or N?

MensaWater 08-27-2012 03:51 PM

Quote:

Originally Posted by robertjinx (Post 4765758)
Should be:

Code:

usernameftp="${username}ftp"

The quotes around the variable aren't required and in some cases can cause confusion. Any way on second look I realized OP's issue is in the case above line 95 rather than line 95 itself. Often error output pukes on unexpected lines rather than the lines that are causing the problem before them.


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