上下文
我想在bash中运行存储在变量中的命令。
我的bash文件:
#!/bin/bash
# -----------------------------------------------
# --- COMMANDS ---
# --- Vanilla style ---
# -----------------------------------------------
GRUNT="node_modules/grunt-cli/bin/grunt"
LS="ls"
# -----------------------------------------------
# --- COMMANDS ---
# --- Using Docker ---
# -----------------------------------------------
GRUNT="docker exec compose_custom-node_1 node_modules/grunt-cli/bin/grunt"
LS="docker exec compose_custom-node_1 ls"
# ***********************************************
# *** Execution ***
# ***********************************************
# -----------------------------------------------
# Compile SCSS using Grunt
# -----------------------------------------------
echo "Building CSS from Sass files..."
echo "$(docker exec compose_custom-node_1 ls -l)"
echo "$($LS -l)"
$($GRUNT sass)
问题:
当我运行此bash文件时,grunt sass
命令会抛出错误:
mybash.sh: line 25: $'\E[4mRunning' : command not found
我的bash全部归还:
darckcrystale@kermit:/var/www/my_folder$ ./my_bash.sh
Building CSS from Sass files...
total 188
-rw-rw-r-- 1 node node 3627 May 2 19:00 Gruntfile.js
drwxr-xr-x 282 root root 12288 May 3 12:12 node_modules
drwxr-xr-x 4 node node 4096 May 2 18:39 sass
total 188
-rw-rw-r-- 1 node node 3627 May 2 19:00 Gruntfile.js
drwxr-xr-x 282 root root 12288 May 3 12:12 node_modules
drwxr-xr-x 4 node node 4096 May 2 18:39 sass
my_bash.sh: ligne 25: $'\E[4mRunning' : commande introuvable
调查:
命令echo "$(docker exec compose_custom-node_1 ls -l)"
和echo "$($LS -l)"
似乎有效,但不是$($GRUNT sass)
。
如果我在终端中运行docker exec compose_custom-node_1 node_modules/grunt-cli/bin/grunt
,我会看到此输出:
Running "sass:app1" (sass) task
Running "sass:sticky-app2" (sass) task
Done, without errors.
问题:
你对我有什么线索吗?我做错了什么?答案 0 :(得分:4)
最好使用函数存储复杂命令而不是变量
grunt() {
docker exec compose_custom-node_1 node_modules/grunt-cli/bin/grunt "$@"
}
并将其称为
grunt "saas"
脚本中的任何需要。请参阅BashFAQ-050,其中讨论了复杂案例的确切要求。