jeudi 10 novembre 2016

If: Expression syntax - Error with a C Shell Script

I'm working on debugging a C-shell script that was given to me to add options to. This script, clean, is supposed to display the name of each file in a given directory and allows the user to decide whether or not keep or delete them. The problem is, when executed with a directory as an argument (or without) the error "If: Expression Syntax." Here's what it looks like.

## script name: clean
## helps to remove unwanted files from a directory


if ($#argv !=1) then
    echo usage: $0 directory; exit(1)
else if (! -d $1) then
    echo $1 not a directory; exit(1)
endif

set dir = $1
chdir $dir
set files = *

# process files
foreach file ($files)
    if (! -f $file) continue
    echo ' ' ## gives a blank line
    echo "file = $file" ## identifies file being processed
    echo ' '
    head $file
    while (1)
        echo -n rm $file '?? (y, n, \!, or q)' :
        set c = $<
        switch ($c)
            case y:
                if ( {rm $file} ) then
                    echo '*****' $file rm-ed
                else
                    echo cannot rm $file
                endif
                    break ## break out of while
            case n:
                echo '*****" $file not rm-ed
                break
            case q:
                exit(0)
            case \!:
                echo command:
                eval $<
                ## in $< the variable $file can be used
            endsw
        end ## of while
    end ## of foreach

I've tried including a "#!bin/csh" at the top on a FreeBsd remote system that uses C Shell. I also can't put it in my /usr/bin on my local Ubuntu 16.04 LTS system, in which I have C Shell installed, added to the default Bash.

If anyone has any suggestions I'd greatly appreciate it, thanks.

Aucun commentaire:

Enregistrer un commentaire