无法弄清楚如何实现此bash参数

时间:2020-06-07 03:34:23

标签: bash shell scripting

因此,我假设此任务是在aws实例上运行的自动化脚本,该脚本实际上是将内核更新为最新的稳定版本。该脚本还假定在..

中带有一些参数。
Installs current linux stable kernel source code into given Subdir
    or ~/src/linux-stable by default.  p01 takes a number of options,
    as described below.
    -g         git clone source from kernel.org instead of wget or curl
    -v         Version of new stable kernel but does not install it.
    -h         Help should display options with examples.
    -R         Reboot after download and install.
    -D Subdir  Subdir is the fullpath of downloaded stable kernel source

所以,我遇到的问题是-D参数。我可以就好使用任何其他参数的运行我的脚本,但是当涉及到实际指导脚本使用特定的子目录我只是失去了。问题是,我的老师可以通过几种方式运行此脚本...

script.sh -g -D / some / Directory

script.sh -D / some / Directory -g

所以我的问题是我不能依靠对参数进行计数,并希望在最后传递-D参数。有人可以指导我,以便我可以查找-D参数,然后将其后的下一个输入作为子目录吗?还是有更好的方法?

#!/bin/bash

for arg in "$@"
do
    case $arg in
        -g) Git=true ;;
        -v) Version=true ;;
        -h) Help=true ;;
        -R) Reboot=true ;;
        -D) Dir=true ;;
        *) echo "Unknown"
        exit 1 ;;
    esac
done

sudo yum update -y -q && sudo yum install -y -q jq wget perl ncurses-devel make gcc bc \
bison flex elfutils-libelf-devel openssl-devel  

sudo yum groupinstall -q -y “Development Tools”

InstalledKernel=$(uname -r | cut -d. -f-2)
TargetDir=${Arg_TargetDir:-"$HOME/src/linux-stable"}

mkdir $HOME/src
mkdir $TargetDir
#sudo rm /etc/dracut.conf.d/xen.conf

V=$(curl -s https://www.kernel.org/releases.json | \
    jq '.latest_stable.version' -r)

M="$(echo $(curl -s https://www.kernel.org/releases.json | \
    jq '.latest_stable.version' -r)  \
    | cut -d. -f1)"

if [[ $InstalledKernel == $V ]]; then
    echo "linux-stable already installed"
   # exit 1
fi 

if [[ $Help ]]; then
    echo "p01 - install current linux stable kernel
    Installs current linux stable kernel source code into given Subdir
    or ~/src/linux-stable by default.  p01 takes a number of options,
    as described below.
    -g         git clone source from kernel.org instead of wget or curl
    -v         Version of new stable kernel but does not install it.
    -h         Help should display options with examples.
    -R         Reboot after download and install.
    -D Subdir  Subdir is the fullpath of downloaded stable kernel source
    "
    exit 0
fi

if [[ $Version ]]; then
    echo $(curl -s https://www.kernel.org/releases.json | \
    jq '.latest_stable.version' -r)  
    exit 0
fi

if [[ $Dir ]]; then
    echo $TargetDir
fi

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

gpg2 --locate-keys "torvalds@kernel.org" "gregkh@kernel.org"
echo -e "1\nq" | gpg --command-fd 0 --search-keys "joel@debian.org"
for trusted in "torvalds@kernel.org" "gregkh@kernel.org" "joel@debian.org"; do
    echo -e "5\ny\n" | gpg --command-fd 0 --expert --edit-key $trusted trust
done

if [[ $( ccache --version | grep -c 3.7.8 ) -lt 1 ]]; then 
    cd $HOME/src
    wget "https://github.com/ccache/ccache/releases/download/v3.7.8/ccache-3.7.8.tar.xz"
    wget "https://github.com/ccache/ccache/releases/download/v3.7.8/ccache-3.7.8.tar.xz.asc"

    if [[ $(gpg --verify ccache-3.7.8.tar.xz.asc ccache-3.7.8.tar.xz 2>&1 \
        | grep -c "Good signature .* \[ultimate\]") -lt 1 ]]; then
        echo "Error: invalid source"
        exit 1
    else
        echo "Source verified"
    fi

    tar xvf ccache-3.7.8.tar
    cd $HOME/src/ccache-3.7.8
    ./configure
    make -j$(nproc)
    sudo make -j$(nproc) install
fi



if [[ $Git ]]; then
    #echo "git"
    git clone --depth 1 --single-branch --branch v$V \
        "https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git" \
        $TargetDir || exit 1
    cd $TargetDir

    if [[ $(git tag -v v$V 2>&1 \
        | grep -c "Good signature .* \[ultimate\]") -lt 1 ]]; then
        echo "Error: invalid source"
        exit 1
    else
        echo "Source verified"
    fi

else 
    #echo "wget"
    cd $TargetDir
    wget "https://cdn.kernel.org/pub/linux/kernel/v${M}.x/linux-${V}.tar.xz"
    wget "https://cdn.kernel.org/pub/linux/kernel/v${M}.x/linux-${V}.tar.sign"
    xz -d -v linux-${V}.tar.xz

    if [[ $(gpg --verify linux-$V.tar.sign linux-$V.tar 2>&1 \
        | grep -c "Good signature .* \[ultimate\]") -lt 1 ]]; then
        echo "Error: invalid source"
        exit 1
    else
        echo "Source verified"
    fi
    tar xvf linux-${V}.tar
fi

sudo rm /etc/dracut.conf.d/xen.conf
cd ${TargetDir}
cd linux-${V}
sudo cp /boot/config-`uname -r` .config 
yes '' | ccache make -j$(nproc) localmodconfig
ccache make -j$(nproc) 
ccache make -j$(nproc) modules
sudo make -j$(nproc) modules_install
sudo make -j$(nproc) install

if [[ $Reboot ]]; then
    sudo reboot 
fi

0 个答案:

没有答案