LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2006, 07:53 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
What Is Wrong With This Bash Script File?


Hello everyone,


When I executing the following bash script file,

--------------------
Foo=gcc\ -DDEBUG
Goo=-I.
Zoo=$(Foo) $(Goo)
echo $(Zoo)
--------------------


--------------------
./test.sh: line 1: Goo: command not found
./test.sh: line 1: Foo: command not found
./test.sh: line 1: Zoo: command not found
--------------------

I simply want to set three variables and test their output. Are there anything wrong with this script file?


thanks in advance,
George
 
Old 05-03-2006, 08:02 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Don't put parentheses around the variable substitutions; just do $Foo. But this line should be different:
Code:
Zoo="$Foo $Goo"
You have to quote this line if you want it to work, I think. In fact, you should quote any assignment that isn't a number, even if you don't have to.
 
Old 05-03-2006, 10:49 AM   #3
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
Or use ${Foo}
Read more over here: http://www.linuxcommand.org/

And don't forget the include the bash-header:
Code:
#!/bin/bash

--- rest of script ---

exit

Last edited by muha; 05-03-2006 at 10:53 AM.
 
Old 05-03-2006, 09:06 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
I'm curious why you say to use exit at the end, instead of just naturally allowing EOF to terminate the script. Can Bash be configured to ignore EOF as a session terminator?

Also, I just want to point out, with respect, that I believe it's more flexible to use "#!/usr/bin/env bash" than "#!/bin/bash", since bash may not be installed into /bin (as it is not on BSD systems, for example). I think env is either stored in or symlinked in /usr/bin on every Unix-like installation.
 
Old 05-04-2006, 01:23 AM   #5
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Hi taylor_venable,


Quote:
Originally Posted by taylor_venable
Don't put parentheses around the variable substitutions; just do $Foo. But this line should be different:
Code:
Zoo="$Foo $Goo"
You have to quote this line if you want it to work, I think. In fact, you should quote any assignment that isn't a number, even if you don't have to.
Your method works! I used Makefile style to write bash files, so I add () around variables. I have also found another approach to work out this solution.

Zoo=$Foo\ $Goo


regards,
George
 
Old 05-04-2006, 01:26 AM   #6
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Cool muha! Youe method works!


Quote:
Originally Posted by muha
Or use ${Foo}
Read more over here: http://www.linuxcommand.org/

And don't forget the include the bash-header:
Code:
#!/bin/bash

--- rest of script ---

exit
Why should we add exit?


regards,
George
 
Old 05-04-2006, 01:28 AM   #7
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Hi taylor_venable,


Quote:
Originally Posted by taylor_venable
I'm curious why you say to use exit at the end, instead of just naturally allowing EOF to terminate the script. Can Bash be configured to ignore EOF as a session terminator?

Also, I just want to point out, with respect, that I believe it's more flexible to use "#!/usr/bin/env bash" than "#!/bin/bash", since bash may not be installed into /bin (as it is not on BSD systems, for example). I think env is either stored in or symlinked in /usr/bin on every Unix-like installation.
What is the function of env? I have never used it before. Is it available on both Linux and UNIX (like Solaris)?


regards,
George
 
Old 05-04-2006, 02:43 AM   #8
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
@exit: it's something i picked up from over here: http://www.linuxcommand.org/wss0090.php#exit
I thought including exit on the last line of code caused a script to terminate succesfully (so no errors were made).
 
Old 05-04-2006, 03:02 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The script exits with the result value of the last cmd run. If you want to set it explictly, use
exit <some_positive_integer_or_zero>
conventionally on Unix style systems, zero is success, (positive) int is an error. iirc, negative value is not allowed here.
 
Old 05-04-2006, 11:06 AM   #10
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
From the manual page: "The env utility executes another utility after modifying the environment as specified on the command line. ... Probably the most common use of env is to find the correct interpreter for a script, when the interpreter may be in different directories on different systems. The following example will find the `perl' interpreter by searching through the directories specified by PATH."
Code:
#!/usr/bin/env perl
In other words, env applies an environment (the current one, modified by options passed to env) to an executable. So actually, you can use env to specify all kinds of environment variables, including PATH and LD_LIBRARY_PATH, so that things are just right when the script gets executed. This feature would be increasingly useful in varied environments. The env binary is stored in /usr/bin on all three of the major BSDs, and also on Slackware Linux, in my experience. I think it's somewhat of a standard, so it should be there on other systems as well, but you'll have to check to be sure.
 
Old 05-07-2006, 03:52 AM   #11
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Cool muha! I have never used it in this way before. I think there must be some default return values if I do not call exit explicitly at the last line of a script. Right?


Quote:
Originally Posted by muha
@exit: it's something i picked up from over here: http://www.linuxcommand.org/wss0090.php#exit
I thought including exit on the last line of code caused a script to terminate succesfully (so no errors were made).

regards,
George
 
Old 05-07-2006, 03:55 AM   #12
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thank you chrism01!


Quote:
Originally Posted by chrism01
The script exits with the result value of the last cmd run. If you want to set it explictly, use
exit <some_positive_integer_or_zero>
conventionally on Unix style systems, zero is success, (positive) int is an error. iirc, negative value is not allowed here.
Do you know whether there is some default value to return if we do not write exit on the last line of a script file?


regards,
George
 
Old 05-07-2006, 03:58 AM   #13
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thank you taylor_venable!


