Search code examples
bashsyntaxcsh

Expression syntax with if command using curly braces in csh


I'm trying to check if a volume is mounted using a csh script.

This code works

#!/bin/csh
set MOUNT_FOLDER = "/Volumes/AAA"
if ( `mount | grep -c "on $MOUNT_FOLDER"` == 0 ) then
    echo Not mounted
else
    echo Mounted
endif

but I would like to try using the syntax with { } and the exit code of grep. I've tried with

if ( { mount | grep -q "on $MOUNT_FOLDER" } ) then
...

but it prints the mount output and it regardless the value of $MOUNT_FOLDER the expression is always true.


Solution

  • Unlike in bash, if You have piped commands in csh and want get command exit status You need to encapsulate in sub-shell ( ... | ... )

    So following should work for You:

    if ( { ( mount | grep -q "on $MOUNT_FOLDER" ) } ) then
    ...