0

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
1
  • You probably don't want i=echo $?`` between the locate command and the if [ $? != 0 ] test - you will end up testing the exit status of the echo Mar 20, 2019 at 16:53

1 Answer 1

0

why IHS= at this place?

If IFS is needed for thos script, you might consider something like

OLD_IFS=$IFS
IFS=
... your script
IFS=$OLD_IFS

The beginning of yout script this becomes:

#!/usr/bin/ksh
file="test.txt"
while IFS= read -r line
do

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .