Eval ( ) Shell
eval is a built-in Linux command which is used to execute arguments as a shell command. It combines arguments into a single string and uses it as an input to the shell and execute the commands.
Eval ( ) Shell
REPLs can be created to support any text-based language. REPL support for compiled languages is usually achieved by implementing an interpreter on top of a virtual machine which provides an interface to the compiler. For example, starting with JDK 9, Java included JShell as a command-line interface to the language. Various other languages have third-party tools available for download that provide similar shell interaction with the language.
As a shell, a REPL environment allows users to access relevant features of an operating system in addition to providing access to programming capabilities. The most common use for REPLs outside of operating system shells is for interactive prototyping. Other uses include mathematical calculation, creating documents that integrate scientific analysis (e.g. IPython), interactive software maintenance, benchmarking, and algorithm exploration.
so $CONDA_PATH/bin/conda shell.bash hook will probably generate a bash command line which, when executed, will connect Anaconda into the running bash process, and eval "$($CONDA_PATH/bin/conda shell.bash hook)" will take care of executing that command. Try running $CONDA_PATH/bin/conda shell.bash hook directly from a shell prompt to see what it is producing, in order to better understand what is going on. This may well help you understanding why it isn't working in the Docker context.
In the Bash programming language, the eval built-in has a wide range of applications. That is, it can execute other commands, set shell environment, and perform variables substitution and indirection.
Note that while "$@" is essentially always better than eval "$@", it's easy to unintentionally introduce a dependency on bad behavior through the shell debugging anti-strategy of "adding quotes until it works":
But command "eval echo \$$User" runs the parameter passed to eval. After expansion, remaining parameters are echo and $Hello. Hence eval command runs the command echo $Hello. And so the output is Mr. X
Although the syntax looks pretty plain, but the power lies in the simplicity itself. If we want to get deeper on what exactly is performed. The argument is converted like a single string and that acts as an input to another child process of the shell where the resulting commands are executed.
In this section let us look at some hands-on some examples from the real world in order to understand the eval function in detail. Here we would start with the basics of eval function, then move on to an example where we would print value of a variable assigned to another variable, and in the final one, we would look at a simulation of accessing the index use case we talked about earlier.
command="ls -lrt"echo "We will execute a command of finding the list of files in a directory"echo "In the first we will just execute '$command' resulting in same output as eval"$commandecho "Now we will perform eval of the variable command which will use the command variable as a string and execute the command in child process."eval $command
#!/bin/bashecho "Storing var1 with a value EduCBA"var1="EduCBA"echo "Assign var2 as string var1 "var2=var1echo "We will store the echo as well s a string in another var"echo_var="echo"echo "We will send the string containing echo_var, str2's output as a variable and then print it"echo "The eval statement is similar to echo of var1"eval $echo_var \$$var2
In conclusion, we have simulated an index accessing use case and tried to simplify the process of understanding eval through our 3 examples. If we carefully note that in some cases the eval is the same as normally executing the command, as evident from the first example. From the example 2 onwards, we were able to feel the power of eval and finally in example 3, we are well appreciating the use of eval in shell script and why shell script has attained such popularity.
If there are no arguments, or only null arguments, eval shall return a zero exit status; otherwise, it shallreturn the exit status of the command defined by the string of concatenated arguments separated by s.
I have two shell scripts Caller and getGlobalParameters.There is a function defined in the getGlobalParameters script which calls a java class and uses the values returned by the java class to set the environment variables.Once the call to the getGlobalParameters completes, the caller is trying to use the environment variables set by the getGlobalParameters.The issue I am facing is the environment variables set by the getGlobalParameters are not accessible to the Caller.Below is the code snippet of Caller and getGlobalParameters.
where getGlobalParameters.sh is an executable. As any executable it runs in its own environment and it cannot change the environment of its parent. It's "accidental" getGlobalParameters.sh is a script. The script is interpreted probably by the same shell as the parent (Caller.sh), but not by the same instance (process) as the parent. The child process initially inherits the environment from the parent, but then the environments are independent.
This is particularly annoying when using the shell to generate output files from mongodb. Files need to be cleansed of the extraneous output. Both mongosh and mongo apparently echo some output to STDOUT, so the results of --eval end up in the output. This appears to be true only for the last expression evaluated by --eval, e.g. --eval 'expr1;expr2;expr3'; only the result of "expr3" will be seen in echoed output.
I am aware that this behavior has be reported in: SERVER-4391, SERVER-23810(closed), SERVER-27159(closed), however, it remains unresolved. The proposed workaround in SERVER-4391 of the "void" solution is a clumsy, especially when embedding in a Bash shell script, at best. I would really like to see this fixed as it's the only way that I know of to pass arguments into a mongosh script.
The JShell API and tool will provide a way to interactively evaluatedeclarations, statements, and expressions of the Java programminglanguage within the JShell state. The JShell state includes an evolvingcode and execution state. To facilitate rapid investigation and coding,statements and expressions need not occur within a method, andvariables and method need not occur within a class.
The jshell tool will be a command-line tool with features to easeinteraction including: a history with editing, tab-completion, automaticaddition of needed terminal semicolons, and configurable predefinedimports and definitions.
A new interactive language is not the goal: All accepted input must matchgrammar productions in the Java Language Specification (JLS). Further,within an appropriate surrounding context, all accepted input must bevalid Java code (JShell will automatically provide that surroundingcontext -- the "wrapping"). That is, if X is an input that JShellaccepts (as opposed to rejects with error) then there is an A and Bsuch that AXB is a valid program in the Java programming language.
Exploration of coding options is also important for developersprototyping code or investigating a new API. Interactive evaluation isvastly more efficient in this regard than edit/compile/execute andSystem.out.println.
The JShell API will provide all of JShell's evaluation functionality.The code fragments that are input to the API are referred to as"snippets". The jshell tool will also use the JShell completion API todetermine when input is incomplete (and the user must be prompted formore), when it would be complete if a semicolon were added (in which case thetool will append the semicolon) and also how to complete input whencompletion is requested with a tab. The tool will have a set of commandsfor query, saving and restoring work, and configuration. Commands willbe distinguished from snippets by a leading slash.
The JShell state is held in an instance of JShell. A snippet isevaluated in a JShell with the eval(...) method, producing anerror, declaring code, or executing a statement or expression. In thecase of a variable with an initializer, both declaration and executionoccur. An instance of JShell contains previously-defined andmodified variables, methods, and classes, previously-defined importdeclarations, the side-effects of previously-entered statements andexpressions (including variable initializers), and external code bases.
Since the desired use is exploration, the declarations (variables,methods, and classes) must be able to evolve over time while, at the sametime, preserving evaluated data. One choice would be to make a changeddeclaration a new additional entity in some or all cases, but that iscertain to be confusing and does not play well with exploring theinteraction between declarations. In JShell, each unique declaration keyhas exactly one declaration at any given time. For variables and classesthe unique declaration key is the name, and,the unique declaration key for methods is the name and the parametertypes (to allow for overloading). As this is Java, variable, methods,and classes each have their own name spaces. 350c69d7ab