首页 > 解决方案 > 首次运行 bash 脚本时需要一元运算符 - 我该如何解决这个问题?

问题描述

#!/bin/bash

#script complains if our lastdownfile dosen't exist.
latestripcord=`elinks -dump https://cancel.fm/ripcord/ -no-numbering| grep Ripcord_Win  | cut -c 5-`
#standard elinks dump scripty goodness. the cut -c 5- is to strip the leading zero
lastdownloadripcord=`cat lastdownload`
#we have stored the last download in a text file.
version=`echo $lastdownloadripcord | cut -d _ -f3| cut -d . -f 1-3`
#maybe we can do more with the version?
if [ $latestripcord == $lastdownloadripcord ];then
    echo "latest version $version installed"
#to do - strip out and store the latest version number somewhere for use in the script.
else
    echo $latestripcord|tee lastdownload| curl -sS $latestripcord > ripcord.zip
    unzip ripcord.zip -d ./ripcord
#if we  have a new version, update last downloaded version and download  latest. Unzip to its own dir
fi

本质上 - 我正在为作为 zip 文件提供的应用程序倾倒下载链接,存储链接以供稍后比较,以便我知道新版本已经发布,如果是,则下载最新版本。

通过编辑最后一个下载文件,我已经用“假”新版本对此进行了测试。但是在第一次运行时,没有 lastdownload 文件,这会导致 2 条错误消息。

cat: lastdownload: No such file or directory
./autoripcord.sh: line 9: [: https://cancel.fm/dl/Ripcord_Win_0.4.24.zip: unary operator expecte

前者是因为没有 lastdownload 文件,后者是因为第 9 行的语句没有可比较的内容。我在随后的运行中很好,因为文件已生成并且有一个要比较的值。

处理这个问题的“正确”方法是什么?理论上,我可以创建一个虚拟文件(但这解决了第一个错误,而不是第二个错误)。理论上,我可以添加另一个循环来检查临时存储文件是否存在,但这感觉有点矫枉过正。

标签: bash

解决方案


代替

if [ $latestripcord == $lastdownloadripcord ];then

if [[ $latestripcord == $lastdownloadripcord ]];then

推荐阅读