1

I'm working on a script that takes commands from MySQL and executes them one by one. My problem is I can't execute the commands from script:

./bash.sh: line 26: /myscript.sh -c "": no such file or directory

Line 26 is the command I wish to execute ("$com"). if i try to run the command manually, by echoing the content of "$com" and than run it from terminal, it's working.

What am I doing wrong?

if [ ! "${#array[*]}" -eq "0" ]; then
for (( i=0 ; i<cnt ; i++ )); do
        id=$(echo "${array[$i]}" | sed 's@\t@^@g' | cut -f'1' -d'^')
        com=$(echo "${array[$i]}" | sed 's@\t@^@g' | cut -f'2' -d'^')
        imp=$(echo "${array[$i]}" | sed 's@\t@^@g' | cut -f'3' -d'^')

        if [[ "$id" = [0-9]* ]]; then
                "$com"
                echo "DELETE FROM list WHERE id='$id'" | mysql "$DB_USER" -u "$DB_USER" -p"$DB_PASS"
        fi
done
else
        echo "The list is empty"
fi
4
  • @Vaughn Cato it worked like a charm! Commented Feb 19, 2013 at 1:08
  • echo ${array[$i]} and see what the value is just above the "com" line maybe there is no field 2 when you are doing cut ...this might help you debug it better. Commented Feb 19, 2013 at 1:09
  • 3
    It is difficult understand what you are trying to do, but you should probably read mywiki.wooledge.org/BashFAQ/050. It explains why you should not store commands in variables, and what can be done instead Commented Aug 30, 2013 at 11:06
  • The real issue is how you populate array in the first place. Consider maintaining a collection of parallel arrays, i.e. cmd[$i] would contain the command to execute, id[$i] the ID, imp[$i] the value for imp. Commented Aug 30, 2013 at 12:52

2 Answers 2

4

Added as Community Wiki

This question was solved in the comments by Vaughn Cato and the OP hasn't been seen since March.

The accept answer there was to use eval so the if statement in the script would be:

if [[ "$id" = [0-9]* ]]; then
   eval "$com"
   echo "DELETE FROM list WHERE id='$id'" | mysql "$DB_USER" -u "$DB_USER" -p"$DB_PASS"
fi
Sign up to request clarification or add additional context in comments.

Comments

1

Change

"$com"

rather to

$com

(remember: too many quotes can be as harmful as too few)

than to

eval "$com"
  • it is unnecessarily complicated to use eval just to undo the unwanted effect of quoting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.