LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-26-2002, 02:01 PM   #1
HickorySlim
LQ Newbie
 
Registered: Mar 2002
Location: New York City
Posts: 7

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

Last edited by HickorySlim; 07-26-2002 at 02:02 PM.
 
Old 07-26-2002, 02:43 PM   #2
A-dummy
Member
 
Registered: Jun 2002
Location: Kanpur,India
Distribution: RH-7.0 , 7.3
Posts: 130

Rep: Reputation: 15
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......
 
Old 07-26-2002, 03:10 PM   #3
bbeers
Member
 
Registered: Jul 2002
Location: Florida
Distribution: Centos, Slackware
Posts: 260

Rep: Reputation: 30
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?
 
Old 07-27-2002, 10:23 PM   #4
HickorySlim
LQ Newbie
 
Registered: Mar 2002
Location: New York City
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 07-29-2002, 08:14 AM   #5
bbeers
Member
 
Registered: Jul 2002
Location: Florida
Distribution: Centos, Slackware
Posts: 260

Rep: Reputation: 30
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
 
Old 08-01-2002, 08:32 PM   #6
HickorySlim
LQ Newbie
 
Registered: Mar 2002
Location: New York City
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 08-01-2002, 08:49 PM   #7
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 55
Get rid of ;
 
Old 08-01-2002, 09:14 PM   #8
turnip
Member
 
Registered: Jul 2002
Posts: 143

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

Last edited by turnip; 08-01-2002 at 09:16 PM.
 
Old 08-01-2002, 09:54 PM   #9
HickorySlim
LQ Newbie
 
Registered: Mar 2002
Location: New York City
Posts: 7

Original Poster
Rep: Reputation: 0
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??
 
Old 08-01-2002, 10:45 PM   #10
A-dummy
Member
 
Registered: Jun 2002
Location: Kanpur,India
Distribution: RH-7.0 , 7.3
Posts: 130

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

Last edited by A-dummy; 08-01-2002 at 10:50 PM.
 
Old 08-02-2002, 09:24 AM   #11
HickorySlim
LQ Newbie
 
Registered: Mar 2002
Location: New York City
Posts: 7

Original Poster
Rep: Reputation: 0
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!

Last edited by HickorySlim; 08-02-2002 at 10:26 AM.
 
Old 08-03-2002, 06:47 AM   #12
linuxcool
LQ Addict
 
Registered: Jun 2001
Posts: 1,183

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


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
acpi works fine with 2.4 but not with 2.6 sneutrinono Linux - Laptop and Netbook 6 11-19-2004 05:04 AM
e0 will Rx, but not Tx...e1 works fine giant Linux - Networking 2 09-15-2004 01:13 PM
SED command doesn't works fine on AIX JSnake AIX 4 07-01-2004 12:32 PM
Everything works fine... but no ADSL!! Conexus Slackware 10 04-11-2004 06:29 AM
Works just fine...I think /home/kyle Linux - Software 0 03-10-2004 05:09 AM


All times are GMT -5. The time now is 04:32 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration