#!/bin/bash
#dbyum.sh

# 在没有root权限时,使用yum search和yumdownloader 安装软件到指定路径下

E_BADARGS=65

if [ $# -eq 1 ]
then
  rpm_name=$1
  install_path=${HOME}/yum_custom
elif [ $# -eq 2 ] ; then
  rpm_name=$1
  install_path=$2
else
  echo -e "\nUsage: `basename $0` {rpm_name} [install_path](default: \$HOME/yum_custom)"
  echo "在没有root权限时,使用yum安装软件到指定路径下(默认:\$HOME/yum_custom)."
  echo "在大多数情况下(有的时候可能也不一定需要),你可以使用yum list | grep xxx 或yum search xxx"
  echo "来查找可用的软件包名称作为第一个参数:比如perf命令的准确软件包名称是:perf.x86_64"
  echo ""
  exit $E_BADARGS
fi

E_YUM_NOT_INSTALLED=66
E_YUMDOWNLOADER_NOT_INSTALLED=67
E_RPM2CPIO_NOT_INSTALLED=68
E_CPIO_NOT_INSTALLED=69
E_YUM_DOWNLOAD_FAILED=70
E_INVALID_INSTALL_PATH=71
E_INSTALL_FAILED=72


if [ ! -d ${install_path} -o ! -w ${install_path} ]
then
  echo "install_path: ${install_path} is not a directory or not writable!"
  exit $E_INVALID_INSTALL_PATH
fi

if ! type yum &> /dev/null ; then
  echo "You should make sure yum is installed!";
  exit $E_YUM_NOT_INSTALLED
fi

if ! type yumdownloader &> /dev/null ; then
  echo "You should make sure yumdownloader is installed!";
  exit $E_YUMDOWNLOADER_NOT_INSTALLED
fi

if ! type rpm2cpio &> /dev/null ; then
  echo "You should make sure rpm2cpio is installed!";
  exit $E_RPM2CPIO_NOT_INSTALLED
fi

if ! type cpio &> /dev/null ; then
  echo "You should make sure cpio is installed!";
  exit $E_CPIO_NOT_INSTALLED
fi


number=$RANDOM
TMP_DOWNLOAD_DIR=/tmp/rpm.tmp.$number
echo "TMP_DOWNLOAD_DIR: $TMP_DOWNLOAD_DIR"
mkdir -p $TMP_DOWNLOAD_DIR

#yum search rpm_name --->  found its name
yumdownloader --destdir=$TMP_DOWNLOAD_DIR --resolve ${rpm_name}
if [ $? -ne 0 ]
then
  echo "yumdownloader failed!"
  rm -rf $TMP_DOWNLOAD_DIR
  exit $E_YUM_DOWNLOAD_FAILED
fi

cd ${install_path}
install_path=$(pwd)

# 遍历所有.rpm 文件
for file in $(find "$TMP_DOWNLOAD_DIR" -type f -name "*.rpm"); do
  echo "Installing $file ......"
  if ! rpm2cpio $file | cpio -idmv ; then
    echo "Failed to execute rpm2cpio or cpio for $file!"
    rm -rf $TMP_DOWNLOAD_DIR
    exit $E_INSTALL_FAILED
  fi
done
rm -rf $TMP_DOWNLOAD_DIR

source_file=${install_path}/source.txt

echo "export PATH=$install_path/usr/sbin:$install_path/usr/bin:$install_path/bin:\${PATH}" > $source_file
echo "export MANPATH=$install_path/usr/share/man:\${MANPATH}" >> $source_file
echo "export LD_LIBRARY_PATH=${install_path}/usr/lib:${install_path}/usr/lib64:\${LD_LIBRARY_PATH}" >> ${source_file}

echo "Success to install \"${rpm_name}\" into \"${install_path}\", you can source [  ${source_file}  ] to use it!"

exit 0

apollo008
151 声望9 粉丝

走完这一生,如果我和你在一起会变得更好,那我们就在一起,否则我就丢下你。我回顾我最光辉的时刻就是和不同的人在一起,变得更好的最长连续时刻。