Restrict Specific Variable In Bash - bash

I have a script that sets various variables.
maxMemSize=8g
poolThreadCount=4
...
I have to execute this script on multiple system, all of them running CentOS. Now what is required is to restrict setting of some of these variable on some of the systems. For example on system1, maxMemSize should not be set, while on system2 setting poolThreadCount should be restricted.
Like the way we can set readonly environmental variables to restrict export for those specific variables, can we do some similar trick to restrict setting specific variables in bash scripts too?

To restrict variable re-definition you can set it as read only in your ~/.bashrc or in any other file, lets say ~/.readonly:
readonly maxMemSize=3g
readonly poolThreadCount=2
Now you need to tell your bash which init file should run before it start execution. This is defined by a environmental variable $BASH_ENV:
export BASH_ENV=~/.bashrc
or
export BASH_ENV=~/.readonly
Now execute your script under bash
bash initThreadRead.sh

Related

Finding all the Enviorment Variables in Bash

I recently got a book on shell scripting in bash. It states to list all the Environment Variables using the printenv command. I've noticed though that this doesn't list all variables, for example $PWD or $REPLY or $SSH_TTY. Is their a complete list I can reference for all of these Environment Variables and their Functions?
Within a shell like bash there are two types of variables; environment variables (Wikipedia) and shell variables. There are a number of predefined shell variables.
You can use the export built-in to "promote" a shell variable to an environment variable, which has the effect of making that variable available to any subprocesses launched from the shell.
As the name implies, printenv only reports the process' environment variables. Variables like PWD or REPLY are shell variables, and thus aren't displayed. As suggested in the comments, invoking set with no arguments will print all variables (environment and shell) available in your current session.
To display a list of the Environment Variables you can use
set
#if you want to see it nicely you can pipe it to more like this
set | more

How to overwrite environment variables in mac using Shell Script

I have a set of environment variables that need to be set on the basis of the arguments specified in the shell script.
But the problem is that those variables are already defined in the bash profile
FOR EXAMPLE:
bash_profile has a variable called "KARAN":
export KARAN=/config/1
Now on running the shell script, this is what it should do:
export KARAN=/config/2 (Changed the bash profile's KARAN value to 2)
Your question is not clear. If your script needs to set the env var to a specific value just do so using export VAR=val. What I think you're asking is how to have a script modify the environment of the current shell. And that is impossible without the cooperation of both shells. That is because environment vars are inherited by child processes. But a child process cannot directly modify the environment of its parent process (or some other random process for that matter). To do so the two processes must coordinate the exchange of data. This is typically done by using the source command if the child process is a shell script. Or by having the child process write a series of export statements to stdout and having the parent shell capture and execute those statements. For example, let's say I have a script named set_env that looks like this
#!/bin/sh
echo export KARAN=/config_2
echo export VAR2=val2
The current shell would then do
eval $(set_env)
Note, however, eval is dangerous. I prefer to do this which is slightly safer:
set_env | source /dev/stdin
That, however, only works in shells like ksh and zsh. Due to how bash handles pipelines the source is actually executed in a child shell and therefore the vars won't be set in the current shell.
You can create a new Profile with all the new definitions. and then call the line below on top of your shell script. Similarly, you can create as many profiles as you want and use it.
source bash_profile_new

Can't get environment variable from /etc/profile in GoLang

I've set some environment variables in /etc/profile, I can access them from bash, but for some reason I cant get them from Go.
/etc/profile:
...
TEST_ENV=test_me
I can access it from bash:
echo $TEST_ENV
test_me
I can't access this variable from GO
os.Getenv("TEST_ENV") // returns ""
If I list the available environment variables with
os.Environ()
I don't see the variable I'm looking for, but there a few variables that might help:
SHELL=/bin/sh
USER=root
LOGNAME=root
I guess my problem is related to different sessions and shells, so I even tried running
exec.Command("source /etc/profile")
and get the variables after, but it still returns nothing.
Can you give me some tips how to get environment variables if they're set in /etc/profile? I'd prefer getting them from that file, but if necessary, I can put the variables in a different place as well.
When you set an environment variable in bash, by default it isn't exported. Only exported environment variables are passed along to processes created by the shell (i.e., programs that you run). Try export TEST_ENV=test_me.

Accessing system wide environment variables inside a bash script

I've added a system wide environment variable in /etc/environment. I want to access this variable in a bash script. Is there any way to access it?
Assuming that PATH is an environmental variable, also in /etc/environment, I can access path in a script like this:
#!/bin/sh
echo $PATH
So what's wrong with your variable?

What's the difference of the command output after inputting the command "env", "export", "set" under Bash Shell in Solaris?

OS: Solaris
Shell: Bash Shell
Scenario: Input the commands separately: "env", "export" and "set" (without any arguments) and there will be a list of variables and values returned.
My question: What's the difference among the returned values after inputting the three commands?
The env and export commands yield the same information, but not in the same format. And bash's export produces a very radically different output from the output of ksh or (Bourne) shell's version. Note that set and export are shell built-in commands, but env is an external command that has other uses than just listing the content of the environment (though that is one of its uses).
The set command lists the variables you've created. This includes environment variables, regular (non-environment) variables, and function definitions (which we'll ignore here).
Consider:
x1=abc
x2=def; export x2
export x3=ghi
There are two exported variables (x2 and x3), and one regular (non-exported) variable. The set command will list all three; export and env will only list the exported ones.
The output of the env command is mandated by the POSIX standard. This is simply the variable name and value followed by a newline:
name=value
Classically, the Bourne shell simply listed variables the same way for both set and export.
Korn shell encloses values in quotes if the value contains spaces or other characters that need protection, but otherwise uses the name=value notation.
The set command in bash generates assignments with the value protected in quotes. However, the output for export is a declare -x var=value with quote protection. The general idea is presumably that you can use export > file followed by source file to reset the environment variables to the values that were in the environment at the time you did the export.
Summary
Not all shell variables are environment variables.
The set command lists all shell variables and may list functions too.
The export command lists environment variables.
The set and export commands are built into the shell.
The env command with no arguments lists the environment it inherited from the process that executed it.
The set command shows you all of the shell variables defined in your session.
The export command lists a subset (usually) of the ones above. These are created with either export or declare -x : variables which are globally visible - ie., visible to child processes.
The env command is used to to enable porting scripts from account to another account or machine to machine.
env runs a program in a modified or different environment.

Resources