Quote:
Originally Posted by taylor_venable
From the manual page: "The env utility executes another utility after modifying the environment as specified on the command line. ... Probably the most common use of env is to find the correct interpreter for a script, when the interpreter may be in different directories on different systems. The following example will find the `perl' interpreter by searching through the directories specified by PATH."
Code:
#!/usr/bin/env perl
In other words, env applies an environment (the current one, modified by options passed to env) to an executable. So actually, you can use env to specify all kinds of environment variables, including PATH and LD_LIBRARY_PATH, so that things are just right when the script gets executed. This feature would be increasingly useful in varied environments. The env binary is stored in /usr/bin on all three of the major BSDs, and also on Slackware Linux, in my experience. I think it's somewhat of a standard, so it should be there on other systems as well, but you'll have to check to be sure.
What do you mean "the current one, modified by options passed to env"? I think you mean passing parameter to env -- but I am not sure about it. Could you show me an example to show your idea please?


regards,
George
 
Old 05-07-2006, 01:10 PM   #14
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
I mean, the current environment is applied to the program that env is going to execute. And by passing VARIABLE=value options to env, you can modify that environment. For example:
Code:
#!/usr/bin/env -S MY_ENV_VARIABLE=foo bash

echo -n "MY_ENV_VARIABLE = "
echo $MY_ENV_VARIABLE
echo -n "PWD = "
echo $PWD
MY_ENV_VARIABLE probably won't exist before the script starts executing. But when you call env, it creates the variable MY_ENV_VARIABLE and assigns "foo" to it. Then the env utility runs bash, which takes over interpretation on the rest of the script. And now a historical note on the significance of the -S option, courtesy of the FreeBSD env manual page:
Quote:
Note that the way the kernel parses the `#!' (first line) of an interpreted script has changed as of FreeBSD 6.0. Prior to that, the FreeBSD kernel would split that first line into separate arguments based on any whitespace (space or <tab> characters) found in the line. So, if a script named /usr/local/bin/someport had a first line of:
Code:
#!/usr/local/bin/php -n -q -dsafe_mode=0
then the /usr/local/bin/php program would have been started with the arguments of:
Code:
arg[0] = '/usr/local/bin/php'
arg[1] = '-n'
arg[2] = '-q'
arg[3] = '-dsafe_mode=0'
arg[4] = '/usr/local/bin/someport'
plus any arguments the user specified when executing someport. However, this processing of multiple options on the `#!' line is not the way any other operating system parses the first line of an interpreted script. So after a change which was made for FreeBSD 6.0 release, that script will result in /usr/local/bin/php being started with the arguments of:
Code:
arg[0] = '/usr/local/bin/php'
arg[1] = '-n -q -dsafe_mode=0'
arg[2] = '/usr/local/bin/someport'
plus any arguments the user specified. This caused a significant change in the behavior of a few scripts. In the case of above script, to have it behave the same way under FreeBSD 6.0 as it did under earlier releases, the first line should be changed to:
Code:
#!/usr/bin/env -S /usr/local/bin/php -n -q -dsafe_mode=0
The env utility will be started with the entire line as a single argument:
Code:
arg[1] = '-S /usr/local/bin/php -n -q -dsafe_mode=0'
and then -S processing will split that line into separate arguments before executing /usr/local/bin/php.
In other words, the "-S" is pretty important if you do anything more complicated than
Code:
/usr/bin/env bash
by passing options to env and/or bash. If the manual is correct, this should be available on many systems, but I only have BSD systems so I can't check it out for you.
 
Old 05-08-2006, 02:22 AM   #15
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks taylor_venable!


Quote:
Originally Posted by taylor_venable
I mean, the current environment is applied to the program that env is going to execute. And by passing VARIABLE=value options to env, you can modify that environment. For example:
Code:
#!/usr/bin/env -S MY_ENV_VARIABLE=foo bash

echo -n "MY_ENV_VARIABLE = "
echo $MY_ENV_VARIABLE
echo -n "PWD = "
echo $PWD
MY_ENV_VARIABLE probably won't exist before the script starts executing. But when you call env, it creates the variable MY_ENV_VARIABLE and assigns "foo" to it. Then the env utility runs bash, which takes over interpretation on the rest of the script. And now a historical note on the significance of the -S option, courtesy of the FreeBSD env manual page:In other words, the "-S" is pretty important if you do anything more complicated than
Code:
/usr/bin/env bash
by passing options to env and/or bash. If the manual is correct, this should be available on many systems, but I only have BSD systems so I can't check it out for you.
I have tried your method on my Red Hat Linux system, but the behavior is quite different.

In the below scripts,

Code:
#!/usr/bin/env -S FOO=foo GOO=goo bash
echo $FOO
echo $GOO
From the output, it seems that the -S option is not supported.

Code:
[root@localhost root]# ./test3.sh 
/usr/bin/env: invalid option -- S
Try `/usr/bin/env --help' for more information.
When I remove the -S option, the script will be pending and it seems that it will not end it execution, like this,

Code:
#!/usr/bin/env FOO=foo GOO=goo bash
echo $FOO
echo $GOO
Output,

Code:
[root@localhost root]# ./test3.sh

regards,
George

Last edited by George2; 05-08-2006 at 02:23 AM.
 
  


Reply



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
Adding script file on Bash greatmenon Linux - Software 1 01-21-2006 07:14 AM
Bash File not found, What am I doing wrong? Magus_Rune Programming 2 11-28-2004 03:39 PM
how to creata file through bash script? varala_kanth Linux - Software 2 04-29-2004 02:52 PM
My first BASH script, what's wrong with it? szf2 Linux - Newbie 2 11-12-2003 01:43 PM
Basic BASH script, what's wrong??? Satriani Linux - General 2 06-02-2003 05:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:53 PM.

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
Open Source Consulting | Domain Registration