Bash 错误 "command not found" 运行 存储在 var 中的命令
Bash error "command not found" running a command stored in var
上下文:
我想运行一个命令存储在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 的全部 return:
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.
问题:
你有什么线索可以告诉我吗?我做错了什么?
使用函数存储复杂命令比使用变量更好,
grunt() {
docker exec compose_custom-node_1 node_modules/grunt-cli/bin/grunt "$@"
}
并将其称为
grunt "saas"
脚本中任何需要的地方。请参阅 BashFAQ-050,其中讨论了对复杂案例的确切要求。
上下文:
我想运行一个命令存储在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 的全部 return:
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.
问题:
你有什么线索可以告诉我吗?我做错了什么?
使用函数存储复杂命令比使用变量更好,
grunt() {
docker exec compose_custom-node_1 node_modules/grunt-cli/bin/grunt "$@"
}
并将其称为
grunt "saas"
脚本中任何需要的地方。请参阅 BashFAQ-050,其中讨论了对复杂案例的确切要求。