We have a shell script which should read line from a file, where the file contents are the filenames, we need to locate the path of these filenames and copy to a different directory. Here we have written the below shell script, each time the script behaves differently At once the while loop just reads one line and exits At second instance, the locate command works fine for first line of the file and the second line in the file does not provide the output it gives a blank output
Shell Script
#!/usr/bin/ksh
file="test.txt"
while IFS= read -r line
do
# display $line or do somthing with $line
echo "$line"
fileloc=`locate "$line"`
i=`echo $?`
if [ $? != 0 ]
then
echo "Unsuccessful"
else
echo $fileloc
cp $fileloc /home/user/PO_AUDIT
echo "Successful"
fi
done <"$file"
test.txt (below are the filenames that are stored in our server)
11687892
11687893
i=
echo $?`` between thelocate
command and theif [ $? != 0 ]
test - you will end up testing the exit status of theecho