LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   else: endif not found in csh (https://www.linuxquestions.org/questions/programming-9/else-endif-not-found-in-csh-4175413463/)

Mark_667 06-26-2012 08:31 AM

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


smallpond 06-26-2012 08:59 PM

This looks wrong:
Quote:

set set noFilesRemote = ls | wc -l /mnt/Backup/
Also there should be a space before left paren
Quote:

if (

Mark_667 06-27-2012 04:21 AM

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


pan64 06-27-2012 04:34 AM

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.


All times are GMT -5. The time now is 03:54 PM.