Reputation: 143
My /home/s/project/bin/FileOperation.sh
file is:
#!/bin/ksh
/home/s/project/Param/Param_File.config
current=$(dirname $0)
echo $current
echo 'hello'
echo $RootDir
echo $message
My /home/s/project/Param/Param_File.config
file is:
RootDir=/home/s/project
message=hello
When I execute the above shell script I get below output:
.
hello
How can I get the values of $RootDir
and $message
printed?
Is there any shortcut way to import a config file instead of writing /home/s/project/Param/Param_File.config
in shell script?
Upvotes: 0
Views: 2019
Reputation: 8116
Try this:
#!/bin/ksh
. /home/s/project/Param/Param_File.config
current=$(dirname $0)
echo $current
echo 'hello'
echo $RootDir
echo $message
The dot (.
) tells the shell to execute the file as a script in the current environment (thus the environment changes are effective). See e.g. here. You must ensure the executed file is a valid script.
Without the dot(.
) the script is run in a new process, which can't alter it's parent environment.
The relevant part of the documentation:
. name [arg ...]
If name is a function defined with the function name reserved word syntax, the function is executed in the current environment (as if it had been defined with the name() syntax.) Otherwise if name refers to a file, the file is read in its entirety and the commands are executed in the current shell environment. The search path specified by PATH is used to find the directory containing the file. If any arguments arg are specified, they become the positional parameters while processing the . command and the original positional parameters are restored upon completion. Otherwise the positional parameters are unchanged. The exit status is the exit status of the last command executed.
Upvotes: 2