A variable of which a part is variable - linux

I have variables that look something like this:
$INFOMonday
$INFOTuesday
In my Bash script, I would like to use the value of todays variable, so to say. I extracted the day with:
dayofweek=$(date --date=${dateinfile#?_} "+%A")
What I need help with, is doing the following:
echo $INFO$dayofweek
Is there any way of adding a variable as part of a variables name? For example, if it's monday, the echo would return the value of $INFOMonday.

The old-style way of doing this is with the indirection operator ${!variable}:
dayofweek=$(date...)
var=INFO$dayofweek
echo ${!var}
Note that in ${!var}, var must be a variable-name, not a string expression or other type of substitution. It's a little clunky.
The newer way of doing it would be with an associative array.

Related

Bash - Creating a string with a string variable

I have a variable called CURRENTDATE=20151105.
I want to create a string as below:
abc_20151105_20151105
I tried the following variations:
echo "abc_$CURRENTDATE_$CURRENTDATE"
This gave abc_20151105
echo "abc_'$CURRENTDATE'_'$CURRENTDATE'"
This gave abc_'20151105'_'20151105'
What am I missing here? Thanks in advance!
The problem is that the underscore is a valid character for a variable name. Try one of these:
echo "abc_"$CURRENT_DATE"_"$CURRENT_DATE
echo "abc_${CURRENT_DATE}_$CURRENT_DATE"
Bash doesn't have a concatenation operator, so you concatenate strings by smashing them together in the command; this is what the first example is doing. The second uses braces to explicitly point out the variable name.
You must surround the variable name with ${} in order to isolate it from other valid characters. Like this:
echo "abc_${CURRENT_DATE}_${CURRENT_DATE}"

Unbound variable during url printing in bash/shell

I have 2 variables that I want to use to derive a 3rd variable:
export REGION_NAME=phx
export phx_url=https://www.google.com
I am trying to do the following:
echo "$((${REGION_NAME}_url))"
And I get the following error:
-sh: https://www.google.com: syntax error in expression (error token is "://www.google.com")
All I am trying to do is to derive an environment variable from an other one but it does not work simple like that. I think it has to be escaped and could not find anything online.
Thanks in advance for the help.
$((...)) is arithmetic expansion. You didn't mean that. Try normal variable expansion (with indirection) instead.
REGION_NAME=phx
phx_url=https://www.google.com
R_VAR=${REGION_NAME}_url
echo "${!R_VAR}"
One possible solution is using eval like:
var="phx"
eval "${var}_url='some'"
echo $phx_url #prints "some"
But, I not recommending this (because the eval could be pretty dangerous).
Instead of use associative arrays (aka hash variable), like:
declare -A urls
var="phx"
urls[$var]="some2"
echo "${urls[phx]}" #prints "some2"

BASH variable values as new defined variables

I don't know exactly how to ask this in English, but I want to have the value of a variable as a new variable...
The script also has a loop with increasing numbers, and in the end I want to have the variables VAR1, VAR2 etc.
I'm trying this:
COUNT=$(echo 1)
DEFINE=$(echo VAR$COUNT)
$DEFINE=$(echo gotcha!)
When I try this way, I have this error message:
~/script.sh: line n: VAR1=gotcha!: command not found
I played a bit around with brackets and quotation marks, but it didn't work... any solutions?
The problem is that bash expects a command as a result of expansions, not an assignment. VAR1=gotcha! is not a command, hence the error.
It would be better to use an array:
COUNT=$(echo 1)
VAR[COUNT]='gotcha!'
echo ${VAR[COUNT]}
I guess $(echo 1) stands for a more complex command, otherwise you can just use COUNT=1.
You can use declare to create such a "dynamic" variable, but using an array is probably a better choice.
COUNT=1
DEFINE="VAR$COUNT"
declare "$DEFINE=gotcha"

Concatenate string literals

I've seen a couple posts for this, like this one, but none are helping me in my particular situation.
scriptsPath="/var/db/gbi/scripts/"
echo "$scriptsPathawesome.csv";
I would expect this to echo /var/db/gbi/scripts/awesome.csv
Instead I get .csv
Seems like it thinks I'm trying to reference a variable named $scriptsPathawesome. How can I concatenate the $scriptsPath variable to the "awesome.csv" string literal?
You need to surround your variable with curly braces like so:
scriptsPath="/var/db/gbi/scripts/"
echo "${scriptsPath}awesome.csv";

Default values for the arguments to a Unix shell script?

Normally when a parameter is passed to a shell script, the value goes into ${1} for the first parameter, ${2} for the second, etc.
How can I set the default values for these parameters, so that if no parameter is passed to the script, we can use a default value for ${1}?
You can't, but you can assign to a local variable like this: ${parameter:-word} or use the same construct in the place you need $1. this menas use word if _paramater is null or unset
Note, this works in bash, check your shell for the syntax of default values
You could consider:
set -- "${1:-'default for 1'}" "${2:-'default 2'}" "${3:-'default 3'}"
The rest of the script can use $1, $2, $3 without further checking.
Note: this does not work well if you can have an indeterminate list of files at the end of your arguments; it works well when you can have only zero to three arguments.
#!/bin/sh
MY_PARAM=${1:-default}
echo $MY_PARAM
Perhaps I don't understand your question well, yet I would feel inclined to solve the problem in a less sophisticated manner:
! [[ ${1} ]] && declare $1="DEFAULT"
Hope that helps.

Resources