LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   'command not found' but works fine anyway (http://www.linuxquestions.org/questions/linux-general-1/command-not-found-but-works-fine-anyway-26606/)

HickorySlim 07-26-2002 03:01 PM

'command not found' but works fine anyway
 
I'm getting several odd shell script behaviors in BASH:

1) when I run a shell script, I get "command not found" errors for every line of the script -- but the script runs fine anyway. This is a simple script that sets some environment variables and then runs javac.

2) the first line #! /bin/sh causes the script to fail so I've taken it out. Same for #! /bin/bash. Both are there

3) The shell script can't read any environment variables. I have a one-line script that simply echos $MYVAR and here's what happens when I set MYVAR and then run the script:

> MYVAR="hello"
> echo $MYVAR
hello
> ./myscript
the value of MYVAR is
>


Any advice here would be appreciated.

Thx.

A-dummy 07-26-2002 03:43 PM

atleast for the third problem ,you should export <variable> ....when you execute a shell script it's
executed in a sub-shell with different environment from the one in which you are......what exactly do
you mean that #!/bin/bash causes script to fail.....can u post ur script here......

bbeers 07-26-2002 04:10 PM

Maybe you are not running bash? What distro? What is in your /etc/shells file? What is at the end of each line in your /etc/passwd?

HickorySlim 07-27-2002 11:23 PM

Well, the environment variable problem and the #!/bin/bash problem have been solved by the age-old cure: a reboot. But the "command not found" behavior remains.

I'm on RedHat 7.2.
/etc/shells file has /bin/sh at the top, then /bin/bash, then some others.
/etc/passwd has /bin/bash at the end of each line.
The ps command shows I'm in bash.

bbeers 07-29-2002 09:14 AM

I see two likely possibilites for your "command not found" problem.
One, your path doesn't include the directory where these comands live,
or two, there is some typographical error at the top of your script which then prevents the rest of the script from being interpreted properly by bash. If the script is not too long, post it and someone may figure out the problem.

-Bob

HickorySlim 08-01-2002 09:32 PM

I don't think it's that simple, because if the commands aren't being found, why do they still execute properly?

Also, looks like the export command stopped working again. Here's the complete script:

#!/bin/sh
PROJECT_HOME=/usr/kastware/maybeso;
JAVA_HOME=/usr/java/j2sdk1.4.0;
CATALINA_HOME=/usr/local/jakarta-tomcat-4.0.4;
PATH=$JAVA_HOME/bin;

export PROJECT_HOME;
export JAVA_HOME;
export CATALINA_HOME;
export PATH;


After running this script, none of the variables are set in my environment. However, if I run this script inside another script using ". ./scriptname" then the variables are picked up OK.

Any advice is much appreciated.

neo77777 08-01-2002 09:49 PM

Get rid of ;

turnip 08-01-2002 10:14 PM

and it looks like because you defigned them as variables you need

export $PROJECT_HOME
export $

and so on..

and there is no space in between #!/bin/sh its not #! /bin/sh

HickorySlim 08-01-2002 10:54 PM

Thanks, but still broken:

1) I don't have any space between #! and /bin/sh
2) semi-colons were I thought optional, but I removed them anyway to no avail
3) export $VARNAME is definitely not the way to go, as that only produces errors like "/usr/java/j2sdk1.4.0': not a valid identifier". Every time I've seen the export command used, it has been without the $.

One thing that's strange is I noticed when I do "which export" it shows no export in any of my bin directories! But last time I booted up it worked fine; previous to that it was broken again. Anyone ever heard of some "every other boot things are OK" problem??

A-dummy 08-01-2002 11:45 PM

when you are executing a script using ./<script-name> it is run inside a child shell with it's own
memory & environment.....so you cant export variables from that script to it's parent shell.....try something
as simple as
$ vi my_script

#!/bin/sh
P=hi
export P

$ ./my_script
$echo $P
nothing will appear.....but . ./<script-name> will execute it in the current shell itself & hence no child
shell is created.....you can however put this line in ur .bashrc (or .profile whatever is executed in your
shell)
. <script-path>
(Note the "dot" before scipt-path & this was meant on bash....I have never used ksh or csh)
This will set environmental variables at the same time when shell is created.....

& make a small correction in your script....
use
PATH=$PATH:<your-java-path-here>
otherwise you are changing the path itself & that's why you got error on "which export" after executing
your script.....

HickorySlim 08-02-2002 10:24 AM

Hey, bingo! Executing in current shell works! So I guess `export` only exports to subshells, not parent shells?

Also, thanks for spotting the PATH problem. Strangely, though, even after a reboot and before running any scripts, I don't seem to have "export" on my system. I even did a full search:

find . -name 'export*'

from the root directory, and it doesn't seem to be on my system...

And finally, thanks for the no-semi-colon suggestion -- that solves the "command not found" mystery: it was apparently looking for a second command to execute on each line after the semi-colon, and of course it found none, but still nonetheless executed the command it did find before the semi-colon. It all makes sense now...

Thanks to everyone for your help! It's a good feeling having such a responsive community around you...

ADDENDUM: I started getting the old problems whenever I included that top #!/bin/sh line again... but this time one file worked while the other choked, in the same directory, same exact line on top. In addition, javac was behaving rather weirdly. I removed all but the top two lines, which were the #! line followed by an echo, and still one worked while other choked. I traced over each character, letter for letter with vi, and the two files were identical, except then I noticed in the corner that one file said "dos", and the character count was 2 chars longer. I ran dos2unix on all files and now they both work!

linuxcool 08-03-2002 07:47 AM

You won't find export because it's a bash builtin command. Go here and enter in the search box builtins and you'll get a list of the bash builtin commands. To find out more about the builtin commands, read bash's man page.

In place of the the dot " . " in front of the script name to get the script to execute in the current shell, you can use source. Ex.:

. ./script_name

or

source ./script_name


All times are GMT -5. The time now is 02:52 AM.