首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从tarball升级Firefox量子

从tarball升级Firefox量子
EN

Code Review用户
提问于 2018-07-22 16:24:42
回答 2查看 277关注 0票数 9

我编写了一个小脚本来自动将Firefox升级到Debian下的一个新版本,因为我不想从不稳定的地方安装它,而且snap版本存在呈现问题。

欢迎您提出任何意见,我是否遗漏了什么或潜在的问题?

代码语言:javascript
运行
复制
#!/bin/bash

# firefox_upgrade - program to upgrade firefox quantum

error_exit()
{
    echo "$1" 1>&2
    exit 1
}

firefox_path=""
firefox_file=""

# parsing path and filename
if [ $# -ne 1 ]; then
    error_exit "usage: $0 firefox_quantum_path"
else
    firefox_path="$1"
    firefox_file="${firefox_path##*/}"
fi

# checking if input is a valid file
if [ ! -f "$firefox_path" ]; then
    error_exit "Invalid file! Aborting."
fi

# removing previous install, if existent
firefox_bin="/opt/firefox"
if [ -e "$firefox_bin" ]; then
    rm -rf $firefox_bin
else
    echo "$firefox_bin doesn't exist."
fi

# removing previous symlink, if existent
firefox_link="/usr/bin/firefox-quantum"
if [ -f "$firefox_link" ]; then
    rm $firefox_link
else
    echo "$firefox_link doesn't exist."
fi

# copying the tar to /opt
rsync -ah --progress $firefox_path /opt/$firefox_file

# unpacking the tar if successfully changed directory
if cd /opt; then
    tar -jxvf $firefox_file
else
    error_exit "Could not change directory! Aborting."
fi

# if unpack was successful, set permissions, create symlink, and remove tar
if [ "$?" = "0" ]; then
    chmod 755 /opt/firefox
    ln -s /opt/firefox/firefox /usr/bin/firefox-quantum
    rm $firefox_file
else
    error_exit "Could not extract file! Aborting."
fi
exit 0
EN

回答 2

Code Review用户

回答已采纳

发布于 2018-07-22 17:57:41

很不错的剧本,我有很多小建议。

总是在命令参数

中使用双引号变量。

这些命令中的参数应该双引号,以防止分词和全球化:

rsync -ah -- $firefox_path /opt/$firefox_file .rm $firefox_link ..。焦油-jxvf $firefox_file ..。rm $firefox_file

避免在脚本

中更改工作目录

更改工作目录通常容易出错,而且令人困惑,因此,在不更改工作目录的情况下,寻找方法来完成所需的工作。

如果cd /opt;则tar -jxvf $firefox_file error_exit“无法更改目录!中止”。fi

另一种不更改目录(并更正引用)编写此文件的方法:

代码语言:javascript
运行
复制
tar -jxvf "/opt/$firefox_file" -C /opt

检查命令的退出代码是非常好的,这种写作风格实际上可以帮助您做到这一点,方法是在结尾处进行简化:

代码语言:javascript
运行
复制
if ! tar -jxvf "/opt/$firefox_file" -C /opt; then
    error_exit "Could not extract file! Aborting."
fi

chmod 755 /opt/firefox
ln -s /opt/firefox/firefox /usr/bin/firefox-quantum
rm "/opt/$firefox_file"

在文件

的顶部声明常量

有些常量值对程序的行为非常重要,但它们被隐藏在代码中。最好将它们移到文件顶部附近的某个位置,在那里它们很容易被看到和更改。

firefox_bin="/opt/firefox“firefox_link=/usr/bin/firefox-量子版

还有一些其他值不在变量中,最好为它们定义变量,例如,以下命令中的变量:

-s /opt/firefox/firefox /usr/bin/firefox-量程

技术

在这里,else分支是不必要的,因为如果条件为真,脚本无论如何都会退出。我建议放弃else,只需在条件之后移动它的身体(就像在其他地方一样)。

如果;那么error_exit“用法:$0 firefox_quantum_path”firefox_path="$1“firefox_file=”${firefox_path=##**/}“fi

要将变量设置为空,只需编写如下代码:

代码语言:javascript
运行
复制
firefox_path=
firefox_file=

在脚本的末尾使用exit 0通常不是一个好主意。这会强制退出代码0(成功)。脚本的退出代码是最后一个命令的退出代码。通过将其强制为0,最后一个命令的失败将被错误地掩盖。

票数 8
EN

Code Review用户

发布于 2020-04-10 01:18:50

我发现您的脚本在我使用它的时候非常有用(自从您最初发布这个问题后不久)。不过,我做了一些修改。主要是,我的版本没有将原始tarball复制到/opt (这是不必要的I/O,特别是因为它在解压缩后立即被删除);相反,它使用tar's --one-top-level参数将tarball从其原始位置提取到/opt,而不复制它或更改工作目录。另外,我的版本并不实际删除现有的安装和符号链接,它只是在必要时覆盖它们。

代码语言:javascript
运行
复制
#!/bin/bash

# firefox-upgrade - program to upgrade firefox quantum

error_exit() {
    echo "$1" 1>&2
    exit 1
}

# parsing path and filename
if [ "$#" -ne 1 ]; then
    error_exit "$0: usage: $0 firefox_quantum_path"
fi
firefox_path="$1"

# checking if input is a file
if [ ! -f "$firefox_path" ]; then
    error_exit "Not a file! Aborting."
fi

# unpacking the tar
if tar --overwrite --one-top-level=/opt -jxvf "$firefox_path"; then
    # if unpack was successful create symlinks if they don't
    # already exist or don't point to the right target
    if [ "$(readlink /usr/bin/firefox 2> /dev/null)" != /opt/firefox/firefox ]; then
        ln -fs /opt/firefox/firefox /usr/bin/firefox
    fi
    if [ "$(readlink /usr/bin/firefox-quantum 2> /dev/null)" != /opt/firefox/firefox ]; then
        ln -fs /opt/firefox/firefox /usr/bin/firefox-quantum
    fi
else
    error_exit "Could not extract file! Aborting."
fi
exit 0

此外,我还修改了脚本,以便与Thunderbird一起使用:

代码语言:javascript
运行
复制
#!/bin/bash

# thunderbird-upgrade - program to upgrade thunderbird

error_exit() {
    echo "$1" 1>&2
    exit 1
}

# parsing path and filename
if [ "$#" -ne 1 ]; then
    error_exit "$0: usage: $0 thunderbird_path"
fi
thunderbird_path="$1"

# checking if input is a file
if [ ! -f "$thunderbird_path" ]; then
    error_exit "Not a file! Aborting."
fi

# unpacking the tar
if tar --overwrite --one-top-level=/opt -jxvf "$thunderbird_path"; then
    # if unpack was successful create symlinks if they don't
    # already exist or don't point to the right target
    if [ "$(readlink /usr/bin/thunderbird 2> /dev/null)" != /opt/thunderbird/thunderbird ]; then
        ln -fs /opt/thunderbird/thunderbird /usr/bin/thunderbird
    fi
else
    error_exit "Could not extract file! Aborting."
fi
exit 0
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/200044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档