Bash比较存储的"布尔"值与什么?

Alg*_*fic 3 bash

我正在尝试创建一个接受一个参数,一个文件的程序,然后在60秒后检查文件发生了什么.为此,我需要将结果存储-e $1在变量中,然后在60秒后检查它.我似乎无法让if表达听我说,我知道这是错的.出于测试目的,此脚本会立即打印出比较结果.期待这个工作的样本,我不知道我对这个小程序做了多少版本.谢谢!明天到期,非常感谢任何帮助!

#!/bin/bash
onStartup=$(test -e $1) 
if [ -e "$1" ]; then
    unixtid1=$(date  +"%s" -r "$1") #To check if the file was edited. 
    echo $unixtid1
fi
sleep 3

#Here trying to be able to compare the boolean value stored in the
#start of the script. True/False or 1 or 0? Now, both is actually printed. 
if [[ $onStartup=1 ]]; then
    echo "Exists"
fi

if [[ $onStartup=0 ]]; then
    echo "Does not exists"
fi
Run Code Online (Sandbox Code Playgroud)

Ale*_*yak 5

使用$?特殊的shell变量来获取命令的结果.记住0手段的返回值true.这是修改后的脚本

#!/bin/bash
test -e $1
onStartup=$?

if [ $onStartup -eq 0 ]; then
unixtid1=$(date  +"%s" -r "$1") #To check if the file was edited. 
echo $unixtid1
fi
sleep 3

#Here trying to be able to compare the boolean value stored in the
#start of the script. True/False or 1 or 0?
if [[ $onStartup -eq 0 ]]; then
echo "Exists"
else
echo "Does not exists"
fi
Run Code Online (Sandbox Code Playgroud)

您的原始示例尝试test在onStartup变量中存储命令的文字输出.该test命令的文字输出是一个空字符串,这就是为什么你没有看到任何输出.