sh file can't work well but commands in sh can - linux

I'm using a bash shell to export some path,like this:
env.sh:
export GOPATH=$GOPATH:$QBOXROOT/pili-zeus
export PATH=$PATH:$QBOXROOT/pili-zeus/bin
after call sh env.sh,I call echo $GOPATH ,but nothing output,and I call export again to check the variables exported,but GOPATH isn't included.But when I copy these commands in the shell file,and execute them directly in command line,it can work!why this happens?I'm using mac.

You need to use source env.sh to get these variables stick to your context (i.e. terminal).
"source is a Unix command that evaluates the file following the command, as a list of commands, executed in the current context."
https://en.wikipedia.org/wiki/Source_(command)

Related

how can we change the directory in linux using perl script after taking input from user [duplicate]

How to set a global environment variable in a bash script?
If I do stuff like
#!/bin/bash
FOO=bar
...or
#!/bin/bash
export FOO=bar
...the vars seem to stay in the local context, whereas I'd like to keep using them after the script has finished executing.
Run your script with .
. myscript.sh
This will run the script in the current shell environment.
export governs which variables will be available to new processes, so if you say
FOO=1
export BAR=2
./runScript.sh
then $BAR will be available in the environment of runScript.sh, but $FOO will not.
When you run a shell script, it's done in a sub-shell so it cannot affect the parent shell's environment. You want to source the script by doing:
. ./setfoo.sh
This executes it in the context of the current shell, not as a sub shell.
From the bash man page:
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command executed
from filename.
If filename does not contain a slash, file names in PATH are used to
find the directory containing filename.
The file searched for in PATH need not be executable. When bash is not
in POSIX mode, the current directory is searched if no file is found
in PATH.
If the sourcepath option to the shopt builtin command is turned off,
the PATH is not searched.
If any arguments are supplied, they become the positional parameters
when filename is executed.
Otherwise the positional parameters are unchanged. The return status
is the status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or cannot
be read.
source myscript.sh is also feasible.
Description for linux command source:
source is a Unix command that evaluates the file following the command,
as a list of commands, executed in the current context
#!/bin/bash
export FOO=bar
or
#!/bin/bash
FOO=bar
export FOO
man export:
The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to word.
A common design is to have your script output a result, and require the cooperation of the caller. Then you can say, for example,
eval "$(yourscript)"
or perhaps less dangerously
cd "$(yourscript)"
This extends to tools in other languages besides shell script.
In your shell script, write the variables to another file like below and source these files in your ~/.bashrc or ~/.zshrc
echo "export FOO=bar" >> environment.sh
In your ~/.bashrc or ~/.zshrc, source it like below:
source Path-to-file/environment.sh
You can then access it globally.
FOO=bar
export FOO

Substitute variables value in myscript.sh file when running with sh command

I have a property file say mypropety.properties where i have set multiple key value pairs.
I load properties to the current session running $. myproperty.properties command.
Now when I run script using $. myscript.sh it loads all variable values from session and works fine. However in my use case I need to run the command using sh myscript.sh command and when I run it this way it doesn't replace variable values in the script and fails.
Any idea how to do variable substitution when running a script with sh command?
Bash looks for the variable BASH_ENV in the environment and if present expands and reads the file.
sh -c "BASH_ENV=myproperty.properties myscript.sh"

export doesn't work in shell script but does via CLI

I have the following file - test.sh - in .:
#!/bin/sh
export ASDF=test
I do chmod +x test.sh,then ./test.sh and finally echo $ASDF and... nothing. It's as though $ASDF hasn't been set. But if I do it via the CLI instead of a shell script it works just fine and $ASDF is defined.
Why isn't the shell script working?
It is because:
./test.sh
will create a sub shell and set env variables in the sub shell. Once sub shell exits this variable isn't available in parent shell.
Use this form to avoid forking a sub shell and execute test.sh in the current shell itself:
. ./test.sh
OR:
source ./test.sh
Now that variable ASDF will be available in current shell also.

export env at archlinux shell

when I at company , I have to export 3 enviroment variables, http_proxy,https_proxy,all_proxy,
I wrote a file ~/bin/setproxy like this
#! /bin/sh
export http_proxy=http://......:8888
export https_proxy=http://......:8888
export all_proxy=http://......:8888
but when I execute this file at bash, then use env | grep http_proxy , I got nothing.
but "source ~/bin/setproxy" works, but is there any way short this to 1 word command.
I wrote another file only 1 line,
source ~/bin/setproxy
but it does not work.
When you execute that script a sub-shell is spawned and the three export are perfomed in that shell, when the script finishes, the sub-shell exits, that's why you don't see the environment variables as set.
You could put that code in a function, say in your .bashrc, and call that, this way it will work, something like the following:
function setproxy {
export http_proxy=http://......:8888
export https_proxy=http://......:8888
export all_proxy=http://......:8888
}
I think your problem is because you are executing either:
~/bin/setproxy
or:
your_other_file_which_sources_setproxy
In both those cases, they run in a subshell which means the export is in that subshell, not the shell you're calling them from.
You can either use the short form of source:
. ~/bin/setproxy
or create an alias:
alias sp='source ~/bin/setproxy'
in your .bashrc or other startup scripts.
That latter solution will allow you to just execute:
sp
to do the work.

exported variables are not reflected in "env" output

I ran the below script to set environment variables for oracle(oracle_env.sh which comes with oracle package itself).
ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export ORACLE_HOME
ORACLE_SID=XE
export ORACLE_SID
NLS_LANG=`$ORACLE_HOME/bin/nls_lang.sh`
export NLS_LANG
PATH=$ORACLE_HOME/bin:$PATH
export PATH
if [ $?LD_LIBRARY_PATH ]
then
LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
else
LD_LIBRARY_PATH=$ORACLE_HOME/lib
fi
export LD_LIBRARY_PATH
After that when I ran env to ensure that the variables are exported properly, I found no properties are exported(below is the output).
invincible:/home/invincible# /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
invincible:/home/invincible# env | grep ORACLE_HOME
invincible:/home/invincible#
Now I am not sure whether variables are exported properly.If not what I have done wrong? Please help me out.
And one more thing, I am running as root.
The scripts only sets the environment inside the subshell it runs in. You should source it:
# POSIX
. /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
or
# bash/ksh
source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
I believe that when you run a script, bash forks and execs the script in a new shell instance, any exports done in the script doesn't propagate back to your parent shell.
However it seems that you can simply execute your script with:
prompt$ . /path/to/script.sh # note the period!
Example:
prompt$ echo "export FOO=foobar" > /tmp/tst
prompt$ sh /tmp/tst
prompt$ echo $FOO
prompt$ . /tmp/tst
prompt$ echo $FOO
foobar
I believe you should use source to load that script.
source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
From man source:
source filename [arguments]
Read and execute commands from filename in the current shell environment and
return the exit
status of the last command executed from filename.
Exporting variables only makes them available to children of the shell you export them from. There is no way of changing the environment variables in the parent shell, as you seem to be trying to do. You can change the variables in the same shell by sourcing the script using the "dot" command:
. myscript

Resources