LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   syntax error near unexpected token `then' (https://www.linuxquestions.org/questions/linux-server-73/syntax-error-near-unexpected-token-%60then%27-4175483839/)

tuananhk53 11-08-2013 02:09 AM

syntax error near unexpected token `then'
 
Hi,
I'm trying to build a script for adding existing local Users to LDAP Directory, but i have an error:
Code:

adduser.sh: line 12: syntax error near unexpected token `then'
adduser.sh: line 12: `then'

Here is my script:
Code:

#!/bin/bash

SUFFIX='dc=pingcom,dc=vn'
LDIF='adduser.ldif'

echo -n > $LDIF
for line in `grep "x:[5-9][0-9][0-9]:" /etc/passwd | sed -e "s/ /%/g"`
do
  UID1=`echo $line | cut -d: -f1`
  NAME=`echo $line | cut -d: -f5 | cut -d: -f1`
  if [ ! "$NAME" ]
  then
      NAME=$UID1
  else
      NAME=`echo $NAME | sed -e "s/%/ /g"`
  fi
  SN=`echo $NAME | awk '{print $2}'`
  if [ ! "$SN" ]
  then
      SN=$NAME
  fi
  GIVEN=`echo $NAME | awk '{print $1}'`
  UID2=`echo $line | cut -d: -f3`
  GID=`echo $line | cut -d: -f4`
  PASS=`grep $UID1: /etc/shadow | cut -d: -f2`
  SHELL=`echo $line | cut -d: -f7`
  HOME=`echo $line | cut -d: -f6`
  EXPIRE=`passwd -S $UID1 | awk '{print $7}'`
  FLAG=`grep $UID1: /etc/shadow | cut -d: -f9`
  if [ ! "$FLAG" ]
  then
      FLAG="0"
  fi
  WARN=`passwd -S $UID1 | awk '{print $6}'`
  MIN=`passwd -S $UID1 | awk '{print $4}'`
  MAX=`passwd -S $UID1 | awk '{print $5}'`
  LAST=`grep $UID1: /etc/shadow | cut -d: -f3`

  echo "dn: uid=$UID1,$SUFFIX" >> $LDIF
  echo "objectClass: inetOrgPerson" >> $LDIF
  echo "objectClass: posixAccount" >> $LDIF
  echo "objectClass: shadowAccount" >> $LDIF
  echo "uid: $UID1" >> $LDIF
  echo "sn: $SN" >> $LDIF
  echo "givenName: $GIVEN" >> $LDIF
  echo "cn: $NAME" >> $LDIF
  echo "displayName: $NAME" >> $LDIF
  echo "uidNumber: $UID2" >> $LDIF
  echo "gidNumber: $GID" >> $LDIF
  echo "userPassword: {crypt}$PASS" >> $LDIF
  echo "gecos: $NAME" >> $LDIF
  echo "loginShell: $SHELL" >> $LDIF
  echo "homeDirectory: $HOME" >> $LDIF
  echo "shadowExpire: $EXPIRE" >> $LDIF
  echo "shadowFlag: $FLAG" >> $LDIF
  echo "shadowWarning: $WARN" >> $LDIF
  echo "shadowMin: $MIN" >> $LDIF
  echo "shadowMax: $MAX" >> $LDIF
  echo "shadowLastChange: $LAST" >> $LDIF
  echo >> $LDIF
done

i don't think to solve this error:
if[...];
then
...............
Maybe, help me for a script
Thanks all!

pan64 11-08-2013 02:22 AM

I haven't got any error messages.
try bash -vx adduser.sh to check what was going wrong. Which version of bash did you try?

tuananhk53 11-08-2013 02:41 AM

Quote:

Originally Posted by pan64 (Post 5060576)
I haven't got any error messages.
try bash -vx adduser.sh to check what was going wrong. Which version of bash did you try?

Hi pan64
Here is version after commmand sh --version
Code:

GNU bash, version 4.1.2(1)-release (i368-redhat-linux-gnu)
..........................
There is NO WARRANTY, to the extent permitted by law

