如何测试是否需要 kinit?

How to test if a kinit is needed?

如果我需要的话,我想将一些东西添加到我的 .bashrc 文件到 运行 和 kinit。有没有办法测试我是否需要做 kinit?像这样:

if [ kinitNeeded ]; 
    do kinit; 
done

kinitNeeded() { ??? }

你可以试试 klist -s。来自手册页:

"causes klist to run silently (produce no output), but to still set the exit status according to whether it finds the credentials cache. The exit status is ‘0’ if klist finds a credentials cache, and ‘1’ if it does not or if the tickets are expired."

我找到了解决方案,但有点麻烦。

if [ `klist 2>&1 | grep -i 'No credentials' | wc -l` -gt 0 ]; then
    kinit
fi

尝试:

klist -s; echo $?

Return 0 如果可以,1 否则