Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to do the following in a bash script:

  1. read some filenames out of an file with filenames
  2. set a variable input path
  3. concat 1. and 2.
  4. use 3. in sql-statement

Here my code:

 #!/bin/bash
 INPath="/home/bono/RD/BV-OUT/"

 while 
 read line
 do
 RD="$line"
 RDFile="$INPath$RD"

 echo -e $RDFile

 ###MYSQLs:
 mysql -u root -D RD --local-infile << EOF
   LOAD DATA LOCAL INFILE '$RDFile'
     INTO TABLE bv_tmp_all FIELDS TERMINATED BY ';' LINES TERMINATED BY '\r\n';
 EOF
 ##### EOSQL

 done < /home/bono/RD/BV-OUT/allto468

The echo shows the right path and filenamen but mysql always says:

not found (Errcode: 2 - File or Directory not found)  

I've tried different ways to concate the two strings but none worked :(

The following code works with mysql (without the concate) but it is not what I need:

 #!/bin/bash

 while 
 read line
 do

 RDFile="/home/bono/RD/BV-OUT/468-R11"

 echo -e $RDFile

 ###MYSQLs:
 mysql -u root -D RD --local-infile << EOF
   LOAD DATA LOCAL INFILE '$RDFile'
     INTO TABLE bv_tmp_all FIELDS TERMINATED BY ';' LINES TERMINATED BY '\r\n';
 EOF
 ##### EOSQL

 done < /home/bono/RD/BV-OUT/allto468

What is wrong with the concate?

share|improve this question

closed as off-topic by BuZZ-dEE, Eric Carvalho, mikewhatever, Jorge Castro, minerz029 Feb 11 at 20:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – BuZZ-dEE, Eric Carvalho, mikewhatever, Jorge Castro, minerz029
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Thank you Florian. Sorry I am new here ;) –  Bono Feb 7 at 17:57

3 Answers 3

up vote 2 down vote accepted

If the allto468 itself has \r\n line endings, then $line will look like 468-R11\r, and that file is surely not found.

Solutions

  1. run dos2unix on the allto468 file to fix the issue once,
  2. change the done line to

    done < <(sed 's/\r$//' /home/bono/RD/BV-OUT/allto468)
    
share|improve this answer
    
GREAT! It works! Thank you Glenn. Tiny things.... –  Bono Feb 8 at 9:09

It seems that RDFile doesn't exist in your first script. What do you have in /home/bono/RD/BV-OUT/allto468? Are they filenames that really exist in /home/bono/RD/BV-OUT/?

I would add a test in the middle to check if the constructed path really exists, for example like this:

#!/bin/bash
INPath="/home/bono/RD/BV-OUT/"

while read line; do
    RD="$line"
    RDFile="$INPath$RD"
    echo $RDFile

    if ! test -f "$RDFile"; then
        echo error: file does not exist: $RDFile
        continue
    fi

    ###MYSQLs:
    mysql -u root -D RD --local-infile << EOF
LOAD DATA LOCAL INFILE '$RDFile'
 INTO TABLE bv_tmp_all FIELDS TERMINATED BY ';' LINES TERMINATED BY '\r\n';
EOF
    ##### EOSQL
done < /home/bono/RD/BV-OUT/allto468
share|improve this answer
    
Thank you. The test says the file does not exist!!! But if I use the result of the echo directly than it works.... :( The echo shows the right path. What is wrong with the concate? –  Bono Feb 7 at 18:06
    
Do you run the script with a different user? If the script runs as a different user, it's possible that the user doesn't have permission to the containing directory, causing the test to fail. –  janos Feb 7 at 18:12
    
I am root and gave 777 to the directory –  Bono Feb 7 at 18:17
    
And when you are root, can you read the content of that file? For example with sudo head /path/to/file ? –  janos Feb 7 at 18:19
    
Yes I have all rights.... :( –  Bono Feb 7 at 18:21

It looks like a quoting problem to me. To get variable substitution to work, use '"', not "'", that is, replace:

mysql -u root -D RD --local-infile << EOF  
LOAD DATA LOCAL INFILE '$RDFile'  
INTO TABLE bv_tmp_all FIELDS TERMINATED BY ';' LINES TERMINATED BY '\r\n';  
EOF  

with:

mysql -u root -D RD --local-infile << EOF
LOAD DATA LOCAL INFILE "$RDFile"
INTO TABLE bv_tmp_all FIELDS TERMINATED BY ';' LINES TERMINATED BY '\r\n';
EOF

To check for funny characters in the filename, echo "$RDFile" | od -bc.

share|improve this answer
    
Quoting was not the problem....The dos-in-file was! –  Bono Feb 8 at 9:10

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