LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-17-2009, 08:13 AM   #1
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Rep: Reputation: 35
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!
 
Old 10-17-2009, 08:28 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
?
 
Old 10-17-2009, 08:30 AM   #3
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
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.
 
Old 10-17-2009, 08:39 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Aha!!

OK--what is "sh" on your system? Is it aliased to BASH?
 
Old 10-17-2009, 08:44 AM   #5
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
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?
 
Old 10-17-2009, 08:47 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
 
Old 10-17-2009, 08:49 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by fusion1275 View Post
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!!!...
 
Old 10-17-2009, 08:52 AM   #8
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
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.
 
Old 10-17-2009, 09:37 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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......
 
Old 10-17-2009, 09:39 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@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.
 
Old 10-17-2009, 09:45 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531
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

Last edited by unSpawn; 10-17-2009 at 09:47 AM. Reason: //More *is* more...
 
Old 10-17-2009, 10:18 AM   #12
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
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???
 
Old 10-17-2009, 10:30 AM   #13
slide77
LQ Newbie
 
Registered: Jun 2007
Posts: 22

Rep: Reputation: 16
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.
 
Old 10-17-2009, 10:46 AM   #14
wmakowski
Member
 
Registered: Oct 2003
Location: Ohio
Distribution: Fedora 23, 24, RHL 5.2
Posts: 560

Rep: Reputation: 56
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.

Last edited by wmakowski; 10-17-2009 at 11:28 AM.
 
Old 10-17-2009, 10:47 AM   #15
slide77
LQ Newbie
 
Registered: Jun 2007
Posts: 22

Rep: Reputation: 16
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats a good geotagging software/script? enine Linux - Software 3 08-24-2009 08:12 PM
Whats wrong with this script?? Reegz Linux - General 2 09-11-2006 03:24 AM
Whats wrong with my dialing script? pen^2 Linux - General 13 09-16-2004 03:34 AM
Whats wrong with this script??? Sammy2ooo Linux - Networking 2 08-24-2004 07:20 AM
Whats wrong with this PERL script? VisionZ Linux - Newbie 25 03-25-2004 09:58 AM


All times are GMT -5. The time now is 07:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration