From: Ed Morton on
On 2/23/2010 12:33 AM, Melvin wrote:
> Hi,
>
> I was trying to compare if the last lline of a file is $end in tcsh.
>
> I tried this:
> ######################
> set string1 = `cat file|tail -1`
> set string2 = "\$end"
>
> if (string1 != string2) then
> echo "Not matching"
> endif
> #####################
>
> But shell doesn't allow to compare "$end"[I tries adding the \ to
> remove the effect of "$" in $end]
> Error: $end: undefined variable
>
> Thanks in Advance
> Shell Baby

In general, don't write scripts in [t]csh. See question 17 in the FAQ,
http://shell.cfajohnson.com/cus-faq-2.html#17.

To do what in a bourne-type shell you could do:

string1=$(tail -1 file)
string2='$end'
if [ "$string1" != "$string2" ]
then
echo "Not matching"
fi

or just use awk from any shell:

awk 'END{ if ($0 != "$end") print "Not matching" }' file

Regaqrds,

Ed.
From: Maxwell Lol on
Melvin <whereismelvin(a)gmail.com> writes:

> Hi,
>
> The undefined Variable problem is solved.
>
> Sln: set string2 = "\$\end"
>
> But now string2 assignment results in an error "VARIABLE NAME MUST
> CONTAIN ALPHANUMERIC CHARACTERS"
>
> Any Suggestions

This is a tcsh issue. My suggestion: Don't prolong the misery. Change
to a different shell. Save your sanity.

Here is a contest. Guess what the following commands do in tcsh. Then
execute them and see what happens.

set a='$'
set a="$"
set a="\$"
set a=$


In your case, this works
set string2='$'
set string2="${string2}end"

The last line could also be

set string2=${string2}end