简体   繁体   中英

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.

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.

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.

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.

Any advice there would be appreciated!

You are correct: it is quoting. In the command that is just printing, everything - including the | mysqli | mysqli - is within the single quotes '' . Therefore, bash takes all of that as a strong and passes it to echo , which dutifully prints it.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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