Click to See Complete Forum and Search --> : something wrong with my shell code


HelpPlease
12-10-2003, 10:21 AM
Ok...........i created a shell called last.sh (suppose to copy 3 txt files into 1 txt file but i get.............

$ vi last.sh

"last.sh" [Incomplete last line] 8 lines, 136 characters
# last.sh^M
#!/bin/sh^M
if [-f $4] ; then ^M
rm -f $4 ^M
touch $4^M
cat $1 $2 $3 >> $4^M
then do sh <file> file1 file2 file3 <target file>^M
fi
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"last.sh" [Incomplete last line] 8 lines, 136 character
There is something wrong in the coding because when i do...........
$ sh last.sh
last.sh: syntax error at line 7: `then' unexpected
[~]
$ ./last.sh
bash: ./last.sh: Permission denied
[~]
I don't know whats wrong

ph34r
12-10-2003, 10:38 AM
Those ^M characters - did you edit the file in Windows?

Hayl
12-10-2003, 10:40 AM
you have a comment line as the first line, that negates the #!/bin/sh

#!/bin/sh has to be the first line.

# last.sh
#!/bin/sh

should be

#!/bin/sh
# last.sh

if you still get the error after changing that, then chmod +x last.sh

voidinit
12-10-2003, 11:50 AM
The general format of an if statement is:

if [ test] ; then

do something

fi

You have two then statements, that is not acceptable. If you want to do multiple somethings if an expression is true, just list them.

if [ test ]; then

do something
do somethingelse
do athirdthing

fi