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

时间:2017-12-01 22:29:48

标签: bash workflow

我为各种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脚本。

1 个答案:

答案 0 :(得分:1)

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

将配置放在文件中,例如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

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

source tw_config.sh