I want to test if a clearcase view exists and execute the remove command only if it exists. I am trying to do it from a shell script in a Linux 6.x environment. I tried to format my conditions both as one-liner as well as full "if" statement, but don't seem to be able to get it to work. What do I need to do to get both - one-liner and full if syntax - approaches working?
Here is the code, in its latest state
#!/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
I would prefer a one-liner type of solution, but 'if' statement will also work.
Provided the command follows the UNIX-convention of exit codes, the general one-liner looks like:
command && { success1; success2; } || { failure1; failure2; }
The list following &&
specifies what should run when command succeeds (exits with 0
), while the list following ||
specifies what should run when the command fails. In the list, note that all the commands end in a semi-colon, including the last one.
For your specific case, this looks like it'll work:
"${ctdir}"/cleartool lsview "${viewname}" && { "${ctdir}"/cleartool rmview "${viewname}" && echo "view removed" || echo "cannot remove view"; }
Here is an example of this pattern in action, using standard commands:
$ 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