从另一个文件加载 bash 中的变量

Loading variables in bash from another file

我为各种 Android 设备构建了一些 ROM 和其他软件,为了让我更轻松,我使用基于 bash 的脚本。 这是我的 TWRP 构建脚本的示例:

#!/usr/bin/env bash

# Variables
export TW_DEVICE_VERSION="0"
export BRANCH="android-5.1"

# Don't touch this
VERSION=$( grep "TW_MAIN_VERSION_STR" bootable/recovery/variables.h -m 1 | cut -d \" -f2 )-${TW_DEVICE_VERSION}

# Acer Liquid Z500 specific TWRP build configuration
export BRAND="acer"
export DEVICE="acer_Z500"

git clone https://github.com/liquidporting/android_device_${BRAND}_${DEVICE}.git -b ${BRANCH} device/${BRAND}/${DEVICE}
. build/envsetup.sh
lunch omni_${DEVICE}-eng
mka recoveryimage > twrp_${DEVICE}.log
cd out/target/product/${DEVICE}
if [ -f "recovery.img" ]
then
  mv recovery.img twrp-${VERSION}-${DEVICE}.img
else
  echo ""
  echo "*******************************************************************************"
  echo "Something went wrong during the build process, try checking your device tree."
  echo "After that, run the script again and see if you messed up something new or not."
  echo "*******************************************************************************"
  echo ""
fi

if [ -f "twrp-${VERSION}-${DEVICE}.img" ]
then
  megarm /Root/LPAD/TWRP/twrp-${VERSION}-${DEVICE}.img
  megarm /Root/LPAD/TWRP/twrp_${DEVICE}.log
  megaput --no-progress --path /Root/LPAD/TWRP twrp-${VERSION}-${DEVICE}.img
  megaput --no-progress --path /Root/LPAD/TWRP ../../../../twrp_${DEVICE}.log
fi

if [ -f "twrp-${VERSION}-${DEVICE}.img" ]
then
  cd ../../../..
  rm twrp_${DEVICE}.log
  make clean
  cd device
  rm -rf ${BRAND}
  cd ..
else
  rm twrp_${DEVICE}.log
  make clean
  cd device
  rm -rf ${BRAND}
  cd ..
  echo ""
  echo "**************************************************************"
  echo "The build process of TWRP Recovery failed for device ${DEVICE}"
  echo "**************************************************************"
  echo ""
  exit
fi

# Lenovo A328 specific TWRP build configuration
export BRAND="lenovo"
export DEVICE="A328"

git clone https://github.com/liquidporting/android_device_${BRAND}_${DEVICE}.git -b ${BRANCH} device/${BRAND}/${DEVICE}
. build/envsetup.sh
lunch omni_${DEVICE}-eng
mka recoveryimage > twrp_${DEVICE}.log
cd out/target/product/${DEVICE}
if [ -f "recovery.img" ]
then
  mv recovery.img twrp-${VERSION}-${DEVICE}.img
else
  echo ""
  echo "*******************************************************************************"
  echo "Something went wrong during the build process, try checking your device tree."
  echo "After that, run the script again and see if you messed up something new or not."
  echo "*******************************************************************************"
  echo ""
fi

if [ -f "twrp-${VERSION}-${DEVICE}.img" ]
then
  megarm /Root/LPAD/TWRP/twrp-${VERSION}-${DEVICE}.img
  megarm /Root/LPAD/TWRP/twrp_${DEVICE}.log
  megaput --no-progress --path /Root/LPAD/TWRP twrp-${VERSION}-${DEVICE}.img
  megaput --no-progress --path /Root/LPAD/TWRP ../../../../twrp_${DEVICE}.log
fi

if [ -f "twrp-${VERSION}-${DEVICE}.img" ]
then
  cd ../../../..
  rm twrp_${DEVICE}.log
  make clean
  cd device
  rm -rf ${BRAND}
  cd ..
else
  rm twrp_${DEVICE}.log
  make clean
  cd device
  rm -rf ${BRAND}
  cd ..
  echo ""
  echo "**************************************************************"
  echo "The build process of TWRP Recovery failed for device ${DEVICE}"
  echo "**************************************************************"
  echo ""
  exit
fi

是否可以有一个包含构建指令的单独 bash 脚本和另一个包含变量集的脚本?

我的意思是这样的:

#!/usr/bin/env bash

export TW_DEVICE_VERSION="0"
export BRANCH="android-5.1"

VERSION=$( grep "TW_MAIN_VERSION_STR" bootable/recovery/variables.h -m 1 | cut -d \" -f2 )-${TW_DEVICE_VERSION}

export BRAND="acer"
export DEVICE="acer_Z500"

export BRAND="lenovo"
export DEVICE="A328"

export BRAND="doogee"
export DEVICE="X5"

但是在每个设备特定配置之后,我需要它来启动包含构建指令的 bash 脚本。

是的,这是非常可行的,也是一件好事。

将您的配置放入文件中,例如 tw_config.sh。然后,以这种方式调用配置脚本:

source /path/to/tw_config.sh
if [ $? -ne 0 ]; then
  # couldn't load config
  # your logic here - makes sense to exit
fi

如果 tw_config.sh 在你的 PATH 中,你可以简单地说:

source tw_config.sh