zach
zach

Reputation: 30983

pass values from a file as a Makefile variable

I would like to use values from a file to pass to a command in Make. The problem context is passing a set of Ids to fetch proteins from NCBI using eutils CLI. I thought of using process substitution but that will pass a file location and we need a string. So I am trying to set local bash variable and use it in my Make step but I cannot get it to work. Any hints would be greatly appreciated.

zachcp

SHELL := /bin/bash


# efetch with one id
test1.txt:
    efetch -db protein -id 808080249 -format XML >$@

# efetch with two, comma-separated ids 
test2.txt:
    efetch -db protein -id 808080249,806949321  -format XML > $@

# put some ids in a file....
data.txt:
    echo "808080249\n806949321" > $@

# try to call the ids with process substitution.
# doesn't expand before being called so it trips an error...
test3.txt: data.txt
    efetch -db protein -id <(cat $< | xargs printf "%s,") -format XML > $@

# try to set and use a local BASH variable
test4.txt: data.txt
    ids=`cat $< | xargs printf "%s,"`
    efetch -db protein -id $$ids -format XML > $@

Upvotes: 0

Views: 188

Answers (1)

Mario Zannone
Mario Zannone

Reputation: 2883

text3.txt will probably work if you use $(...) or `...` instead of <(...)

test3.txt: data.txt
    efetch -db protein -id $$(cat $< | xargs printf "%s,") -format XML > $@

text4.txt fails because each line is executed in a different shell process, thus the variable set in the first line is out of scope in the second one; will work if you put both the statements on the same line:

test4.txt: data.txt
    ids=`cat $< | xargs printf "%s,"`; efetch -db protein -id $$ids -format XML > $@

Upvotes: 2

Related Questions