简体   繁体   English

Mac OS 上的 Bash shell 脚本 - 某些命令只回显到 shell,不执行

[英]Bash shell script on Mac OS - Certain commands only echoed to shell, not executed

I am attempting to get a shell (bash) script working on a Mac OS X 10.12.5 machine, using the built in version of Bash (3.2) that comes with OS X 10.12 via the Terminal application.我正在尝试通过终端应用程序使用 OS X 10.12 附带的内置版本的 Bash (3.2) 使 shell (bash) 脚本在 Mac OS X 10.12.5 机器上运行。

I am guessing that there is a syntactical issue here, but I have been so far been unable to get one of the commands to execute as expected.我猜这里存在语法问题,但到目前为止我一直无法按预期执行其中一个命令。 Every time the command is called it purely echoes the command to the shell, but does not execute it.每次调用该命令时,它只是向 shell 回显该命令,但不执行它。

Another command that exists in the script executes without issue, so I am guessing this issue somehow relates to the single quotes / double quotes / brackets in use there.脚本中存在的另一个命令执行没有问题,所以我猜这个问题与那里使用的单引号/双引号/括号有关。

The script came from a tutorial that I have been following to learn Bash scripting, so I believe that this would work fine on a Linux Bash shell, but I am not trying to use this via Linux.该脚本来自我一直在学习 Bash 脚本的教程,所以我相信这在 Linux Bash shell 上可以正常工作,但我不想通过 Linux 使用它。

The script looks like so..剧本是这样的。。

#!/bin/bash

dbname="testdb"
table_layout="(id int not null auto_increment, primary key(id), name varchar(255), description text)"
action=$1
list_name=$2
name=$3
id=$3
desc=$4

if [[ $# -lt 1 ]]
then
  echo "******************************"
  echo "Task Application from tutorial"
  echo "******************************"
  echo ""
  echo "Usage:"
  echo "task (new-list)|del-list|add|del|ls)"
  echo "new-list: Create a new list"
  echo "del-list: Delete a list and all tasks inside the list"
  echo "add: task add list_name task_name task_description - creates a new task"
  echo "del: task del list_name task_id deletes a working task"
  echo "ls: task ls - lists tasks, task ls list_name, lists tasks in a list"
  echo ""
  echo ""
fi

function create_list {
    echo create table $list_name$table_layout | mysql $dbname
}

function add_task {
    echo 'insert into "${list_name}" (name, description) values ("${name}","${desc}") | mysql $dbname'
}

But the command which I am having the issue with is this one.. (add task command)但是我遇到问题的命令是这个..(添加任务命令)

echo 'insert into "${list_name}" (name, description) values ("${name}","${desc}") | mysql $dbname'

I have also attempted using the above line like so:我也尝试使用上面的行,如下所示:

echo `insert into "${list_name}" (name, description) values ("${name}","${desc}") | mysql $dbname`

and...和...

echo `insert into `${list_name}` (name, description) values ("`${name}`","`${desc}`") | mysql $dbname`

as well as...也...

echo 'insert into '${list_name}' (name, description) values ("'${name}'","'${desc}'") | mysql $dbname'

(...The last one matching what was included in the original tutorial) (...最后一个匹配原始教程中包含的内容)

I have also installed Bash 4.4 via Homebrew, but the results were the same.我还通过 Homebrew 安装了 Bash 4.4,但结果是一样的。

Any advice there would be appreciated!任何建议将不胜感激!

You are correct: it is quoting.你是对的:它是引用。 In the command that is just printing, everything - including the | mysqli在只是打印的命令中,所有内容 - 包括| mysqli | mysqli - is within the single quotes '' . | mysqli - 在单引号'' Therefore, bash takes all of that as a strong and passes it to echo , which dutifully prints it.因此, bash 将所有这些视为强项并将其传递给echo ,后者尽职尽责地打印它。

The line from the tutorial edit either has this problem or picked it up somewhere.教程编辑中的行要么有这个问题,要么在某处找到它。 (Notably, it does not escape SQL characters and therefore is a security risk if used by anyone other than yourself.) A slight improvement would be to double-quote the variable expansions as well as moving the trailing quote: (值得注意的是,它不会转义 SQL 字符,因此如果被除您以外的任何人使用,则存在安全风险。)一个小小的改进是双引号变量扩展以及移动尾随引号:

echo 'insert into '"${list_name}"' (name, description) values ("'"${name}"'","'"${desc}"'")' | mysql "$dbname"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM