循环中的变量赋值

时间:2013-12-16 09:28:55

标签: shell unix variable-assignment

我正在编写一个shell脚本,用于读取和报告时间戳(当前时间戳存储在$g2中)。最后我将比较两个时间戳。

我想检查$t2是否为空 - 如果是,则将其存储为$g2。 如果没有,则将$t2替换为$t1,并将新的$g2值存储为$t2

我试图在sudo csv样式文件中输出 - 但是当我尝试运行此脚本时,我一直得到命令未找到错误。有什么建议吗?

if
    [[ -z "$t2" ]]
then
    $t2=$g2
    echo "$t1,0,0" >> just_time.txt
else
    $t2=$t1
    $t2=$g2
    echo "$t2,$t1,0" >> just_time.txt
fi

1 个答案:

答案 0 :(得分:1)

试试这个:

if
    [[ -z "$t2" ]]
then
    t2=$g2
    echo "$t1,0,0" >> just_time.txt
else
    t1=$t2
    t2=$g2
    echo "$t2,$t1,0" >> just_time.txt
fi