and i tried command bash -vx adduser.sh
Code:

#!/bin/bash

SUFFIX='dc=pingcom,dc=vn'
+ SUFFIX=dc=pingcom,dc=vn
LDIF='adduser.ldif'
+ LDIF=adduser.ldif

echo -n > $LDIF
+ echo -n
for line in `grep "x:[5-9][0-9][0-9]:" /etc/passwd | sed -e "s/ /%/g"`
do
  UID1=`echo $line | cut -d: -f1`
  NAME=`echo $line | cut -d: -f5 | cut -d: -f1`
  if [ ! "$NAME" ]
  then
adduser.sh: line 12: syntax error near unexpected token `then'
adduser.sh: line 12: `then'

thanks for help, pan

pan64 11-08-2013 02:58 AM

Both worked for me:
GNU bash, version 3.2.51(1)-release (x86_64-suse-linux-gnu)
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)

I assume you have some hidden char(s) in the file (probably you edited it on windows?).
You can try od -cx adduser.sh to check it.

tuananhk53 11-08-2013 03:50 AM

Quote:

Originally Posted by pan64 (Post 5060593)
Both worked for me:
GNU bash, version 3.2.51(1)-release (x86_64-suse-linux-gnu)
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)

I assume you have some hidden char(s) in the file (probably you edited it on windows?).
You can try od -cx adduser.sh to check it.

i don't think this error by version, and i always exit file before doing command sh (my os is ClearOS and CentOS with CLI mode)
thanks for your recommend, but i can't solve this error, hmmm

Firerat 11-08-2013 04:14 AM

have you checked for the 'hidden char(s)' ?

Code:

od -cx adduser.sh
look for \r \n
or
Code:

cat -A adduser.sh
look for ^M$

or anything you don't recognise

tuananhk53 11-08-2013 08:26 PM

Quote:

Originally Posted by Firerat (Post 5060642)
have you checked for the 'hidden char(s)' ?

Code:

od -cx adduser.sh
look for \r \n
or
Code:

cat -A adduser.sh
look for ^M$

or anything you don't recognise

hi Firerat,
thanks for your comment, and i checked for hidden char(s)
Code:

cat -A adduser.sh
Code:

#!/bin/bash $
$
SUFFIX='dc=pingcom,dc=vn'$
LDIF='adduser.ldif'$
$
echo -n > $LDIF$
for line in `grep "x:[5-9][0-9][0-9]:" /etc/passwd | sed -e "s/ /%/g"`$
do$
^IUID1=`echo $line | cut -d: -f1`$
^INAME=`echo $line | cut -d: -f5 | cut -d: -f1`$
^Iif [ ! "$NAME" ]$
^Ithen$
^I^INAME=$UID1$
^Ielse$
^I^INAME=`echo $NAME | sed -e "s/%/ /g"`$
^Ifi$
^ISN=`echo $NAME | awk '{print $2}'`$
^Iif [ ! "$SN" ]$
^Ithen$
^I^ISN=$NAME$
^Ifi$
^IGIVEN=`echo $NAME | awk '{print $1}'`$
^IUID2=`echo $line | cut -d: -f3`$
^IGID=`echo $line | cut -d: -f4`$
^IPASS=`grep $UID1: /etc/shadow | cut -d: -f2`$
^ISHELL=`echo $line | cut -d: -f7`$
^IHOME=`echo $line | cut -d: -f6`$
^IEXPIRE=`passwd -S $UID1 | awk '{print $7}'`$
^IFLAG=`grep $UID1: /etc/shadow | cut -d: -f9`$
^Iif [ ! "$FLAG" ]$
^Ithen$
^I^IFLAG="0"$
^Ifi$
^IWARN=`passwd -S $UID1 | awk '{print $6}'`$
^IMIN=`passwd -S $UID1 | awk '{print $4}'`$
^IMAX=`passwd -S $UID1 | awk '{print $5}'`$
^ILAST=`grep $UID1: /etc/shadow | cut -d: -f3`$
$
^Iecho "dn: uid=$UID1,$SUFFIX" >> $LDIF$
^Iecho "objectClass: inetOrgPerson" >> $LDIF$
^Iecho "objectClass: posixAccount" >> $LDIF$
^Iecho "objectClass: shadowAccount" >> $LDIF$
^Iecho "uid: $UID1" >> $LDIF$
^Iecho "sn: $SN" >> $LDIF$
^Iecho "givenName: $GIVEN" >> $LDIF$
^Iecho "cn: $NAME" >> $LDIF$
^Iecho "displayName: $NAME" >> $LDIF$
^Iecho "uidNumber: $UID2" >> $LDIF$
^Iecho "gidNumber: $GID" >> $LDIF$
^Iecho "userPassword: {crypt}$PASS" >> $LDIF$
^Iecho "gecos: $NAME" >> $LDIF$
^Iecho "loginShell: $SHELL" >> $LDIF$
^Iecho "homeDirectory: $HOME" >> $LDIF$
^Iecho "shadowExpire: $EXPIRE" >> $LDIF$
^Iecho "shadowFlag: $FLAG" >> $LDIF$
^Iecho "shadowWarning: $WARN" >> $LDIF$
^Iecho "shadowMin: $MIN" >> $LDIF$
^Iecho "shadowMax: $MAX" >> $LDIF$
^Iecho "shadowLastChange: $LAST" >> $LDIF$
^Iecho >> $LDIF$
done$

sorry, but i don't understand this command
can you check errors, Firerat?
thanks!

pan64 11-09-2013 01:57 AM

It looks ok...
I cannot find the reason, but you can try to insert the following line before if [ ! "$NAME" ]
echo "processing: $NAME."
and please run the script and post the result

Firerat 11-09-2013 01:07 PM

like pan64, it looks fine to me

I've copied your script, including tabs ( the ^I indicate tabs )
and it works fine


for now, at least, I'm lost...

voleg 11-10-2013 05:16 AM

It is about " ' ` usually. Try to avoid use ` at all, like:
Code:

DATE=$(date -s)
All your script will work better rewritten in awk as single command.

Firerat 11-10-2013 08:51 AM

fixed FLAG, had hangover $UID1
 
or bash

Code:

#!/bin/bash

SUFFIX='dc=pingcom,dc=vn'
LDIF='adduser.ldif'

OrigIFS=$IFS
IFS=":"
while read Login PassEncrypted Nuid Ngid UserNameComment HomeDir Shell;do
    echo "dn: uid=${Login},${SUFFIX}"
    echo "objectClass: inetOrgPerson"
    echo "objectClass: posixAccount"
    echo "objectClass: shadowAccount"
    echo "uid: ${Login}"
    [[ "${UserNameComment}" == "" ]] && UserNameComment=$Login
    echo "sn: ${UserNameComment//,}"
    echo "givenName: ${Login}"
    echo "cn: ${UserNameComment//,}"
    echo "displayName: ${UserNameComment//,}"
    echo "uidNumber: ${Nuid}"
    echo "gidNumber: ${Ngid}"
    echo "userPassword: {crypt}$(grep ${Login}: /etc/shadow | cut -d: -f2)"
    echo "gecos: ${UserNameComment//,}"
    echo "loginShell: ${Shell}"
    echo "homeDirectory: ${HomeDir}"
    echo "shadowExpire: $(passwd -S ${Login} | awk '{print $7}')"
    FLAG=$( grep ${Login}: /etc/shadow | cut -d: -f9 )
    [[ ! "$FLAG" ]] && FLAG=0
    echo "shadowFlag: ${FLAG}"
    IFS=$OrigIFS
    while read LoginName PassState LastPassChange Min Max Warn Inactivity;do
        echo "shadowWarning: ${Warn}"
        echo "shadowMin: ${Min}"
        echo "shadowMax: ${Max}"
        LastPassChange=$(grep ${Login}: /etc/shadow | cut -d: -f3)
        echo "shadowLastChange: ${LastPassChange}"
    done < <(passwd -S ${Login})
    IFS=":"
