使用 shell 脚本删除 clearcase 视图(如果存在) 使用一个衬里(如果)

Using shell scripts to remove clearcase view if it exists using one liner if

我想测试是否存在clearcase视图,只有存在时才执行remove命令。我正在尝试在 Linux 6.x 环境中从 shell 脚本执行此操作。我试图将我的条件格式化为一行和完整的 "if" 语句,但似乎无法让它工作。我需要做什么才能同时获得 - 一行和完整的语法 - 方法?

这是最新状态的代码

#!/bin/ksh
#
STREAMNAME=app_stream_int
PVOB=domain_pvob
VOB=domain_app

viewdir=/opt/local/software/rational/viewstorage
shareddir=/opt/shared/test
storagedir=${shareddir}/viewstorage
projectdir=${shareddir}/projects

ctdir=/opt/rational/clearcase/bin
viewname=$viewdir/test_$STREAMNAME.vws
viewtag=test_$STREAMNAME

echo "STREAMNAME $STREAMNAME - PVOB $PVOB - VOB $VOB"
echo "Removing View if it exists ... \n"

#  [ $(${ctdir}/cleartool lsview ${viewname}) ] && { ${ctdir}/cleartool rmview ${viewname}; echo "view removed" }

#  [ ${ctdir}/cleartool lsview -long ${viewtag} ] && { ${ctdir}/cleartool rmview ${viewname}; echo "view removed" }

#  ${ctdir}/cleartool lsview -long ${viewtag} | grep "Tag" && { ${ctdir}/cleartool rmview ${viewname}; echo "view removed" }

if [ ${ctdir}/cleartool lsview -long ${viewtag} | grep 'Tag' == "0" ]
then
    echo "view found"
    ${ctdir}/cleartool rmview ${viewname}
fi

我更喜欢单行类型的解决方案,但 'if' 语句也可以。

如果命令遵循退出代码的 UNIX 约定,general 一行看起来像:

command && { success1; success2; } || { failure1; failure2; }

&& 后面的列表指定了当命令成功时应该 运行 的内容(以 0 退出),而 || 后面的列表指定了应该 运行 命令失败时。在列表中,请注意所有命令都以分号结尾,包括最后一个。

对于您的具体情况,这看起来可行:

"${ctdir}"/cleartool lsview "${viewname}" && { "${ctdir}"/cleartool rmview "${viewname}" && echo "view removed" || echo "cannot remove view"; }

下面是这个模式的一个例子,使用标准命令:

$ ls foo && { rm -f foo && echo 'removed' || echo 'not removed'; }
ls: cannot access foo: No such file or directory

$ touch foo
$ ls foo && { rm -f foo && echo 'removed' || echo 'not removed'; }
foo
removed

$ sudo touch /foo
$ sudo chmod 600 /foo
$ ls /foo && { rm -f /foo && echo 'removed' || echo 'not removed'; }
/foo
rm: cannot remove ‘/foo’: Permission denied
not removed