Syntax Error Script

Hi guys i'd like to show you this code of my script, where i couldn't find this error
" [ !-d: commando not found"
"Syntax error near unexpected token `fi' "

my code:

#! /bin/bash
	#copiabin.sh: copia todos los archivos ejecutables a bin 
	if [ !-d $HOME/bin ]
	then
	   mkdir $HOME/bin
	fi
	# copia de archivos y contador N
	N=0
	
	for ARCH in *
	do
	   if [ -x $ARCH -a -f $ARCH ] # Si el archivo es ejecutable y es un fichero comun (no directorio)
           then
	       cp $ARCH $HOME/bini
	       echo " $ARCH fue copiado a $HOME/bin"
	       N='expr $N + 1'
	   fi
	done
	if [ $N -eq 0 ]
	then
	    echo "No se copio ningun archivo"
	else
	    echo "Se copiaron $N archivos"
	fi

Some idea to help?
Thanks!!!

Space required below...:slight_smile:

if [ ! -d $HOME/bin]

That will not work. You need to have a space after the file/directory name too. Also, the script provided has many other errors.

Hi pamu, you right i miss the white space, but the other error it's still alive, the unexpected toke fi on 16 line, it's about the fi below of the line that content

N='expr $N + 1'
fi

Yes you are missing the required spaces between all your square brackets...
Go and look your code, I made some changes...

#! /bin/bash
	#copiabin.sh: copia todos los archivos ejecutables a bin 
	if [ ! -d $HOME/bin ]
	then
	mkdir $HOME/bin
	fi
	# copia de archivos y contador N
	N=0
	
	for ARCH in *
	do
	if [ -x $ARCH -a -f $ARCH ] # Si el archivo es ejecutable y es un fichero comun (no directorio)
        then
	cp $ARCH $HOME/bini
	echo " $ARCH fue copiado a $HOME/bin"
	N=$(expr $N + 1)
	fi
	done
	if [ $N -eq 0 ]
	then
	echo "No se copio ningun archivo"
	else
	echo "Se copiaron $N archivos"
	fi

You right too Elixir_sinari, but i put it from the begining!!

Yes elixir... there are lot of errors.....
Thanks....:slight_smile:

Guys the error still persist in the same line 16..

#!/bin/sh      # I am at the moment working on a AIX with no bash...
        #copiabin.sh: copia todos los archivos ejecutables a bin
        if [ ! -d $HOME/bin ]     # space was missing...
        then
           mkdir $HOME/bin
        fi
        # copia de archivos y contador N
        N=0 ; echo "N = "$N

        for ARCH in *
        do
           if [ -x $ARCH -a -f $ARCH ] # Si el archivo es ejecutable y es un fichero comun (no directorio)
           then
               cp $ARCH $HOME/bini
               echo " $ARCH fue copiado a $HOME/bin" ;  echo "N = "$N
               N=$(expr $N + 1)
           fi
        done
        echo "N = "$N
        read kyb   # This is so  to type enter to continue so I can look what is happening and follow...
        if [ $N -eq 0 ]
        then
            echo "No se copio ningun archivo"
        else
            echo "Se copiaron $N archivos"
        fi
~

output produced:

vbe@ son28: /home/vbe> ll
total 144
drwxr-xr-x    2 vbe      staff          4096 Sep 10 18:47 .
drwxr-xr-x   27 bin      bin            4096 Sep 05 11:31 ..
-rwxr-----    1 vbe      staff           105 Apr 30 2009  .kshrc
-rwxr-----    1 vbe      staff           272 Apr 30 2009  .profile
-rw-------    1 vbe      sysdso         1008 Sep 10 18:47 .sh_history
-rw-------    1 vbe      sysdso           54 Sep 10 18:46 .vi_history
-rw-r--r--    1 vbe      sysdso        34146 Sep 10 17:32 smit.log
-rw-r--r--    1 vbe      sysdso          946 Sep 10 17:31 smit.script
-rw-r--r--    1 vbe      sysdso         2292 Sep 10 17:31 smit.transaction
-rwxrwxr-x    1 vbe      sysdso          582 Sep 10 18:46 test001
vbe@ son28: /home/vbe> ./test001
N = 0
 test001 fue copiado a /home/vbe/bin
N = 0
N = 1

Se copiaron 1 archivos
vbe@ son28: /home/vbe> ll
total 152
drwxr-xr-x    3 vbe      staff          4096 Sep 10 18:49 .
drwxr-xr-x   27 bin      bin            4096 Sep 05 11:31 ..
-rwxr-----    1 vbe      staff           105 Apr 30 2009  .kshrc
-rwxr-----    1 vbe      staff           272 Apr 30 2009  .profile
-rw-------    1 vbe      sysdso         1128 Sep 10 18:49 .sh_history
-rw-------    1 vbe      sysdso           60 Sep 10 18:49 .vi_history
drwxr-xr-x    2 vbe      sysdso          256 Sep 10 18:49 bin
-rwxr-xr-x    1 vbe      sysdso          583 Sep 10 18:49 bini
-rw-r--r--    1 vbe      sysdso        34146 Sep 10 17:32 smit.log
-rw-r--r--    1 vbe      sysdso          946 Sep 10 17:31 smit.script
-rw-r--r--    1 vbe      sysdso         2292 Sep 10 17:31 smit.transaction
-rwxrwxr-x    1 vbe      sysdso          583 Sep 10 18:49 test001

Anything else? (I am not going to check the logic...)