done < <( grep "x:[5-9][0-9][0-9]:" /etc/passwd ) > "$LDIF"
IFS=$OrigIFS


probably needs tweaking

tuananhk53 11-11-2013 01:30 AM

Quote:

Originally Posted by pan64 (Post 5061202)
It looks ok...
I cannot find the reason, but you can try to insert the following line before if [ ! "$NAME" ]
echo "processing: $NAME."
and please run the script and post the result

Hi pan,
i tried to insert this line before if.......
but this error isn't solved
hmm, sorry for not reply you soon, pan
and i need your help, thanks for your interest in my thread

tuananhk53 11-11-2013 01:35 AM

Quote:

Originally Posted by Firerat (Post 5061898)
or bash

Code:

#!/bin/bash

SUFFIX='dc=pingcom,dc=vn'
LDIF='adduser.ldif'

OrigIFS=$IFS
IFS=":"
while read Login PassEncrypted Nuid Ngid UserNameComment HomeDir Shell;do
    echo "dn: uid=${Login},${SUFFIX}"
    echo "objectClass: inetOrgPerson"
    echo "objectClass: posixAccount"
    echo "objectClass: shadowAccount"
    echo "uid: ${Login}"
    [[ "${UserNameComment}" == "" ]] && UserNameComment=$Login
    echo "sn: ${UserNameComment//,}"
    echo "givenName: ${Login}"
    echo "cn: ${UserNameComment//,}"
    echo "displayName: ${UserNameComment//,}"
    echo "uidNumber: ${Nuid}"
    echo "gidNumber: ${Ngid}"
    echo "userPassword: {crypt}$(grep ${Login}: /etc/shadow | cut -d: -f2)"
    echo "gecos: ${UserNameComment//,}"
    echo "loginShell: ${Shell}"
    echo "homeDirectory: ${HomeDir}"
    echo "shadowExpire: $(passwd -S ${Login} | awk '{print $7}')"
    FLAG=$( grep ${Login}: /etc/shadow | cut -d: -f9 )
    [[ ! "$FLAG" ]] && FLAG=0
    echo "shadowFlag: ${FLAG}"
    IFS=$OrigIFS
    while read LoginName PassState LastPassChange Min Max Warn Inactivity;do
        echo "shadowWarning: ${Warn}"
        echo "shadowMin: ${Min}"
        echo "shadowMax: ${Max}"
        LastPassChange=$(grep ${Login}: /etc/shadow | cut -d: -f3)
        echo "shadowLastChange: ${LastPassChange}"
    done < <(passwd -S ${Login})
    IFS=":"
done < <( grep "x:[5-9][0-9][0-9]:" /etc/passwd ) > "$LDIF"
IFS=$OrigIFS


probably needs tweaking

hi Firerat,
ok i will try your script, and i'll post the result early
Thanks Fire,hi

pan64 11-11-2013 01:57 AM

Quote:

Originally Posted by tuananhk53 (Post 5062294)
Hi pan,
i tried to insert this line before if.......
but this error isn't solved
hmm, sorry for not reply you soon, pan
and i need your help, thanks for your interest in my thread

Of course, it could not solve the issue because I do not know what's happened. That's why I asked to insert that line and post the result. Answering "it did not solve" has no any meaning, requesting help without posting what I asked means I cannot help you further.

tuananhk53 11-11-2013 02:33 AM

Quote:

Originally Posted by pan64 (Post 5062318)
Of course, it could not solve the issue because I do not know what's happened. That's why I asked to insert that line and post the result. Answering "it did not solve" has no any meaning, requesting help without posting what I asked means I cannot help you further.

ok, pan
this is result when i add a line
Code:

echo "processing: $NAME"
here:
Code:

adduser.sh: line 13: syntax error near unexpected token `then'
adduser.sh: line 13: `then'

it's same error before

hmm, i can understand what you say, but i speak and write English not very well


All times are GMT -5. The time now is 08:17 AM.