enter image description here

I am writing a script where I am trying to export some variable , and after exporting my variable when I am printing, it is printing an extra line after it.

Here I am reading from a csv file containing two columns. This script is present in file script.sh and I am executing that file ./script.sh

while IFS="," read f1 f2
      echo "Source IP        : $f1"     #it is printing without extra line
      echo "Destination IP   : $f2"               #it is printing without extra line  

    export sourceIP=$f1
    export destIP=$f2

    ssh -t -t sjain@$f1 <<ENDSSH
    #Start copying 33KB File
    echo "Destination IP Address: $destIP" # here it is printing an extra line after destIP
    startTime=$(($(date '+%s%N')/1000000))
    scp KB_33.txt sjain@$destIP:/home/sjain
    endTime=$(($(date '+%s%N')/1000000))
    printf 'Elapsed time in copying 33KB file: %s\n' $((endTime-startTime)) 
    #Write the stats to the file
    echo $sourceIP','$destIP',33KB,'$((endTime-startTime)) >> report.txt
done < ipaddress.csv

because of the above problem when I am executing

scp KB_33.txt sjain@$destIP:/home/sjain

it is breaking the command into 2 parts , like this screen shot

scp KB_33.txt sjain@$X.X.X.X

And when I am directly hardcoding the value instead of destIP , it is working fine.

How can I fix this?

Input CSV file Data,
  • Can you post a few lines of your csv files for me to test. – mkc Mar 12 '14 at 18:46
  • Updated the question , it contains only 1 row – saurav Mar 12 '14 at 18:48
  • If it contains just one row, reading it in a while loop is overkill, just use cut command and pipe. – mkc Mar 12 '14 at 18:51
  • in future it will have more rows , for now I have just put for testing purpose – saurav Mar 12 '14 at 18:54
  • I haven't tested this, but do try tr-ing ',' to ' ' and don't use IFS. I'm not sure about the scope in this case, it might mess up the parsing. – orion Mar 12 '14 at 19:41

When you split the contents of your CSV file you're splitting on commas, ,. This can be problematic because the second IP address in that file likely has a trailing newline character after it.

The easiest method to resolve this would be to simply chomp that character off the end of the $destIP variable like so:

export sourceIP=$f1
export destIP=$(echo "$f2" | tr -d '\n')
  • Yes, you were right second IP address has the trailing new lien character in it. - Thank you – saurav Mar 12 '14 at 20:03

echo normally puts a newline after printing its arguments. You can prevent it from doing so by using

echo -n "Destination IP Address: $destIP" 

to strip the $destIP you could try:

export destIP=$(echo $f2) 
  • dats correct , but actual problem is at this line scp KB_33.txt sjain@$destIP:/home/sjain , here it is splitting into 2 lines after variable destIP. – saurav Mar 12 '14 at 18:55
  • Sorry I should have read further. I have updated the answer – Anthon Mar 12 '14 at 19:01
  • thanks anthon , i tried it but still same issue. – saurav Mar 12 '14 at 19:05

You can use tr to delete the newline character. In place of sjain@$destIP, use:

sjain@$(echo $destIP | tr -d '\n')
  • i tried this scp KB_33.txt sjain@$(echo $destIP | tr -d '\n'):/home/sjain but it didn't work – saurav Mar 12 '14 at 19:08
  • Can you show the output of echo $destIP and echo $(echo $destIP | tr -d '\n') – mkc Mar 12 '14 at 19:11
  • #echo $destIP [sjain@XYZ ~]$ echo "Destination IP Address234: 10.X.X.X > " Destination IP Address234: 10.X.X.X #echo $(echo $destIP | tr -d '\n') [sjain@XYZ ~]$ echo "Dest 2 :" 10.X.X.X Dest 2 : 10.X.X.X [sjain@XYZ ~]$ – saurav Mar 12 '14 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.