Create variable from string/nameonly parameter to extract data in bash? - bash

I want to save the variable name and its contents easily from my script.
Currently :-
LOGFILE=/root/log.txt
TEST=/file/path
echo "TEST : ${TEST}" >> ${LOGFILE}
Desired :-
LOGFILE=/root/log.txt
function save()
{
echo "$1 : $1" >> ${LOGFILE}
}
TEST=/file/path
save TEST
Obviously the above save function just saves TEST : TEST
Want I want it to save is TEST : /file/path
Can this be done? How? Many thanks in advance!

You want to use Variable Indirection. Also, don't use the function keyword, it is not POSIX and also not necessary as long as you have () at the end of your function name.
LOGFILE=/root/log.txt
save()
{
echo "$1 : ${!1}" >> ${LOGFILE}
}
TEST=/file/path
save TEST
Proof of Concept
$ TEST=foo; save(){ echo "$1 : ${!1}"; }; save TEST
TEST : foo

Yes, using indirect expansion:
echo "$1 : ${!1}"
Quoting from Bash reference manual:
The basic form of parameter expansion is ${parameter} [...] If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion

Consider using the printenv function. It does exactly what it says on the tin, prints your environment. It can also take parameters
$ printenv
SSH_AGENT_PID=2068
TERM=xterm
SHELL=/bin/bash
LANG=en_US.UTF-8
HISTCONTROL=ignoreboth
...etc
You could do printenv and then grep for any vars you know you have defined and be done in two lines, such as:
$printenv | grep "VARNAME1\|VARNAME2"
VARNAME1=foo
VARNAME2=bar

Related

Setting environment variables with .sh script not working under zsh : how to convert a function [duplicate]

I've noticed that it's usual task to check global variables like PATH, GOPATH and etc. That's why I want to write small function so instead of typing a lot of letters
echo $PATH
I can type just
e PATH
The function itself should be very simple
function e() {
echo $($1) # it produces the error "command not found"
}
But the problem is how to substitute a variable to get the content of PATH?
P.S. I'm using zsh
The traditional (POSIX) notation to handle this uses the eval command, which many will warn you against:
e() {
eval echo \"\$$1\"
}
In bash, however, you can use variable indirection:
function e() {
printf '%s\n' "${!1}"
}
And in zsh, which you added as a tag after my initial answer, indirection is handled differently:
function e() {
printf '%s\n' "${(P)1}"
}
This uses a Parameter Expansion Flag which you can read about in man zshexpn.
P This forces the value of the parameter name to be interpreted as a
further parameter name, whose value will be used where appropriate.

how to call a bash function providing environment variables stored in a Bash array?

I got two variables in a bash script. One contains the name of a function within the script while the other one is an array containing KEY=VALUE or KEY='VALUE WITH SPACES' pairs. They are the result of parsing a specific file, and I can't change this.
What I want to do is to invoke the function whose name I got. This is quite simple:
# get the value for the function
myfunc="some_function"
# invoke the function whose name is stored in $myfunc
$myfunc
Consider the function foo be defined as
function foo
{
echo "MYVAR: $MYVAR"
echo "MYVAR2: $MYVAR2"
}
If I get the variables
funcname="foo"
declare -a funcenv=(MYVAR=test "MYVAR2='test2 test3'")
How would I use them to call foo with the pairs of funcenv being added to the environment? A (non-variable) invocation would look like
MYVAR=test MYVAR2='tes2 test3' foo
I tried to script it like
"${funcenv[#]}" "$funcname"
But this leads to an error (MYVAR=test: command not found).
How do I properly call the function with the arguments of the array put in its environment (I do not want to export them, they should just be available for the invoked function)?
You can do like this:
declare -a funcenv=(MYVAR=test "MYVAR2='test2 test3'")
for pairs in "${funcenv[#]}"; do
eval "$pairs"
done
"$funcname"
Note however that the variables will be visible outside the function too.
If you want to avoid that, then you can wrap all the above in a (...) subshell.
why don't you pass them as arguments to your function?
function f() { echo "first: $1"; echo "second: $2"; }
fn=f; $fn oneword "two words"

Substitution error in bash

Here is the code:
declare -A data84
data84=( [Tom]=23 [Lucy]=32 )
function test()
{
data=$1
echo ${${data}[Tom]} #error 1: "${${data}[Tom]}" bad substitution
a=${data}[Tom]
echo ${a} #output unwanted result data84[Tom]
}
test data84
I wanted this function to be able to give 23 when calling echo ${data84[Tom]}. And also can you explain why I got the error 1?
If you have Bash 4.3 or newer, this would be an excellent opportunity to use a nameref. In doing so, you'll not only be able to access array elements without contortions, but also assign to them:
testfunc () {
local -n data=$1
echo "In testfunc: ${data[Tom]}"
data[Lucy]=99
}
declare -A data84
data84=([Tom]=23 [Lucy]=32)
testfunc data84
echo "After testfunc:"
declare -p data84
This will print
In testfunc: 23
After testfunc:
declare -A data84='([Tom]="23" [Lucy]="99" )'
so we've actually changed data84.
Notice that test is a dangerous name for a function, as it might clash with the test shell builtin.
#try:
declare -A data84
data84[Tom]=23
data84[Lucy]=32
function test()
{
data=$1
echo $data[Tom]
a=$data[Tom]
echo ${!a}
}
test data84
Output will be as follows
data84[Tom]
23
Explanation:
From man bash (parameter expansion):
${parameter}
The value of parameter is substituted. The braces are required
when parameter is a positional parameter with more than one
digit, or when parameter is followed by a character which is not
to be interpreted as part of its name. [...]
If the first character of parameter is an exclamation point (!),
a level of variable indirection is introduced. Bash uses the value of
the variable formed from the rest of parameter as the name of the
variable; this variable is then expanded and that value is used in the
rest of the substitution, rather than the value of parameter
itself. This is known as indirect expansion. The exceptions to this
are the expansions of ${!prefix*} and
${!name[#]} described below. The exclamation point
must immediately follow the left brace in order to introduce
indirection.

Accessing function-definition-time, not evaluation-time, value for a variable in bash

I hope that I can do something like this, and the output would be "hello"
#!/bin/bash
foo="hello"
dummy() {
local local_foo=`echo $foo`
echo $local_foo
}
foo=''
dummy
This question means that I would like to capture the value of some global values at definition time, usually used via source blablabla.bash and would like that it defines a function that captures current variable's value.
The Sane Way
Functions are evaluated when they're run, not when they're defined. Since you want to capture a variable as it exists at definition time, you'll need a separate variable assigned at that time.
foo="hello"
# By convention, global variables prefixed by a function name and double underscore are for
# the exclusive use of that function.
readonly dummy__foo="$foo" # capture foo as of dummy definition time, and prevent changes
dummy() {
local local_foo=$dummy__foo # ...and refer to that captured copy
echo "$local_foo"
}
foo=""
dummy
The Insane Way
If you're willing to commit crimes against humanity, however, it is possible to do code generation to capture a value. For instance:
# usage: with_locals functionname k1=v1 [k2=v2 [...]]
with_locals() {
local func_name func_text assignments
func_name=$1; shift || return ## fail if out of arguments
(( $# )) || return ## noop if not given at least one assignment
func_text=$(declare -f "$func_name")
for arg; do
if [[ $arg = *=* ]]; then ## if we already look like an assignment, leave be
printf -v arg_q 'local %q; ' "$arg"
else ## otherwise, assume we're a bare name and run a lookup
printf -v arg_q 'local %q=%q; ' "$arg" "${!arg}"
fi
assignments+="$arg_q"
done
# suffix first instance of { in the function definition with our assignments
eval "${func_text/{/{ $assignments}"
}
...thereafter:
foo=hello
dummy() {
local local_foo="$foo"
echo "$local_foo"
}
with_locals dummy foo ## redefine dummy to always use the current value of "foo"
foo=''
dummy
Well, you can comment out or remove the foo='' line, and that will do it. The function dummy does not execute until you call it, which is after you've blanked out the foo value, so it makes sense that you would get a blank line echoed. Hope this helps.
There is no way to execute the code inside a function unless that function gets called by bash. There is only an alternative of calling some other function that is used to define the function you want to call after.
That is what a dynamic function definition is.
I don't believe that you want that.
An alternative is to store the value of foo (calling the function) and then calling it again after the value has changed. Something hack-sh like this:
#!/bin/bash
foo="hello"
dummy() {
${global_foo+false} &&
global_foo="$foo" ||
echo "old_foo=$global_foo new_foo=$foo"
}
dummy
foo='new'
dummy
foo="a whole new foo"
dummy
Calling it will print:
$ ./script
old_foo=hello new_foo=new
old_foo=hello new_foo=a whole new foo
As I am not sure this address your real problem, just: Hope this helps.
After inspired by #CharlesDuffy, I think using eval might solve some of the problems, and the example can be modified as following:
#!/bin/bash
foo="hello"
eval "
dummy() {
local local_foo=$foo
echo \$local_foo
}
"
foo=''
dummy
Which will give the result 'hello' instead of nothing.
#CharlesDuffy pointed out that such solution is quite dangerous:
local local_foo=$foo is dangerously buggy: If your foo value contains
an expansion such as $(rm -rf $HOME), it'll be executed
Using eval is good in performance, however being bad in security. And therefore I'd suggest #CharlesDuffy 's answer.

ksh: Defining a parameter name with another parameter's value

I have a ksh script that reads a profile script with a number of sessions defined. Each session defines its own parameters as such:
SESSION_ONE_USER=...
SESSION_ONE_PWD=...
SESSION_TWO_USER=...
...
The script gets the SESSION parameter from the command line, but I simply set it for the example.
I want to let the SESSION parameter value define part of another parameter name, that I need the value from, like:
SESSION="SESSION_ONE"
USER=${${SESSION}_USER}
PASS=${${SESSION}_PWD}
That gives me a compile error.
I also tried
GET_USER_PARAM(){
echo ${SESSION}_USER
}
echo $`GET_USER_PARAM`
But that returns $SESSION_ONE_USER
I want it to return the value of the parameter SESSION_ONE_USER instead.
Does anyone have any solutions?
This is what eval is for:
SESSION=SESSION_ONE
eval echo \$${SESSION}_USER
should display the value of $SESSION_ONE_USER.
Don't monkey with variable names, use associative arrays instead
typeset -A users
typeset -A pwd
session=SESSION_ONE
users[$session]=joe
pwd[$session]=secret
for key in "${!users[#]}"; do
echo "user for session $key is ${users[$key]}"
echo "pwd for session $key is ${pwd[$key]}"
done
Try this:
SESSION="SESSION_ONE"
SESSION_ONE_USER="foo"
SESSION_ONE_PWD="bar"
SESSION_USER=${SESSION}_USER
SESSION_PWD=${SESSION}_PWD
USER=${!SESSION_USER}
PASS=${!SESSION_PWD}
echo $USER
echo $PASS
The "!" does a level of indirection. See Shell Parameter Expansion.
If this is ksh, then this is a job for nameref
alias nameref='typeset -n'
Example Solution
function session_parameters { set -u
typeset session=${1:?session name}
nameref user=SESSION_${session}_USER
nameref pass=SESSION_${session}_PASS
print session=$session user=$user pass=$pass
}
SESSION_ONE_USER="User1"
SESSION_ONE_PASS="Pass1"
SESSION_TWO_USER="User2"
SESSION_TWO_PASS="Pass2"
for s in ONE TWO THREE; do
session_parameters $s
done
Sample output
session=ONE user=User1 pass=Pass1
session=TWO user=User2 pass=Pass2
test_session_parameters[12]: session_parameters: line 5:
SESSION_THREE_USER: parameter not set
Note the usage of set -u to force the error message on line 3.
nameref usage: (from the builtin help text)
NAME
typeset - declare or display variables with attributes
SYNOPSIS
typeset [ options ] [name[=value]...]
-n
Name reference.
The value is the name of a variable that name references. name cannot contain a ... Cannot be use with any other options.

Resources