LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Whats up with this basic script?? (https://www.linuxquestions.org/questions/linux-newbie-8/whats-up-with-this-basic-script-762562/)

fusion1275 10-17-2009 07:13 AM

Whats up with this basic script??
 
Quote:

#!/bin/bash

function Output
{
echo "hello World"
}

Output

I run it using ./tester.sh and I get the output:
Quote:

hello World
I run it with debug sh -x ./tester.sh and I get a totally different output:
Quote:

+ function Output
tester.sh: 1: function: not found
+ echo hello World
hello World
+ Output
tester.sh: 1: Output: not found
Why is it complaining about the function command??
Am I seriously doing something fundamentally wrong here? Shall I consult a doctor because my IQ has now dropped to equal a squashed watermelon?

Someone help!

pixellany 10-17-2009 07:28 AM

For starters, where did you get "debug"? It is not on my system or in my distro repositories (Arch).

Is it intended to be used with BASH scripts?

What happens with:
debug -x ./tester.sh

OR:
debug ./tester.sh
?

fusion1275 10-17-2009 07:30 AM

Sorry its the way I typed it.

I meant I ran the script in debug mode using:

sh -x ./tester.sh

I know there is nothing called debug.

pixellany 10-17-2009 07:39 AM

Aha!!

OK--what is "sh" on your system? Is it aliased to BASH?

fusion1275 10-17-2009 07:44 AM

yeah I am using /bin/bash

The script works perfectly. But when I check "under the hood" in the debug mode it is complaining. I have never come across that before so am wondering whats going on.

I have tried it on 2 other machines and still the same error. So cant be me... can it?

pixellany 10-17-2009 07:47 AM

On my system, "sh" is a softlink to bash

Code:

[mherring@Ath ~]$ more debug
#!/bin/bash
function Output
{
echo "hello World"
}
Output

[mherring@Ath ~]$ sh -x ./debug
+ Output
+ echo 'hello World'
hello World


pixellany 10-17-2009 07:49 AM

Quote:

Originally Posted by fusion1275 (Post 3722678)
yeah I am using /bin/bash

Great, but what is "sh"?
Quote:

I have tried it on 2 other machines and still the same error. So cant be me... can it?
Of course it can!!!...;)

fusion1275 10-17-2009 07:52 AM

I type
Quote:

echo $SHELL
I get:

Quote:

/bin/bash
I type
Quote:

sh
I type
Quote:

echo $SHELL
I get:

Quote:

/bin/bash
So its the same.

pixellany 10-17-2009 08:37 AM

Code:

[root@Ath mherring]# sh -x debug
+ Output
+ echo 'hello World'
hello World

[root@Ath mherring]# zsh -x debug
+ Output
+ echo 'hello World'
hello World

[root@Ath mherring]# tcsh -x debug
function: Command not found.
{: Command not found.
hello World
}: Command not found.
Output: Command not found.

[root@Ath mherring]# tcsh
# echo $SHELL
/bin/bash
#

So I duplicated your result by running it with tcsh. Note that when I switch to tcsh shell, the SHELL variable still thinks I'm running bash.

SO....humor me and go into /bin and see what sh is......

druuna 10-17-2009 08:39 AM

@fusion1275:

This: echo $SHELL is not reliable to show which shell is used:
Quote:

$ echo $SHELL
/bin/bash
$ csh
% echo $SHELL
/bin/bash
SHELL is only set/used in bash and ksh.

What does sh --version tell you.

BTW: I, like pixellany, don't have any problems with the "problem" you posted.

unSpawn 10-17-2009 08:45 AM

Shells that don't like this kind of function declaration are Ash, Bsh, Csh and Jsh (as in checking for odd shell linkage which clearly didn't happen anyway). However the exact "tester.sh: 1: function: not found" error I can only reproduce using /bin/bash when 0) invoked as 'sh' AND 1) removing the "function" from the "function Output" line. (The other shells will spit out other types of errors.) Which is odd as with current Bash-2 you wouldn't even need to explicitly use the "function" tag to declare a function as "name() { doSomething; }" will do easily...

//BTW with my systems I didn't see no errors either. I wonder though what doctor could help the OP ;-p

