Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 08 Sep 2007 20:26:47 -0700
From:      Garrett Cooper <youshi10@u.washington.edu>
To:        Giorgos Keramidas <keramida@ceid.upatras.gr>
Cc:        Grant Peel <gpeel@thenetnow.com>, freebsd-questions@FreeBSD.ORG
Subject:   Re: csh if..then delhema.
Message-ID:  <46E367F7.6060705@u.washington.edu>
In-Reply-To: <20070909020657.GA4912@kobe.laptop>
References:  <000801c7f274$6fae71e0$6501a8c0@GRANT> <20070909020657.GA4912@kobe.laptop>

next in thread | previous in thread | raw e-mail | index | archive | help
Giorgos Keramidas wrote:
> On 2007-09-08 20:00, Grant Peel <gpeel@thenetnow.com> wrote:
>   
>> Hi all,
>>
>> I have tried every escape sequence I can think of, and I still get
>> Division by 0 error here..
>>
>>  if ($filesystem == "\/") then
>>                         $fsname = $fsnm1
>>                 elseif ($filesystem == '/var') then
>>                         $fsname =$fsnm2
>>                 elseif ($filesystem == '/usr') then
>>                         $fsname = $fsnm3
>>                 elseif ($filesystem == '/home') then
>>                         $fsname = $fsnm4
>>                 else
>>                         $fsname = 'GREATERTHAN4
>>
>> Any ideas how to excape the forward slashes in the if statemnt?
>>     
>
> Use a better scripting language?
>
> Seriously now, unless you are willing to experiment with csh until you
> get its 'weird' escaping rules to work, you should consider using
> something with a more predictable way of escaping string literals.
>
> For example, there is nothing above which cannot be done a lot more
> easily with Perl and a hash table:
>
>     %fsmap = (
>       '/'     => $fsnm1,
>       '/var'  => $fsnm2,
>       '/usr'  => $fsnm3,
>       '/home' => $fsnm4,
>     );
>
>     $fsname = $fsmap{$filesystem} or 'unknown';
>
> Using the hash results in much 'cleaner' code too.
>
> Now, go forth and convert a csh script to Perl, Python, or something
> with a cleaner syntax :)
>
> - Giorgos
>   

    Or if you want to stick with Unix scripting...

#!/bin/sh

case "$filesystem" in
    '/') fsname=$fsnm1;;
    '/var') fsname=$fsnm2;;
    '/usr') fsname=$fsnm3;;
    '/home') fsname=$fsnm4;;
    *) echo "Oops.. that fs is unknown"; exit 1 ;;
esac

    There ya go. The single quotes are optional in the case statement, 
but bourne compatible shells are semi-regex intelligent, so to avoid to 
any problems, I single-quoted the strings.

    tcsh can burn in hell for all I care. It's a horrible shell IMNHO 
(in my not-so humble opinion). Now if I could only convince the rest of 
the EE community to agree, that'd be nice. Trolls welcome :).

-Garrett



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?46E367F7.6060705>