LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   if: Expression syntax error in csh if statement (https://www.linuxquestions.org/questions/programming-9/if-expression-syntax-error-in-csh-if-statement-4175412232/)

Mark_667 06-19-2012 06:07 AM

if: Expression syntax error in csh if statement
 
I've got a simple csh script that checks for the exit code of grep and echos the result:
Code:

#!/bin/csh

if (df -k | /usr/xpg4/bin/grep -q /mnt/Backup == 0) then

        echo 'Mount worked'
else
        echo 'Mount failed'
endif

Unfortunately I keep getting: if: Expression syntax
Can anyone see what's wrong with it?

dayid 06-19-2012 07:17 AM

Quote:

Originally Posted by Mark_667 (Post 4706842)
I've got a simple csh script that checks for the exit code of grep and echos the result:
Code:

#!/bin/csh

if (df -k | /usr/xpg4/bin/grep -q /mnt/Backup == 0 ) then

        echo 'Mount worked'
else
        echo 'Mount failed'
endif

Unfortunately I keep getting: if: Expression syntax
Can anyone see what's wrong with it?

The problem is in your check/compare statement:
Code:

(df -k | /usr/xpg4/bin/grep -q /mnt/Backup == 0)
There needs to be just one value to each side of the compare symbol (in this case, "==").

Try this:
Code:

#!/bin/csh

if ( `df -k | /usr/xpg4/bin/grep -q /mnt/Backup` == 0) then

        echo 'Mount worked'
else
        echo 'Mount failed'
endif


Mark_667 06-19-2012 07:46 AM

Thanks, that solved that problem, unfortunately I'm coming across more strangeness.
The script says the mount failed but it's definitely listed in the df output:
Code:

{root}: CheckMount.sh
Mount failed
{root}: df -k
Filesystem            kbytes    used  avail capacity  Mounted on
/dev/dsk/c0t0d0s0    21268609 3865380 17190543    19%    /
/proc                      0      0      0    0%    /proc
fd                        0      0      0    0%    /dev/fd
mnttab                    0      0      0    0%    /etc/mnttab
swap                2091208      24 2091184    1%    /var/run
swap                2091200      16 2091184    1%    /tmp
/dev/dsk/c0t0d0s7    15120459 2131387 12837868    15%    /export/home
<BackupServer>:/Solaris_Backups/<Solaris box>
                    2930134008 2499720736 430413272    86%    /mnt/Backup

Running the if statement outside of the script gives:
Code:

{root}: `df -k | /usr/xpg4/bin/grep -q /mnt/Backup`
usr/ucb/: Permission denied
{root}: su
# `df -k | /usr/xpg4/bin/grep -q /mnt/Backup`
: Command not found
# /usr/xpg4/bin/grep
Usage:  grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [file ...]
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern... [-f pattern_file]...[file...]
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [-e pattern]... -f pattern_file [file...]
#


acid_kewpie 06-19-2012 07:59 AM

if you run
Code:

# `something`
then you're trying to run the *output* of the command, not just running the command, see? If you try running something simple...
Code:

# `echo ls`
then you'll see that the echo command prints out the result "ls" but being in backticks it then proceeds to run ls itself... yes?

pixellany 06-19-2012 08:16 AM

How about just running the command and then testing the error code:
<command>
if [ $? ] then <do something>; else <do something else>; endif

dayid 06-19-2012 10:26 AM

Also, outside the syntax issue - why use df to check if something is mounted rather than just using mount?

The below will also work for csh:
Code:

#!/bin/sh
mount | grep "/mnt/Backup" > /dev/null 2>&1 && echo "It's mounted" || echo "Not there"


Mark_667 06-20-2012 04:48 AM

So far I've got:
Code:

#!/bin/csh

set isMounted = 'Not Mounted'
mount | grep /mnt/Backup >& ./IsMounted.log

set isMounted = `wc -l ./IsMounted.log`

if(isMounted == '1 ./IsMounted.log') then
        echo 'Mount worked'
        echo $isMounted
else
        echo 'Mount failed'
        echo $isMounted
endif

which outputs:
Quote:

Mount failed
1 ./IsMounted.log
I don't understand how it's failing the equality test. I'd also like to be able to do it without having to redirect output to another file and count in the lines but this is the only way I can get it to work at the moment.

dayid, your code gives:
Ambiguous output redirect
I think it's the '2>&1' but changing this to the more csh friendly >& gives 'Missing name for redirect'?

dayid 06-20-2012 07:42 AM

So you tried it w/o the data redirects also, right?
Code:

#!/bin/csh
mount | grep "/mnt/Backup"  && echo "It's mounted" || echo "Not there"


Mark_667 06-20-2012 10:47 AM

Quote:

So you tried it w/o the data redirects also, right?
Ahh, I didn't know you could do that. I eventually settled on this:
Code:

#!/bin/csh

set isMounted = false

mount | grep "/mnt/Backup" && set isMounted=true || set isMounted=false

if($isMounted == 'true') then
        echo 'Mount worked'
        echo $isMounted
else
        echo 'Mount failed'
        echo $isMounted
endif



All times are GMT -5. The time now is 12:44 AM.