[Condition] if then else

Hi,
I want to test some commands in a script but it doesn't work..
Can you help me ?

my_script file1
nom=$1
for FILE in $(find temp/ -name "$nom"); do echo $nom; done 2> /dev/null
name_log=$(basename "${FILE}")
if [ -f "$name_log" ]; then echo $nom
else
echo -e "There is no file "$nom""
                read -p ""
                echo $REPLY
fi

Thank you

The for is a bit funny as it does the exact same thing for each file. "find ... |while read FILE" is more robust and parallel.

name_log only has access to the last file if any. You are tesitng that a file of the same entry name is in $PWD. It seems like more needs to be inside the loop: do ... done.

Try it like this:

nom=$1
echo "Your looking for file($nom) in directory and subdirectories(/temp)..."
for FILE in `find /temp -name $nom 2>/dev/null`
do
  echo "File found: $FILE"
done
if [ -z $FILE ]; then
  echo "File($nom) not found in directory and subdirectories(/temp)..."
fi

Best pre-clean FILE:

$ (FILE=x
for FILE in $(sleep 2)
 do
  echo hi
 done
echo $FILE
)
x
$

Thanks

./spacebar test
Your looking for file(test) in directory and subdirectories(/Scripts)...
File found: Scripts/test

it's OK

./spacebar tes
Your looking for file(tes) in directory and subdirectories(/Scripts)...

it doesn't work

---------- Post updated at 05:11 PM ---------- Previous update was at 05:04 PM ----------

My english spoken is average.
My level in script is low.
I'm not sure to have understand, can you tell me more because it doesn't work and i would like keeping this script with this form as you advise.

nom=$1
find -name "$nom" |while read FILE in $(/temp);
name_log=$(basename "${FILE}")
do
echo "$nom"
done
if [ -f "$name_log" ]; then
echo -e "There is no file "$nom""
                read -p ""
                #echo $REPLY
fi
  1. Do not use potentially dirty variables,
  2. 'find' is where the starting directory must go,
  3. 'do' goes right after "while read vars ;" and
  4. just test for blank in a simple, intuitive way.
FILE=                                                               # or 'unset FILE'
 
find /temp -name "$nom" |while read FILE ; do ... done 
 
if [ "$FILE" = "" ] ....

Hello,
Thank you for your advices.

The file "test" exist

 ./search test

The result is

test

that's OK

The file "tes" doesn't exist

 ./search test

blank result
The right result would be "There is no file tes"

What's wrong ? All suggestions are welcome.
Thanks

my_script

nom=$1
FILE=""
find ./temp -type f -name "$nom" |while read FILE ;do
name_log=$(basename "${FILE}")
if [ ! -f "$DIRECTORY/$name_log" ]; then echo $name_log
else
echo -e "There is no file "$name_log""
fi
done

That behaviour is quite easy to understand: find doesn't locate any file and thus its output is empty; the while read gets an immediate EOF condition and the loop is not entered.

Thanks, my idea consists to test return code without using while but it seems that the command find doesn't like return code.

Another thing, my script returns this error

./search3: line 7: syntax error near unexpected token `else'
./search3: line 7: `else'

I'm ashamed, I don't know Why ?

nom=$1
for FILE in $(find ./temp/ -name "$nom"); do
if [ echo "$?" eq 0 ] then
echo $? #mode debug
name_log=$(basename "${FILE}")
echo -e "The file "$name_log" exist"
else
echo -e "There is no file "$nom""
fi
done

There should be a ; between ] and then or put then on a new line.
The should be a - in front eq

This code will break if there are too many files or if the file names contain spaces or certain special characters. It is better to use:

find ./temp/ -name "$nom" |
while read FILE
do 
  ...

Checking the exit status does not make sense there. Also, the way you are doing that is totally wrong.

if [ echo "$?" eq 0 ] then

should be (again, why???)

if [ $? -eq 0 ]; then

Thanks for your help Scrutinizer and elixir_sinari
I follow yours instructions

nom=$1
find ./temp/ -name "$nom"|
while read FILE
do
if [ $? -eq 0 ]; then
echo $? #mode debug
name_log=$(basename "${FILE}")
echo -e "The file "$name_log" exist"
else
echo -e "There is no file "$nom""
fi
done

Here the results

 ./my_script test
0
The file test exist

But when the file doesn't exist

 ./my_script tes

no response

How do i make for testing a missing file ?
Can you show me a bit of script for that ?

Thanks in advance

It's a bit tricky to get values back to your main script from subshells, and pipes are running in subshells. You can try this one that will save a good result in a variable for use outside the loop and output what you request, but you can't use the result, without further processing, in the main:

$ find ./temp -iname "$nom" | { while read FILE; do echo $FILE exists; FOUND=1; done; [ "$FOUND" != "1" ]  && echo No file $nom found; }

Another approach:

if find . -name "$nom" > /dev/null 2> /dev/null
then
        printf "File: ${nom} exists\n"
else
        printf "File: ${nom} does not exist\n"
fi

Thanks a lot RudiC
It's OK :slight_smile:

@bipinajith: I'm afraid that proposal will not work as (at least my implementation [find (GNU findutils) 4.4.2] of) find will exit with status 0 even if no files found.

You're right! My bad.

Here is a modified version that I think might work:

r=$( find . -name "$nom" -exec echo "1" \; )

if [ "$r" = "1" ]
then
        printf "File: ${nom} exists\n"
else
        printf "File: ${nom} does not exist\n"
fi

I don't know exactly what you are trying to achieve, but perhaps you are looking for something like this:

if find ./temp -type f -name "$nom" | grep -q .
then
  echo "$nom" found
else 
  ....
fi

or this:

if list=$( find ./temp -type f -name "$nom" | grep . )
then
  echo "This is the list of file with the pattern: \"$nom\""
  echo "$list"
else
  echo "no files with the pattern: \"$nom\" were found"
fi

Hello,

I need to extend my search into 2nd path /TMP if in the first the file has been not found, but with this configuration, it doesn't work. Can you help me.

find ./temp -iname "$nom" && find ./TMP -iname "$nom" | { while read FILE; do echo $FILE exists; FOUND=1; done; [ "$FOUND" != "1" ] && echo -e "No file "$nom" found"; }

Try find ./temp ./TMP -iname "$nom" ...