teslasimus
teslasimus

Reputation: 1298

No such file or directory error while execute commands from script

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

Upvotes: 1

Views: 2557

Answers (2)

Armali
Armali

Reputation: 19375

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.

Upvotes: 1

Chilledrat
Chilledrat

Reputation: 2605

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

Upvotes: 4

Related Questions