Escaping MYSQL command lines via Bash Scripting

后端 未结 8 961
走了就别回头了
走了就别回头了 2020-12-06 01:09

PHP has mysql_real_escape_string() to correctly escape any characters that might cause problems. What is the best way to mimic this functionality for BASH?

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 01:36

    This will escape apostrophes

    a=$(echo "$1" | sed s/"'"/"\\\'"/g)
    

    Please note though that mysql_real_escape_string also escapes \x00, \n, \r, \, " and \x1a. Be sure to escape these for full security.

    To escape \x00 for example:

    a=$(echo "$1" | sed s/"\x00"/"\\\'"/g)
    

    With a bit of effort you can probably escape these using one sed command.

提交回复
热议问题