2017-05-03 253 views
2

語境:Bash的錯誤「找不到命令」

我想運行存儲在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的整體回報:

[email protected]:/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

如果在終端中運行docker exec compose_custom-node_1 node_modules/grunt-cli/bin/grunt sass會發生什麼情況?這似乎是您的問題命令正在運行 – arco444

+0

@ arco444與運行'docker exec compose_custom-node_1 node_modules/grunt-cli/bin/grunt'相同 – darckcrystale

+2

有關此類問題的常見問題,請參閱http://mywiki.wooledge。 org/BashFAQ/050 –

回答

4

更好地使用功能複雜的命令存儲超過一個變量,

grunt() { 
    docker exec compose_custom-node_1 node_modules/grunt-cli/bin/grunt "[email protected]" 
} 

,並把這個作爲剛剛

grunt "saas" 

需要的地方在你的腳本。請參閱BashFAQ-050,其中討論了有關複雜案例的確切要求。

+0

在我的情況下,我必須爲'GRUNT'命令添加一個參數:'sass'。如何通過創建函數來實現這一點? – darckcrystale

+2

@darckcrystale:簡單,只需傳遞參數即可 – Inian