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
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.
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
./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
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
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 ?
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; }
@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.
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
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.