0
votes

i'm new here, and i wanted to understand why my programm doesn't work. Can you help me ? thank you

Error

./mytry.sh: line 28: syntax error near unexpected token `fi'
./mytry.sh: line 28: `fi'
!/bin/sh

echo "find?"
read find 

if [ -z find ];
then 
echo "Ok"
fi

if [ ! -z $find ];
then
echo " You are in $(pwd)"
echo " I'm searching "

for element in $(ls)
do
echo $element

if [ -d $ element ];
then 
echo $element "exist and is not empty"
fi 
fi
done 
2
if [ -d $ element ]; => if [ -d $element ]; use www.shellcheck.net to validate it - anubhava
close the for loop before if block - Saravanan

2 Answers

1
votes

There were two syntax based mistakes. The shebang was written wrongly and the fi in the last line should come after done.

#!/bin/sh

echo "find?"
read find 

if [ -z find ];
then 
echo "Ok"
fi

if [ ! -z $find ];
then
echo " You are in $(pwd)"
echo " I'm searching "

for element in $(ls)
do
echo $element

if [ -d $ element ];
then 
echo $element "exist and is not empty"
fi 
done 
fi

Above is the corrected script.

0
votes

!/bin/sh

echo "find?" read find

if [ -z find ];
then 
 echo "Ok"

fi

if [ ! -z $find ]; then echo " You are in $(pwd)" echo " I'm searching "

for element in $(ls)
do
  echo $element

  if [ -d $ element ];
  then 
     echo $element "exist and is not empty"
  fi 
done 

fi #should close after for loop