bash - 首次运行 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 行的语句没有可比较的内容。我在随后的运行中很好,因为文件已生成并且有一个要比较的值。
处理这个问题的“正确”方法是什么?理论上,我可以创建一个虚拟文件(但这解决了第一个错误,而不是第二个错误)。理论上,我可以添加另一个循环来检查临时存储文件是否存在,但这感觉有点矫枉过正。
解决方案
代替
if [ $latestripcord == $lastdownloadripcord ];then
和
if [[ $latestripcord == $lastdownloadripcord ]];then
推荐阅读
- javascript - 单击按钮时查找图像使用jquery parent
- python - 如果匹配,则需要匹配 2 个不同的 pandas 数据框的 2 列,我们需要附加新数据
- java - 再次调用时如何重新启动异步方法?
- powerbi - Power BI 嵌入仪表板:针对 /powerbi/refresh/subscribe 阻止的跨源请求
- inner-classes - 使用 Xtext、Xbase 和 JvmModelInferrer 从内部类引用包含类
- c - 显示完整的 Linux 命令行(gcc 输出)?(虚拟机上的c编程)
- java - 单击按钮时如何隐藏通知?
- python - 模块“pygame”没有“退出”成员
- asp.net-mvc - IControllerFactory 'System.Web.Mvc.DefaultControllerFactory' 没有返回名称为 'Home' 的控制器
- c - 在c问题中实现malloc和free函数