LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-26-2012, 08:31 AM   #1
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Rep: Reputation: 30
else: endif not found in csh


I'm getting a 'else: endif not found' error when running the below script to check the number of files in 2 folders is the same. The syntax looks right to me but the output I get is:
Code:
Checking no of files in source and destination.
       2 /
       0 /mnt/Backup/
Items in root locally:
0

Items on dest:
0
All files copied
else: endif not found
Code:
#!/bin/csh

set noFilesLocal = 0
set noFilesRemote = 0

echo 'Checking no of files in source and destination.'

set noFilesLocal = ls | wc -l /
set set noFilesRemote = ls | wc -l /mnt/Backup/

if($noFilesLocal == $noFilesRemote) then
	echo 'Items in root locally:'
	echo $noFilesLocal
	echo ''
	echo 'Items on dest:'
	echo $noFilesRemote
	
	echo 'All files copied'
else
	echo 'Items in root locally:'
	echo $noFilesLocal
	echo ''
	echo 'Items on dest:'
	echo $noFilesRemote
	
	echo 'Not all files copied'
endif
 
Old 06-26-2012, 08:59 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
This looks wrong:
Quote:
set set noFilesRemote = ls | wc -l /mnt/Backup/
Also there should be a space before left paren
Quote:
if (
 
Old 06-27-2012, 04:21 AM   #3
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Original Poster
Rep: Reputation: 30
Thanks, I don't know how I didn't spot the two sets. I've also got a working csh script with no space before the parenthesis.

I've changed the start of the file before the if because, as you can see from the output in the first post, the noFilesLocal and noFilesRemote variables are always going to be different as they include file paths. I've been trying to split them on the space character and use that to compare them but I'm having some difficulty with the syntax.
Code:
#!/bin/csh

echo 'Checking no of files in source and destination.'

set noFilesLocal = `ls | wc -l /`
echo $noFilesLocal
set noFilesRemote = `ls | wc -l /mnt/Backup/`
echo $noFilesRemote

set localArr = awk split($noFilesLocal,localArr, ' ')
echo $localArr
set noFilesLocal = $localArr[1]

echo $noFilesLocal
Which gives:
Quote:
Checking no of files in source and destination.
2 /
0 /mnt/Backup/
set: Syntax error
Update:
I found that cd'ing to the directory first instead of passing in the directory gives the number of items only without the file path being appended:
Code:
cd /
set noFilesLocal = `ls | wc -l`
echo $noFilesLocal

cd /mnt/Backup/
set noFilesRemote = `ls | wc -l`
echo $noFilesRemote

Last edited by Mark_667; 06-27-2012 at 04:41 AM. Reason: Found solution
 
Old 06-27-2012, 04:34 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
that awk will not work also:
Code:
set var = `echo $noFilesLocal | awk ' split ($0, ..... '` # use " " inside awk, not ' '
but in general you cannot manipulate shell variables within awk. It is another language.
 
  


Reply



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
csh is gone. Where can I find it? Agresvig Linux - Software 1 09-26-2007 03:15 PM
csh nirmaltom Programming 1 05-18-2007 08:07 AM
csh script stellafrank Linux - Newbie 3 11-15-2006 06:11 PM
what does #ifndef and #endif means ??? indian Programming 4 03-06-2005 09:51 AM
csh zaza Linux - Newbie 6 05-21-2002 06:26 AM

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

All times are GMT -5. The time now is 02:41 PM.

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