对一个算术条件和一个非算术条件进行逻辑"或"运算

  • 本文关键字:条件 quot 一个 运算 shell
  • 更新时间 :
  • 英文 :


我对Unix/Linux shell脚本几乎没有经验,对算术和逻辑运算符也没有经验。从我在文档中看到的情况来看,这个符号简直是一场噩梦!我有一项简单的任务要做,我不清楚哪种符号会给我正确的结果,所以我想我应该在这里问。

如果自上次发出以来已经过了一定时间,或者上一个票证已经过期,我想生成一个新的Kerberos票证。我可以分别检查这些,并在每种情况下运行相同的代码:

maxIterations=480 # 4 hours, given a 30-second loop
iteration=0
kinit ... [generate the first Kerberos ticket]
while true
do
sleep 30 # short-duration loop because in the real application
# I'm also testing other conditions that could arise at any time,
# not just whether a new ticket should be issued
iteration=`expr $iteration + 1`
if [ $iteration -eq $maxIterations ]
then
echo "Requesting new Kerberos ticket"
kinit ...
fi
if ! klist -f -s
then
echo "Requesting new Kerberos ticket"
kinit ...
fi
# other checks here
done

但是,当然,我不想重复代码,所以我想知道我可以使用什么语法来";OR";将算术比较和对调用CCD_ 1返回的状态的测试放在一起。

对现有代码的较小更改是:

iteration=$((iteration + 1))
if [ "$iteration" -ge "$maxIterations" ] || ! kinit -f -s; then

但更好的方法(如果这真的是bash脚本,而不是sh脚本(是:

if (( ++iteration >= maxIterations )) || ! kinit -f -s; then

注意,在算术上下文中使用++iteration意味着您可以去掉上面的expr行。

最新更新