fusion1275 10-17-2009 09:18 AM

Ooooh weird...

On all my Ubuntu systems I get the same results:

Quote:

ll /bin/sh

lrwxrwxrwx 1 root root 4 2008-05-08 21:39 /bin/sh -> dash

What the hell is dash???

slide77 10-17-2009 09:30 AM

I can't test it right now to duplicate the error I think it's complaining because the function doesn't have anything to do. It looks at the Output line at the bottom for something to plug into the function and doesn't find anything. Try:


function Output
{
echo $1 $2
}

Output Hello World
Output Good Bye
Output One Two

A function performs the action between the brackets on every line you have listed below it with the variables ($1 $2 here) being substituted in the function. So this runs echo $1 $2 on the line "Hello World" then runs echo again on "Good Bye" then runs it again on "One Two" etc.

wmakowski 10-17-2009 09:46 AM

Interesting, about using dash for /bin/sh. At least the results can be explained now. To tell you the truth it is the first time I've heard of it. There is a wikipedia article on it. Looks like Debian and Ubuntu have this as their default for /bin/sh.

slide77 10-17-2009 09:47 AM

Well nevermind, I should have read the thread more carefully and tested the original function first. I ran it and it doesn't give any error here either.

fusion1275 10-17-2009 09:50 AM

Can you guys post where your "sh" is pointing to please. Seems mine isn't correct if going by the posts on here.

r3sistance 10-17-2009 10:57 AM

It depends on what Distribution you are using, most /bin/sh should refer to bash, however for debian based distributions it should be /bin/dash (A debian specific shell, I believe based on ash)

fbsduser 10-17-2009 12:35 PM

Quote:

Originally Posted by fusion1275 (Post 3722758)
Can you guys post where your "sh" is pointing to please. Seems mine isn't correct if going by the posts on here.

Your "sh" symlink points to a shell called dash. You might want to remove that symlink and create a new one pointing to bash.

fusion1275 10-19-2009 07:11 AM

So does that mean that Ubuntu is set up incorrectly?

Shouldn't it default to the bash shell?

pwc101 10-19-2009 07:17 AM

Quote:

Originally Posted by fbsduser (Post 3722886)
Your "sh" symlink points to a shell called dash. You might want to remove that symlink and create a new one pointing to bash.

This is a bad idea. All sorts of things might break. Please don't do this.

@OP: The situation you have is "correct" for Debian based systems.

fusion1275 10-19-2009 07:22 AM

Yeah thought changing it would be a bad idea!!

So any ideas why I get errors and some dont? Seems to depend on what distro they are using now doesnt it??

Lordandmaker 10-19-2009 07:31 AM

Quote:

Originally Posted by fusion1275 (Post 3724795)
So any ideas why I get errors and some dont? Seems to depend on what distro they are using now doesnt it??

For some people, sh is a link to /bin/bash and for the debianites its /bin/dash.

Dash is a debian-developed shell which is being used where the features of bash aren't needed, and speed and simplicity are. There's been warnings for a while that scripts with bashims need a /bin/bash shebang, not a /bin/sh.

So the difference is down to the fact that, even though they're invoking them the same way, some people are running
Code:

/bin/bash -x
, and others are running
Code:

/bin/dash -x
, and these are different shells.

rn_ 10-19-2009 07:43 AM

hmm... OP used #!/bin/bash. I wonder why that didn't take effect.

GazL 10-19-2009 09:27 AM

Quote:

Originally Posted by rn_ (Post 3724809)
hmm... OP used #!/bin/bash. I wonder why that didn't take effect.

because he was explicitly telling it to use 'sh' on the command line when he invoked it.

rn_ 10-19-2009 10:22 AM

Quote:

Originally Posted by GazL (Post 3724902)
because he was explicitly telling it to use 'sh' on the command line when he invoked it.

hm... i guess my understanding of the 'shebang' was incorrect. I always thought it was used to override whatever the invoking shell may be.

hey, this is the second new thing i have learned today.

Thanks.

fusion1275 10-19-2009 10:26 AM

Well looks like my little issue has become into a lesson. I shall make a note of this.


All times are GMT -5. The time now is 06:02 PM.