语境:
我想运行存储在variables中的命令在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)
。
如果我在terminal运行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 ,其中谈到了对复杂情况的确切要求。