Let's consider the following situation:
I've created variabel as follows: my_var=32
I can read its value as follows: echo $my_var
I want to read address of this variable, but I can't figure it out.
Is it possible to perform it?
Is it possible to perform it?
Write a Bash builtin that will basically call var_lookup() with the variable name and then print SHELL_VAR address, or address of value.
You can also compile Bash with debugging symbols and inspect it with a debugger.
Related
In today's class, we were asked to write a script that runs a C file through the preprocessor and save the result into another file, with the following conditions:
The C file name will be saved in the variable $NAMEFILE
The output should be saved in the file NAME
One my classmates fastly wrote the following code as the solution:
#!/bin/bash
gcc -E -o NAME $NAMEFILE
I understand all of the code above, except the last part where the variable $NAMEFILE is written, it seems tricky, why can you simply store the C file name in that variable without even declaring it? the GCC manual doesn't seem to explain that, so I just want to get the logics behind this.
Based on the feedback provided in comment section of my post, the $NAMEFILE variable was a environment variable. I was suggested to read the docs related to environment variables, which I will in the following hours.
My problem was solved.
You are conflating gcc operation / features with the behavior of a shell. The gcc manual would not say anything about "storing the filename in a variable" because that's part or the environment where gcc would be run.
in windows, it would look like this:
gcc -E -o NAME %FILENAME%
You're not going to find any explanation of that in the gcc manual.
The shell (bash) is like "the windows explorer". It's what you use to give the computer (OS) instructions. instead of clicking folders you do cd folder and instead of "double click" you type "./foo/command" or just "gcc".
So just keep that in mind and you'll be fine.
So i have a tcl code that i need to run in ns2. As we all know i just have to type 'ns abc.tcl' in terminal. In my abc.tcl code i have a variable x which i need to change and run the code. Is there any way i can write a script that will change the value of x and run 'ns abc.tcl' in terminal, then change the value again and run 'ns abc.tcl' in terminal again for a set of values for x. I believe i need to write a shell script but i don't know anything about that. Can you tell me the format i should write the script in like what should i write first and where do i write my values of x and how to make it run 'ns abc.tcl in terminal: 'function()' 'do' 'done' etc... If you can direct me to specific links about that would be helpful.
The easiest way, providing it works, is to pass the value in as an argument.
Invoke your code as ns abc.tcl TheValueToPassIn.
Access the value within your code by indexing into the argv global variable with lindex, which should contain a list of all arguments after the script name:
set myValue [lindex $::argv 0]
However, it's possible that that won't work (depending on exactly what the ns program does). If so, pass the value in inside an environment variable:
Invoke your code as MYVAR=TheValueToPassIn ns abc.tcl.
Access the value within your code by looking in the global env array:
set myValue $::env(MYVAR)
There are many other ways to do it, but those two are very easy.
I'm stuck on this little problem. I was wondering if it is possible to pass a variable of a bash-shell script to a f90 code?
I am pretty sure it was discussed here before, but I cannot find an exact duplicate.
You can pass arguments directly as arguments to the program
./program arg1 arg2
you can retrieve the values in the program as character strings in Fortran 2003 using subroutines GET_COMMAND ARGUMENT and COMMAND_ARGUMENT_COUNT. Click on the links to get useful examples.
In older Fortran you have to use non-standard extensions, such as the subroutines GETARG and IARGC.
You can also read an environment variable which was set in the script
VAR1 = ...
VAR2 = ...
./program
using Fortran 2003 subroutine GET_ENVIRONMENT_VARIABLE. In older Fortran you have to use some non-standard extension, such as the subroutine GETENV.
You can also redirect a file to the standard input of the program and read the data using the READ statement.
You can choose between the following possibilities:
In bash use export of the variable
myvar="example"; export myvar
Add them as argument to the fortran call
myFortran "${myvar}"
Write them to a file and read the file
Worst solution, just to mention them all
Write it to stdin of fortran program
echo "${myvar}" | myFortran
And you can use the following procedures to read an environment variable: get_environment_variable
https://gcc.gnu.org/onlinedocs/gfortran/GET_005fENVIRONMENT_005fVARIABLE.html
Or read the number and value of the command argument: command_argument_count,get_command_argument. See:
https://gcc.gnu.org/onlinedocs/gfortran/Intrinsic-Procedures.html#Intrinsic-Procedures
i have a tcl file and defining my environment variable inside that file. something like below
set env(ET_PINASSIGN_SCRIPT) $ET_PINASSIGN_SCRIPT
where $ET_PINASSIGN_SCRIPT variable will have a user incoming input value. Now I need to read this env variable in a shell file (#!/bin/ksh
). This is what i am trying and not working
$env ET_PINASSIGN_SCRIPT .
Any suggestions?
Thanks
I understand this is not possible. A program which is running, eg. your script, receives a duplicate of the environment, and can modify it. But when the program stops running, it disappears, together with its environment and changes.
There are few direct methods to communicate 'from the dead' process. You could create a temporary file, or return simple integers from the exit code.
Another way, would be to run the other program concurrently in a way that they share the same environment,
More info on the environment:
http://en.wikipedia.org/wiki/Environment_variable
Edit: I feel I wasn't clear enough: I just wanted to point out that programming that modifies the environment for other programs is 'extreme' (i.e. dangerous) programming. All kind of tricks can be done which can have lasting influences on the use of the computer.
The simplest method would be to make the Tcl code write a shell script to set the variable to standard out (with puts) and to then do:
eval `tclsh yourscript.tcl`
in your shell.
The Tcl code would probably be something like:
puts "ET_PINASSIGN_SCRIPT='$ET_PINASSIGN_SCRIPT'"
except you'll have to worry about any embedded ' characters, so might do…
puts "ET_PINASSIGN_SCRIPT='[string map {' '\\''} $ET_PINASSIGN_SCRIPT]'"
Okay I know there is a bash debugger. But what I'm seeking is if I had an environment variable in one of my startup scripts and I don't know how it was set or where it might be, is there a way to find it other than exhaustively searching the scripts?
I mean is there a mechanism/tool that provides such a thing? Does bash keep track of variable setting locations?
Even though this might not seem very important but it crossed my mind the other day when I was helping a friend install OpenCL and the package supposedly set the variable $ATISTREAMSDKROOT automatically. Anyway the package was supposed to add a file to /etc/profile.d to allow for setting the variable, but it didn't. And luckily the variable came out blank.
But I was wondering if it hadn't come out blank, and the package added it to some random file, I would have probably had no way of telling where it is other than looking for it.
Of course I know one could write a sed command or two and search through the scripts but I'd consider that exhaustive search :D
One option would be to start an instance of bash with:
bash -x
... and look for where the variable is set in that output. To redirect that output to a file, you could do:
bash -x -ls -c "exit" 2> shell-startup-output
You should see in the output where each file is sourced.