shell 环境变量传递

一共三个文件, 两个shell(father.sh 和 child.sh),一个Makefile

准备脚本

father.sh -> Makefile -> child.sh

father.sh 调用 Makefile 调用 child.sh

 1. father.sh

# father.sh

hello=world
export hello
echo "father $hello"

# ./child.sh
interface=enp1s0
export host_ip=$(ip a show dev ${interface} | grep "inet " | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }')

export host_ip1="This is sherry test"
echo $host_ip

echo "run make testip"
make testip MAKEVAR="makefile command var"

2. Makefile

# Makefile
HOSTIP = ${host_ip}
export HOSTIP

testip:
    @echo "echo hostip"
    @echo ${HOSTIP} $${host_ip1:-unknow}
    @TESTVAR="makefile test" ./child.sh

3. child.sh

# child.sh
echo "child $hello"
echo "child hostip $HOSTIP"
echo "get make var: $TESTVAR"
echo "get make cmd var: $MAKEVAR"

准备 

ls
# Makefile  child.sh  father.sh

chmod a+x  child.sh  father.sh

sed -i -e 's/    /\t/' Makefile

测试

ip r | grep -o "dev [^ ]*" | cut -d " " -f 2 |head -n 1
interface=$(ip r | grep -o "dev [^ ]*" | cut -d " " -f 2 |head -n 1)
./father.sh $interface

 

REF: 

exporting makefile variables to $(shell) environment 

makefile 执行shell脚本 

 

posted @ 2023-08-03 20:07  lvmxh  阅读(86)  评论(0编辑  收藏  举报