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 @   lvmxh  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示