views:

100

answers:

6

Hi, I have a simple shell script as follows:

myfunc()
{
        #print hello world
        echo "Hello World";
}

myfunc

The script works fine when I execute in linux pc but when I run the same in uclinux, I get the error as "syntax error". What could be reason for the problem?

A: 

Maybe your installation of uclinux uses a different shell?

Saying "shell script doesn't work" is like saying "my source code doesn't work". Of course the phrase only makes sense if you say what language your source code is in. Similarly for shell script: is it bash? is it ksh? is it tcsh? For uclinux I highly suspect it's busybox.

slebetman
A: 

Your shell script should have a shebang line that will cause the script to be executed by the shell you designate. This can reduce or eliminate many unexpected errors due to differences in syntax between shells that are caused when the script is executed by the current (or default) shell which may be different for a number of reasons.

The first line of the script file should be similar to:

#!/bin/sh

with the path and name of the shell appropriate to your needs.

Dennis Williamson
A: 

OK, may be I wasn't elaborate. The following code works in uclinux:

#!/bin/sh

echo "Hello World"

But, the following code is not working: !/bin/sh

myfunc() { #print hello world echo "Hello World"; }

myfunc

This is really surprising for me but I am not able to catch the root cause for this. Please help.

A: 

Is your actual myfunc defined in one line as you show it? That's a syntax error since you're commenting out lots of stuff including the }.

Hyman Rosen
@Hyman - you can see the OP 'unformatted' here http://stackoverflow.com/revisions/87da1674-35fe-43cb-bdd0-ac88f72c82e1/view-source - wasn't on one line, just SO formatting it that way.
martin clayton
A: 

If you put myfunc() { #print hello world echo "Hello World"; } on one line, then

#print hello world echo "Hello World"; } gets interpreted as comment. Remove the part #print hello world and try again.

A: 

The result depends what shell you run. Most uclinux's shells are actually symbolic links to Busybox. Busybox implements various tiny shells for different memory foot print requirements. As I remember, only ash supports function syntax. Check your busybox version and its build config.

Tim Wu