Spot the difference

I posted earlier with a problem it's here, I have edited the script a little and it tells me once more that the end of line is unexpected and I'm really lost with this one, thanks for any help.

The new version:

#!/bin/sh
case $# in
0) echo "Usage: enshar filename1 filename2 [...]" >&2
;;
*) for file
do
if [ -d $file ]
then echo "enshar: $file is a directory" >&2
elif [ ! -f $file ]
then echo "enshar: $file does not exist" >&2
elif [ ! -r $file ]
then echo "enshar: $file is not readable" >&2
elif [ -h $file ]
then echo "enshar: $file cannot enshar" >&2
exit n
else
echo "\!EnShAr!\"
set '"cksum" $file'
"test $1 = nnnnnnnnnn || echo $0: bad cksum in $file << '\!EnShAr!\'"
cat $file
fi
done
;;
esac

Use code tags!!

#!/bin/sh

if [ $# -eq 0 ] ; then
  echo "Usage: enshar filename1 filename2 [...]" >&2
  exit 1
fi
for file in "$@"
do 
    if [ -d $file ]
    then 
         echo "enshar: $file is a directory" >2    
         exit 1
    elif [ ! -f $file ]
    then 
         echo "enshar: $file does not exist" >2
         exit 1
    elif [ ! -r $file ]
    then 
         echo "enshar: $file is not readable" >2
         exit 1
    elif [ -h $file ]
    then 
         echo "enshar: $file cannot enshar" >2 
         exit 1
    fi
    cksum $file| read ck summy1 dummy2   # ck is the checksum
    echo "\!EnShAr!\ " 
    # no idea what this line is doing.   $1 cannot be used here and << makes no sense to me
    # try writing this out, not as script, but as what you want done here...
    "test $file = nnnnnnnnnn || echo $0: bad cksum in $file << '\!EnShAr!\'"
    cat $file
done   
    

Hi, I am trying to check the file is good, can you show me how this section would be done properly but I want the same format output as originally stated, thanks for any help

define "good" - it looks like you are writing a scripted front-end for shar....

okay, i'm checking that the file is enshared correctly , that theres no proplems that have happened during it being not archived and then it being, not quite sure of the method you see, just been playing around with it but I have not got it to work correctly yet so wondered if there was someone who knew more about it then me, again thnks for any help offered

I can shed some light here. Look at your earlier code:

     echo "cat > $file <<\!EnShAr!"
              cat $file
              echo "!EnShAr!"
              echo "set `cksum $file`"
              echo "cksum" $file`
              check=$1
              echo "test $1 = $check || echo $0: Bad cksum in $file >&2 " >> shar
              echo "cat > $file <<\!EnShAr!" >> shar

This code is outputting another shell script. One shell script is writing a second shell script. The first 3 lines will output:
cat > /some/file/name <<!EnShAr!
contents of /some/file/name
!EnShAr!

In this context, that line: echo "!EnShAr!" makes perfect sense. You stripped out enough code that it is now useless. Put the script back the way it was. It was useful then.

Hi, thanks for your help earlier, to my understanding this code looks like it should work and I'm very excited about it but the damn thing says the end of line 27 is unexpected, please help!

#!/bin/sh
case $# in
0) echo "Usage: enshar file [ ... ]" >&2
exit 1
;;
*) for file
do
if [ -d $file ]
then echo "enshar: $file is a directory" >&2
exit 3
elif [ ! -f $file ]
then echo "enshar: $file doesn't exist" >&2
exit 4
elif [ ! -r $file ]
then echo "enshar: $file can't be read" >&2
exit 5
elif [ -h $file ]
then echo "enshar: $file can't enshar" >&2
exit 6
else
echo "cat > $file <<\!EnShAr!\"
echo "!EnShAr!"
echo "set 'cksum $file'"
echo"cksum" $file
check=$1
echo "test $1 = $check || echo $0: bad cksum in $file >&2 " >> shar
echo "cat > $file <<\!EnShAr!" >> shar
fi
done
;;
esac

p.s, where should the "fi" be, thanks for any help

ignore the line 27, its basically the end of the file (I added a little bit of code)

Use code tags or I will sic the rapid pocket weasels on you!

They are simple. They are easy. In the editor they look like
[ code ]
stuff
[ / code ]

except without the spaces in the tags. They make it possible to read your code without spontaneous eyebleeding.

#!/bin/sh
case $# in
	0)	echo "Usage: enshar file [ ... ]" >&2
		exit 1
		;;
	*)	for file
		do	
		if [ -d $file ]
		then echo "enshar: $file is a directory" >&2
		exit 3
		elif [ ! -f $file ]
		then echo "enshar: $file doesn't exist" >&2
		exit 4
		elif [ ! -r $file ]
		then echo "enshar: $file can't be read" >&2
		exit 5 
		elif [ -h $file ]
		then echo "enshar: $file can't enshar" >&2 
		exit 6
		else 	
			echo "cat > $file <<\!EnShAr!\"
			echo "!EnShAr!"
			echo "set 'cksum $file'"
			echo"cksum" $file
			check=$1
			echo "test $1 = $check || echo $0: bad cksum in $file >&2 " >> shar
			echo "cat > $file <<\!EnShAr!" >> shar
		fi
		done
		;;
esac

What's the "for file" thing meant to do? Usually you'd do something like

for Z in 'a' 'b' 'c' 'd' 'e'
do
        echo "${Z}"
done

but I see no values for your 'for'.

it's supposed to be $file, thanks for that, and yes in future I will code my posts before you attack me with something pocket sized and unpleasent

Does anyone else have any ideas why its not working, says theres a error at line 31 I think

Repost.

Note how the Z immediately after the 'for' doesn't have a $, and also notice how it's followed by the things you want Z to be. You don't appear to initialize file anywhere, either, and that one is not an automatic variable; neither do you list any values that file is supposed to loop through.

I also might suggest simplifying your code a bit... instead of making the whole thing one giant case statement, just check once at the beginning:

# Must have at least one commandline argument
if [[ $# -eq 0 ]]
then
        echo "Not enough arguments"
        exit 1
fi

# Continue on

Nested case/for/if/etc/ad/infinitums are nasty. Not nesting unless you've got a good reason to can spare you a lot of headaches.

Okay, so I should give "file" a value by doing something like file=$1 yeah? where should I do that. Am I close to my objective here or way off? Sorry about this it's just taking me a min to get my head round it.

Are you saying I should put the file checking part in a if statement and then the shar part in another?

Is the case section of the script needed? can you see any reason why my script is not expecting a end of line section at the end? please help

instead of escaping ! , u have escaped " at line
echo "\!EnShAr!\"

change it to
echo "\!EnShAr\!"

Regards,
------------
Suman

okay my script for the shar section looks like this at the mo:

if
echo "cat > $file <<\!EnShAr!\"
echo "\!EnShAr!"
echo "set `cksum $file`"
echo"cksum" $file
check=$1
echo "test $1 = $check || echo $0: bad cksum in $file >&2 " >> sharS
echo "cat > $file <<\!EnShAr!" >> sharS
fi

-----------------------------------------------------------------------------------
If I do as told to do buy the last post:
"echo "cat > $file <<\!EnShAr\!"
Then it crashes at the fi not being expected,

if I don't it still crashes saying that the end of line is not expected at the end,

Any advice somebody?

Now you've done it...

if
then
  echo "cat > $file <<\!EnShAr\!"
  echo "\!EnShAr\!"
  echo "set `cksum $file`"
# Needed a space between echo and "cksum"
  echo "cksum" $file
# Is this supposed to be echoed?
  check=$1
  echo "test $1 = $check || echo $0: bad cksum in $file >&2 " >> sharS
  echo "cat > $file <<\!EnShAr\!" >> sharS
fi

Got to watch those exclamation marks. They mean something special in sh, even inside double quotes. Maybye make that !EnShAr! thing a variable so you don't have to type the various crazy escapings every time?

There is nothing wrong with this:

#! /usr/bin/ksh

for par ; do
        echo par = $par
done
exit 0

And that will work fine with the Bourne shell too. When a for statement does not have an explicit list, it will step through the positional parameters. Call that little script "fortest" and and it with:
./fortest one two three
It will work fine.

Next, shells have special characters like " and $ that mean special things. Generally a backshash will remove the special meaning from a special character. Now look at this line:
echo "cat > $file <<\!EnShAr!\"
You have removed the special meaning from the last quote so it does not match up with the first quote. So the shell reads the next line and find a normal quote there that ends the quoted string. Your quoted string contains a quote and it goes into a second line which is legal. Now everything is mexed up and the shell will finally barf on the last quote.

Have you tried running it on unix? it does not seem to work on mine, if you can show me a version you know works and I'll see if I'm the one with unix being difficult, thanks

i don't mean the whole thing by the way, just this shar section, i don't wanna have to rewrite the script to use differnt functions. Thanks for any help given