相关疑难解决方法(0)

如何将 bash 脚本准确地写入 Makefile 中?

我喜欢打 bash 脚本,但是如果我准备了多个工具,项目的根目录就塞满了这么多的 shell 脚本。这就是我更喜欢使用 Makefile 的原因。

Makefile 很好。但是,我想像构建常规 bash 脚本一样构建我的 makefile。

例如:

以为自己很快写了一个bash脚本,内容如下:

#!/bin/bash
echo "hello"
cd ~
do-some-work.sh my-parameter
Run Code Online (Sandbox Code Playgroud)

我可以用$ ./my-important-task.sh.

如果我想将该脚本移动到 makefile 中,我应该执行以下操作:

SHELL := /bin/bash 

my-important-task: 
    echo "hello" ;\
    cd ~ ;\
    do-some-work.sh my-parameter
Run Code Online (Sandbox Code Playgroud)

但我想要以下内容:

my-important-task: 
    [[copy and paste the my-important-task.sh file]]
Run Code Online (Sandbox Code Playgroud)

有没有办法实现这个目标?

bash make

20
推荐指数
3
解决办法
7万
查看次数

在 Makefile 中迭代 bash 关联数组

$ bash -version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Run Code Online (Sandbox Code Playgroud)

考虑以下 shell 脚本:

#!/bin/bash

declare -A PROVS=( ["NL"]=10 ["PE"]=11 ["NS"]=12 ["NB"]=13 ["QC"]=24 ["ON"]=35 ["MB"]=46 ["SK"]=47 ["AB"]=48 ["BC"]=59 ["YK"]=60 ["NT"]=61 ["NU"]=62 )

for key in "${!PROVS[@]}" ; do \
  touch "foo_${key}_${PROVS[${key}]}" ; \
done
Run Code Online (Sandbox Code Playgroud)

我正在尝试在 Makefile 中执行等效操作:

SHELL := /bin/bash
.PHONY: foo
foo:
  declare -A PROVS=( ["NL"]=10 ["PE"]=11 ["NS"]=12 ["NB"]=13 ["QC"]=24 ["ON"]=35 ["MB"]=46 ["SK"]=47 ["AB"]=48 ["BC"]=59 ["YK"]=60 ["NT"]=61 ["NU"]=62 )

  for key in "$${!PROVS[@]}" ; do \
    touch "foo_$${key}_$${PROVS[$${key}]}" ; \
  done
Run Code Online (Sandbox Code Playgroud)

我 …

bash make associative-array

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

bash ×2

make ×2

associative-array ×1