I have declared one variable IS_abc=false, on basis of certain condition I am changing value to IS_abc=true
IS_abc=false
declare -a my_arr
my_arr = ('abc' 'pqr' 'xyz')
....
.... // some operation
IS_abc=true
for i in "${my_arr[#]}"
do
//here i want to access value of $IS_abc as true
//how to do this
done
I have tried accessing using $IS_'$i' , but it raising error as invalid substitution
Tell me if I am doing anything wrong here?
You can use indirect var reference:
my_arr=('abc' 'pqr' 'xyz')
IS_abc=true
var="IS_${my_arr[0]}"
echo "${!var}"
Output:
true
I'm doing it like this:
value=`eval echo \\${IS_${i}}`
There's probably a better way but this should work.
Related
Mail::send('emails.cont',['name'=>$n,'email'=>$email],function($message){
$message->to('abc#gmail.com','efe')->from('cde#gmail.com')->subject($s);
});
There for the subject, I am trying to pass a variable called $s which contains a value which defined there. But, it will underlying in red and saying undefined variable called $s. How to solve that?
can you please try the following code
Mail::send('emails.cont',['name'=>$n,'email'=>$email],function($message) use ( $s ) {
$message->to('abc#gmail.com','efe')->from('cde#gmail.com')->subject($s);
});
I'm trying to store a value returned from a method like this: var=$(methodName), but the program never enters the method... It's weird because I do the same thing a few lines earlier (alreadyExists-variable in code sample), and it works fine. I had to do this: var='methodName' to make the program enter the method.
It works, so why care? I'm probably making a mistake, and I need to know what it is and learn from it. Let me know if you need more info to answer the question. Thanks!
overwriteOrNot()
{
echo DEBUG
# This debug string does not print if method is called from "local overwrite=$(overwriteOrNot)"
# but prints if method is called from "local overwrite='overwriteOrNot'"
...
}
local alreadyExists=$(studentNumberExists studentNumber)
if $alreadyExists ; then
# local overwrite=$(overwriteOrNot)
local overwrite='overwriteOrNot'
...
If you're using return, then you need to either directly branch on its result:
if overwriteOrNot; then
: "the function returned 0"
else
: "the function returned something other than 0"
fi
...or store the value of $? immediately after running the function:
overwriteOrNot
local overwrite=$?
Note that return can only return a single-byte integer. If you need to pass content which doesn't fit that type, it needs to be either passed on stdout or in a global variable.
The following:
local overwrite='overwriteOrNot'
assigns a string; it doesn't invoke a function. Instead:
local overwrite=$(overwriteOrNot)
You can check the return value from calling overwriteOrNot with the $? variable, or by checking its numeric return value directly in a conditional statement like:
if overwriteOrNot; then
:
fi
If you assign to overwrite, you can also check its value with any valid test condition such as equality, regular expression match, or emptiness. For example:
if [[ "$overwrite" == "foo" ]]; then
:
fi
I'm not really sure a good name for this question so please rename it if you can think of a better one.
In Bash I have a function that I am using to store certain functions. Consider:
menv_funcs=()
function menv_function {
menv_funcs+=($1)
}
I am then using it in this manner:
menv_function fetch
function fetch {
...
}
I would like to use it like this though:
menv_function fetch {
...
}
Essentially I'm looking for something like the preprocessor macros in C would do but I have been unable to find a way. Any ideas?
As far as I'm aware, you can't directly achieve this. However, I can think of two solutions that may be of interest to you.
First of all, you could just declare the functions as usual, and then obtain the list of declared functions through declare -F. This could be done like:
function fetch {
:
}
menv_funcs=()
while IFS=$"\n" read l; do
menv_funcs+=${l#declare -f }
done < <(declare -F)
Which will cause menv_funcs[#] to list all the functions declared at the point of calling the snippet. Of course, this may catch unwanted functions as well.
To avoid this, you may add some prefix to function names and filter the list:
function menv_fetch {
:
}
menv_funcs=()
while IFS=$"\n" read l; do
if [[ ${l} == 'declare -f menv_'* ]]; then
menv_funcs+=${l#declare -f menv_}
fi
done < <(declare -F)
And if you really want to achieve something like macros, you may try to play with eval:
menv_funcs=()
function menv_function {
local name=${1}
local body=${2}
menv_funcs+=( ${name} )
eval "function ${name} ${body}"
}
menv_function fetch "{
:
}"
But note that you will actually need to quote the whole function body and escape everything appropriately.
I'm trying to query an exist-db with xquery by taking parameters from the URL and building up seach parameters
xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare option exist:serialize "method=xml media-type=text/xml omit-xml-declaration=no indent=yes";
let $param1:= request:get-parameter("param1",'0')
let $person :=
if($param1 = '0')
then "'*'"
else concat('contributions/person/#val="',$param1,'"')
return
<xml>
{
for $x in subsequence(//foo/bar[$person],1,3)
return $x
}
</xml>
The code above shows that I get the parameter from the url $param1.
variable $person checks to see if there was a parameter and based on that creates a query parameter. This variable works fine, from testing it prints out either '*' for no param or
contributions/person/#val='hello, world'
When I run the query it prints out as if the value is '*'. In the for $x part, can I pass a variable like that? I've tried putting concat($person,'') with the same results. Hardcoding the full path gives me the results I'm looking for, but I'm looking to create something more dynamic.
To note: there is only one variable, $person, but there will be others once I get it to work
I think ideally you would avoid dynamic string evaluation. In this example, some pretty simple reorganization would solve the problem without it:
<xml>
{
for $x in subsequence(//foo/bar[
if ($param1 = '0')
then *
else (contributions/person/#val = $param1)
],1,3)
return $x
}
</xml>
However, you can use eval(), but keep in mind there are security risks:
<xml>
{
for $x in subsequence(eval(
concat('//foo/bar[',$person,']')
),1,3)
return $x
}
</xml>
I have an array called config. I'm trying to echo a variable from the array in the session.
I've tried:
echo $this->session->userdata('config['item']');
but it doesn't work. What's wrong with my syntax here? I've print_r'd my session and the items are in the config array. I've also tried:
echo $this->session->userdata("config['item']");
I get no errors this time, but no data either.
If config is an array . And item is string name of what you want to get from config then
echo $this->session->userdata($config['item']);
or
echo $_SESSION[$config['item']];
If config is an array inside session you should first get it.
$tmp = $this->session->userdata('config');
echo $tmp['item'];
or
echo $_SESSION['config']['item']
Sorry for my english.
If you want to use the session array, use the variable, not the function:
echo $this->session->userdata['user_data']['item'];
If you want to write:
$this->session->userdata['user_data']['item'] = 'value';
$this->session->userdata['other_data']['other'] = 'value2';
$this->session->sess_write();
This allows you to edit values in array just like you do with $_SESION['user_data']['avatar'] = $avatar, with 'only' one extra line and only using CI library.
Always escape your string it should be this way:
echo $this->session->userdata('config[\'item\']');