【问题标题】:Loading variables in bash from another file从另一个文件加载 bash 中的变量
【发布时间】:2017-12-02 14:03:42
【问题描述】:

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

【问题讨论】:

  • . build/envsetup.sh 行已经在这样做了。查看envsetup.sh 的内部,您会看到类似的命令(可能还有一些控制设置的逻辑)。祝你好运。

标签: bash workflow


【解决方案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

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

source tw_config.sh

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多