Creating a string variable name from the value of another string - bash

In my bash script I have two variables CONFIG_OPTION and CONFIG_VALUE which contain string VENDOR_NAME and Default_Vendor respectively.
I need to create a variable with name $CONFIG_OPTION ie VENDOR_NAME and assign the value in CONFIG_VALUE to newly created variable.
How I can do this?
I tried
$CONFIG_OPTION=$CONFIG_VALUE
But I am getting an error on this line as
'./Build.bash: line 137: VENDOR_NAME="Default_Vendor": command not found'
Thanks.

I know that nobody will mention it, so here I go. You can use printf!
#!/bin/bash
CONFIG_OPTION="VENDOR_NAME"
CONFIG_VALUE="Default_Vendor"
printf -v "$CONFIG_OPTION" "%s" "$CONFIG_VALUE"
# Don't believe me?
echo "$VENDOR_NAME"

This uses bash builtins:
#!/bin/bash
VAR1="VAR2"
declare "${VAR1}"="value"
echo "VAR1=${VAR1}"
echo "VAR2=${VAR2}"
The script output:
VAR1=VAR2
VAR2=value
Here's the snippet using your variable names:
#!/bin/bash
CONFIG_OPTION="VENDOR_NAME"
declare "${CONFIG_OPTION}"="value"
echo "CONFIG_OPTION=${CONFIG_OPTION}"
echo "VENDOR_NAME=${VENDOR_NAME}"
The script output:
CONFIG_OPTION=VENDOR_NAME
VENDOR_NAME=value

For pure shell, possibly try:
#!/usr/bin/env sh
option=vendor_name
value="my vendor"
eval $option="'$value'" # be careful with ', \n, and \ in value
eval echo "\$$option" # my vendor
echo "$vendor_name" # my vendor
Why?
#!/usr/bin/env sh
printf -v "var" "val" # prints the flag, var not set
declare var=val # sh: declare: not found
echo ${!var} # sh: syntax error: bad substitution
I don't like eval, but are there any POSIX options?

Existing answers are great for strings, but do not work with arrays.
Here is a working solution for arrays:
ARRAY=(a b c d e f)
array_name="ARRAY_NAME"
TARGET_ARRAY="${array_name}[#]" # ARRAY="ARRAY[#]"
TARGET_ARRAY=("${!TARGET_ARRAY}") # ARRAY=(a b c d e f)
the array_name string can also have * suffix to obtain the array elements.

Related

how to assign each of multiple lines in a file as different variable?

this is probably a very simple question. I looked at other answers but couldn't come up with a solution. I have a 365 line date file. file as below,
01-01-2000
02-01-2000
I need to read this file line by line and assign each day to a separate variable. like this,
d001=01-01-2000
d002=02-01-2000
I tried while read commands but couldn't get them to work.It takes a lot of time to shoot one by one. How can I do it quickly?
Trying to create named variable out of an associative array, is time waste and not supported de-facto. Better use this, using an associative array:
#!/bin/bash
declare -A array
while read -r line; do
printf -v key 'd%03d' $((++c))
array[$key]=$line
done < file
Output
for i in "${!array[#]}"; do echo "key=$i value=${array[$i]}"; done
key=d001 value=01-01-2000
key=d002 value=02-01-2000
Assumptions:
an array is acceptable
array index should start with 1
Sample input:
$ cat sample.dat
01-01-2000
02-01-2000
03-01-2000
04-01-2000
05-01-2000
One bash/mapfile option:
unset d # make sure variable is not currently in use
mapfile -t -O1 d < sample.dat # load each line from file into separate array location
This generates:
$ typeset -p d
declare -a d=([1]="01-01-2000" [2]="02-01-2000" [3]="03-01-2000" [4]="04-01-2000" [5]="05-01-2000")
$ for i in "${!d[#]}"; do echo "d[$i] = ${d[i]}"; done
d[1] = 01-01-2000
d[2] = 02-01-2000
d[3] = 03-01-2000
d[4] = 04-01-2000
d[5] = 05-01-2000
In OP's code, references to $d001 now become ${d[1]}.
A quick one-liner would be:
eval $(awk 'BEGIN{cnt=0}{printf "d%3.3d=\"%s\"\n",cnt,$0; cnt++}' your_file)
eval makes the shell variables known inside your script or shell. Use echo $d000 to show the first one of the newly defined variables. There should be no shell special characters (like * and $) inside your_file. Remove eval $() to see the result of the awk command. The \" quoted %s is to allow spaces in the variable values. If you don't have any spaces in your_file you can remove the \" before and after %s.

How to perform pattern substitution on the keys of a bash associative array

I need to perform a pattern substitution on the keys of a bash associative array. Example:
$ declare -A aa=( [A]=0 [B]=1 [C]=2 )
To prefix the values with foo_ one can use:
$ echo --${aa[#]/#/foo_}--
--foo_0 foo_1 foo_2--
But how to prefix the keys? This does not work (at least in GNU bash, version 4.3.30(1)-release):
$ echo --${!aa[#]/#/foo_}-- # <- does not work
----
Is there a better way than the following workaround?
$ declare -a keys=( ${!aa[#]} )
$ echo --${keys[#]/#/foo_}--
--foo_A foo_B foo_C--
You can use printf:
printf 'foo_%s\n' "${!aa[#]}"
foo_A
foo_B
foo_C

Can I assign the default values to array in bash?

* I previously asked a question but it was not the correct question. Now I made the correct question and fixed sample code. And I will up an answer which partially cites an answer to the previous question. *
I would like to default value to arrays in bash. Please see following,
function chmod_chown_func() {
local file_path="$1"
local chmod_options[2]=${2:='-R 744'} # This line has error.
local chown_options[2]=${3:='-R root:root'} # This line has error.
sudo chmod "${chmod_options[#]}" "${file_path}"
sudo chown "${chown_options[#]}" "${file_path}"
}
chmod_chown_func "test.txt"
The error message is
$2: cannot assign in this way
Thank you very much.
Parameter Expansions
Yes, the expansion ${a:=default} changes the value of a.
It is called "Assign Default Values" in the bash manual.
$ unset a
$ echo "<${a}> and <${a:=default}>, But <${a}>"
<> and <default>, But <default>
But that syntax could not be applied to positional parameters.
Positional parameters can be (mostly) changed with set.
$ echo "$#"
a b c
$ set -- d e f
$ echo "$#"
d e f
But you can use the expansion of "Use default value" as called in the manual:
$ unset a
$ echo "<${a}> and <${a:-default}>, But <${a}>"
<> and <default>, But <>
To assign value(s) to an array variable.
A common idiom is
$ array=( aaa bbb ccc )
$ echo "${array[1]}"
bbb
Or:
$ declare -a array=( aaa bbb ccc )
Which also will make the variable local to a function if used inside the function.
However, it comes with the detail that wildcards (*, ? and []) will be expanded (unless quoted or the option set -f is used).
Overall, it is better to use read:
$ IFS=' ' read -a array <<<"$a"
Array index
You can not assign a whole array by using one index. This:
chmod_options[2]=${2:-'-R 744'}
Will only create one array value, at index 2. A better way will be:
chmod_options=( ${2:--R 744} )
Or, as explained above:
IFS=' ' read -a chmod_options <<<"${2:--R 744}"
The follwings are error points and an answer code.
Error 1:
The default value by ${variable:='some value'} does not work with positional parameter.
It should be ${variable:-'some value'}
Error 2:
To assign an default value to an array, declare an array and assign a default array value to it.
An example answer code is following
function chmod_chown_func() {
local file_path="$1"
local -a chmod_options=${2:-( -R 744 )}
local -a chown_options=${3:-( -R root:root )}
sudo chmod "${chmod_options[#]}" "${file_path}"
sudo chown "${chown_options[#]}" "${file_path}"
}
I didn't have much luck with the existing answers, so might as well KISS and use a little logic to determine if the array is empty, and then assign the default value if necessary.
local -a chmod_options="$2"
local -a chown_options="$3"
# Assign default value if chmod_options is an empty string or array
[[ -z "$chmod_options" || ${#chmod_options[#]} -eq 0 ]] && chmod_options=('-R 744')
# Assign default value if chown_options is an empty string or array
[[ -z "$chown_options" || ${#chown_options[#]} -eq 0 ]] && chown_options=('root:root')

Is there a way to loop variables from another file into my bash script?

Sorry to be a pain, but I'm not sure how I can loop values from an outside file, into my bash script as variables. I have three variable names in my bash script:
$TAGBEGIN
$TAGEND
$MYCODE
In a separate varSrc.txt file, I have several variables:
# a - Some marker
tagBegin_a='/<!-- Begin A -->/'
tagEnd_a='/<!-- End A -->/'
code_a=' [ some code to replace in between tags ] '
# b - Some marker
tagBegin_b='/<!-- Begin B -->/'
tagEnd_b='/<!-- End B -->/'
code_b=' [ some code to replace in between tags ] '
# c - Some marker
...
I need my bash script to be able to loop through each "# marker"* section and perform a function:
source varSrc.txt
$TAGBEGIN
$TAGEND
$MYCODE
...
sed '
'"$TAGEND"' R '"$MYCODE"'
'"$TAGBEGIN"','"$TAGEND"' d
' -i $TARGETDIR
Note: sed code logic (not quoting mess) courtesy of Glenn J.
I need some kind of looping logic like:
for (var i = 0; i <= markers in varSrc.txt ; i++) {
// set bash vars equal to varSrc values
$TAGBEGIN= $tagBegin_i
$TAGEND= $tagEnd_i
$MYCODE= $code_i
// run the 'sed' replace command
sed '
'"$TAGEND"' R '"$MYCODE"'
'"$TAGBEGIN"','"$TAGEND"' d
' -i $TARGETDIR
}
Is this something that can be feasibly done in a bash script and is this a good approach? Any suggestions, pointers or guidance is very, very appreciated!
*(which I don't think is a real marker I can use)
[Answering the question as amended]
There's no need use use, iterate over, or think about markers at all. Leave them out.
source varSrc.txt
for beginVar in "${!tagBegin_#}"; do # Iterate over defined begin variable names
endVar=tagEnd_${var#tagBegin_} # Generate the name of the end variable
codeVar=code_${var#tagBegin_} # Generate the name of the code variable
begin=${!beginVar} # Look up the contents of the begin variable
end=${!endVar} # Look up the contents of the end variable
code=${!codeVar} # Look up the contents of the code variable
sed -e "$end R $code" -e "$begin,$end d" -i "$file"
done
[Answers original, pre-amended question]
source only works if your input file is valid bash syntax; it isn't. Thus, you'll need to parse it yourself, something like the following:
begin= end= code=
while IFS= read -r; do
case $REPLY in
#*)
# we saw a marker; process all vars seen so far
[[ $begin && $end && $code ]] || continue # do nothing if we have no vars seen
sed -e "$end R $code" -e "$begin,$end d" -i "$file"
;;
'$TAGBEGIN='*) begin=${REPLY#'$TAGBEGIN='} ;;
'$TAGEND='*) end=${REPLY#'$TAGEND='} ;;
'$MYCODE='*) code=${REPLY#'$MYCODE='} ;;
esac
done <varSrc.txt
What you can do is export your variables in your second file an the execute the script within your current environment (with a dot before the script) to get the variable names/markers you can parse the file and search for an $ or #

Capturing multiple line output into a Bash variable

I've got a script 'myscript' that outputs the following:
abc
def
ghi
in another script, I call:
declare RESULT=$(./myscript)
and $RESULT gets the value
abc def ghi
Is there a way to store the result either with the newlines, or with '\n' character so I can output it with 'echo -e'?
Actually, RESULT contains what you want — to demonstrate:
echo "$RESULT"
What you show is what you get from:
echo $RESULT
As noted in the comments, the difference is that (1) the double-quoted version of the variable (echo "$RESULT") preserves internal spacing of the value exactly as it is represented in the variable — newlines, tabs, multiple blanks and all — whereas (2) the unquoted version (echo $RESULT) replaces each sequence of one or more blanks, tabs and newlines with a single space. Thus (1) preserves the shape of the input variable, whereas (2) creates a potentially very long single line of output with 'words' separated by single spaces (where a 'word' is a sequence of non-whitespace characters; there needn't be any alphanumerics in any of the words).
Another pitfall with this is that command substitution — $() — strips trailing newlines. Probably not always important, but if you really want to preserve exactly what was output, you'll have to use another line and some quoting:
RESULTX="$(./myscript; echo x)"
RESULT="${RESULTX%x}"
This is especially important if you want to handle all possible filenames (to avoid undefined behavior like operating on the wrong file).
In case that you're interested in specific lines, use a result-array:
declare RESULT=($(./myscript)) # (..) = array
echo "First line: ${RESULT[0]}"
echo "Second line: ${RESULT[1]}"
echo "N-th line: ${RESULT[N]}"
In addition to the answer given by #l0b0 I just had the situation where I needed to both keep any trailing newlines output by the script and check the script's return code.
And the problem with l0b0's answer is that the 'echo x' was resetting $? back to zero... so I managed to come up with this very cunning solution:
RESULTX="$(./myscript; echo x$?)"
RETURNCODE=${RESULTX##*x}
RESULT="${RESULTX%x*}"
Parsing multiple output
Introduction
So your myscript output 3 lines, could look like:
myscript() { echo $'abc\ndef\nghi'; }
or
myscript() { local i; for i in abc def ghi ;do echo $i; done ;}
Ok this is a function, not a script (no need of path ./), but output is same
myscript
abc
def
ghi
Considering result code
To check for result code, test function will become:
myscript() { local i;for i in abc def ghi ;do echo $i;done;return $((RANDOM%128));}
1. Storing multiple output in one single variable, showing newlines
Your operation is correct:
RESULT=$(myscript)
About result code, you could add:
RCODE=$?
even in same line:
RESULT=$(myscript) RCODE=$?
Then
echo $RESULT $RCODE
abc def ghi 66
echo "$RESULT"
abc
def
ghi
echo ${RESULT#Q}
$'abc\ndef\nghi'
printf '%q\n' "$RESULT"
$'abc\ndef\nghi'
but for showing variable definition, use declare -p:
declare -p RESULT RCODE
declare -- RESULT="abc
def
ghi"
declare -- RCODE="66"
2. Parsing multiple output in array, using mapfile
Storing answer into myvar variable:
mapfile -t myvar < <(myscript)
echo ${myvar[2]}
ghi
Showing $myvar:
declare -p myvar
declare -a myvar=([0]="abc" [1]="def" [2]="ghi")
Considering result code
In case you have to check for result code, you could:
RESULT=$(myscript) RCODE=$?
mapfile -t myvar <<<"$RESULT"
declare -p myvar RCODE
declare -a myvar=([0]="abc" [1]="def" [2]="ghi")
declare -- RCODE="40"
3. Parsing multiple output by consecutives read in command group
{ read firstline; read secondline; read thirdline;} < <(myscript)
echo $secondline
def
Showing variables:
declare -p firstline secondline thirdline
declare -- firstline="abc"
declare -- secondline="def"
declare -- thirdline="ghi"
I often use:
{ read foo;read foo total use free foo ;} < <(df -k /)
Then
declare -p use free total
declare -- use="843476"
declare -- free="582128"
declare -- total="1515376"
Considering result code
Same prepended step:
RESULT=$(myscript) RCODE=$?
{ read firstline; read secondline; read thirdline;} <<<"$RESULT"
declare -p firstline secondline thirdline RCODE
declare -- firstline="abc"
declare -- secondline="def"
declare -- thirdline="ghi"
declare -- RCODE="50"
After trying most of the solutions here, the easiest thing I found was the obvious - using a temp file. I'm not sure what you want to do with your multiple line output, but you can then deal with it line by line using read. About the only thing you can't really do is easily stick it all in the same variable, but for most practical purposes this is way easier to deal with.
./myscript.sh > /tmp/foo
while read line ; do
echo 'whatever you want to do with $line'
done < /tmp/foo
Quick hack to make it do the requested action:
result=""
./myscript.sh > /tmp/foo
while read line ; do
result="$result$line\n"
done < /tmp/foo
echo -e $result
Note this adds an extra line. If you work on it you can code around it, I'm just too lazy.
EDIT: While this case works perfectly well, people reading this should be aware that you can easily squash your stdin inside the while loop, thus giving you a script that will run one line, clear stdin, and exit. Like ssh will do that I think? I just saw it recently, other code examples here: https://unix.stackexchange.com/questions/24260/reading-lines-from-a-file-with-bash-for-vs-while
One more time! This time with a different filehandle (stdin, stdout, stderr are 0-2, so we can use &3 or higher in bash).
result=""
./test>/tmp/foo
while read line <&3; do
result="$result$line\n"
done 3</tmp/foo
echo -e $result
you can also use mktemp, but this is just a quick code example. Usage for mktemp looks like:
filenamevar=`mktemp /tmp/tempXXXXXX`
./test > $filenamevar
Then use $filenamevar like you would the actual name of a file. Probably doesn't need to be explained here but someone complained in the comments.
How about this, it will read each line to a variable and that can be used subsequently !
say myscript output is redirected to a file called myscript_output
awk '{while ( (getline var < "myscript_output") >0){print var;} close ("myscript_output");}'

Resources