LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2012, 06:07 AM   #1
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Rep: Reputation: 30
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?
 
Old 06-19-2012, 07:17 AM   #2
dayid
Member
 
Registered: Apr 2012
Location: Austin, TX
Posts: 44

Rep: Reputation: Disabled
Quote:
Originally Posted by Mark_667 View Post
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
 
Old 06-19-2012, 07:46 AM   #3
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Original Poster
Rep: Reputation: 30
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...]
#
 
Old 06-19-2012, 07:59 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
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?
 
Old 06-19-2012, 08:16 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
How about just running the command and then testing the error code:
<command>
if [ $? ] then <do something>; else <do something else>; endif
 
Old 06-19-2012, 10:26 AM   #6
dayid
Member
 
Registered: Apr 2012
Location: Austin, TX
Posts: 44

Rep: Reputation: Disabled
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"
 
Old 06-20-2012, 04:48 AM   #7
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Original Poster
Rep: Reputation: 30
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'?
 
Old 06-20-2012, 07:42 AM   #8
dayid
Member
 
Registered: Apr 2012
Location: Austin, TX
Posts: 44

Rep: Reputation: Disabled
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"
 
Old 06-20-2012, 10:47 AM   #9
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Original Poster
Rep: Reputation: 30
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
 
  


Reply

Tags
csh


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] source SYBASE.csh problem... syntax error: unexpected end of file ghostshell Linux - Software 2 02-12-2012 08:06 AM
multiple expression if statement s_linux SUSE / openSUSE 4 02-11-2012 08:02 AM
gfortran: Syntax error in DATA statement at (1) science_guy Programming 1 10-16-2009 12:10 AM
tricky syntax for passing arguments in csh aliases frerejacques Programming 1 03-23-2009 03:20 PM
multi-expression 'test' (syntax?) error gw1500se Mandriva 1 04-26-2005 08:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration