Contact multiple variable with string bash - bash

This is my script code
#!/bin/bash
timestamp=$(date +%F-%T)
clinet_id="123"
STRING=s3://<bucketname>/folder/$client_id/$client_id_gdpr_access_report_$timestamp.csv
echo "$STRING"
$SHELL
If i run this code am getting timestamp value.csv file
how can i concatenate variable with string.
am expecting out put like below
s3://<bucketname>/folder/123/123_report_2022-01-25-14:55:47.csv
i can able to concatenateaccess_report_$timestamp.csv
if i add $client_id_ in the beginning, it will print
2022-01-25-14:55:47.csv
Expecting a better advice

You need to look better at the names of your variables; it's 'client_id' not 'clinet_id' ...
And you should take care of double quoting your string, and put braces around variables when in doubt:
STRING="s3://<bucketname>/folder/${client_id}/${client_id}_gdpr_access_report_${timestamp}.csv"

Related

How can you use the actual regex value in an environment variable?

I'm writing a shell script for an assignment. The script is supposed to read regex argument and then use that regex. The problem is, if the user doesn't use single/double quotations then the regex will be evaluated before I need it to be evaluated.
Here's an example; let's say that the user executed the script and passed *f* as an argument, and the code has the following lines:
arg=$1 #*f*
echo "$arg"
In the above example echo returns a file name in the current directory that contains the regex *f*. I need echo to print *f*. It works if the user passes '*f*', but is there no other way?

How to convert a variable to string format? KSH shell scripting

I am trying to convert a variable to a string format so I can use java runsql utility to insert its value into a database later. The database needs the value to be in a char format, hence string.
This is a dumbed down version of my code so I can get to the heart of what I'm asking -
#!/bin/ksh -x
value1=2018-01-01
value2=2018-02-01
mystring=$value1,$value2
echo $mystring
stringify=${'value1'},${'value2'})
echo $stringify
What happens is I get no output for stringify or depending on how I switch up the arrangement of the symbols I get the literal string 'value1' or 'value2'.
What am I doing wrong? I feel like this is very simple, maybe I've just been staring at it too long, I dunno.
You can just escape quote like this:
mystring=\'$value1\',\'$value2\'
Output:
$ echo $mystring
'2018-01-01','2018-02-01'
A simpler option to get the same output (as suggested by #CharlesDuffy) is:
mystring="'$value1','$value2'"
You can just do like this, more simpler:
#!/bin/ksh -x
value1=2018-01-01
value2=2018-02-01
mystring=$value1,$value2
echo $mystring
stringify="'$value1','$value2'" #use double-quotes around the variables
echo $stringify
Output:
2018-01-01,2018-02-01
'2018-01-01','2018-02-01'

Including Variables in Strings, Bash Scripting

I need to insert variables into a string to create a URL. Right now, I'm looping over an array of values and inserting them into the string.
year="2015"
for i in "${array[#]}"
do
url="https://www.my-website.com/place/PdfLinkServlet?file=\place\\$i\099%2Bno-display\\$year_$i.pdf"
echo $url
done
The $i is being replaced with the corresponding array element, but $year just leaves a blank space. Can someone explain why and how to get a url that looks like: url="https://www.my-website.com/place/PdfLinkServlet?file=\place\place_id\099%2Bno-display\2015_place_id.pdf"
Because variable names can legally contain _ characters, there's no way for Bash to know that you wanted $year instead of $year_. To disambiguate, you can use enclose the variable name in brackets like this:
${year}_${i}.pdf
It's not bad practise to do this any time you are shoving variable expansions together. As you can see, it actually makes them stand out better to human eyes too.
Use ${var} instead:
year="2015"
for i in "${array[#]}"; do
url="https://www.my-website.com/place/PdfLinkServlet?file=place${i}099%2Bno-display${year}_${i}.pdf"
echo "$url"
done
Here is a little hack that also works well. And I use it for my projects frequently. Add the _ to the end of 2015 in the variable year
like year="2015_"
and remove the _ from the url variable and join the two variables i and year together like $year$i
I added an arbitrary array so that the script can run.
#!/bin/bash
year="2015_"
array=(web03 web04 web05 web06 web07)
for i in "${array[#]}";
do
url="https://www.my-website.com/place/PdfLinkServlet?file=\place\\$i\099%2Bno-display\\$year$i.pdf"
echo $url
done

variable substitution removing quotes

I seem to have some difficulty getting what I want to work. Basically, I have a series of variables that are assigned strings with some quotes and \ characters. I want to remove the quotes to embed them inside a json doc, since json hates quotes using python dump methods.
I figured it would be easy. Just determine how to remove the characters easy and then write a simple for loop for the variable substitution, well it didn't work that way.
Here is what I want to do.
There is a variable called "MESSAGE23", it contains the following "com.centrify.tokend.cac", I want to strip out the quotes, which to me is easy, a simple echo $opt | sed "s/\"//g". When I do this from the command line:
$> MESSAGE23="com."apple".cacng.tokend is present"
$> MESSAGE23=`echo $MESSAGE23 | sed "s/\"//g"`
$> com.apple.cacng.tokend is present
This works. I get the properly formatted string.
When I then try to throw this into a loop, all hell breaks loose.
for i to {1..25}; do
MESSAGE$i=`echo $MESSAGE$i | sed "s/\"//g"`
done
This doesn't work (either it throws a bunch of indexes out or nothing), and I'm pretty sure I just don't know enough about arg or eval or other bash substitution variables.
But basically I want to do this for another set of variables with the same problems, where I strip out the quotes and incidentally the "\" too.
Any help would be greatly appreciated.
You can't do that. You could make it work using eval, but that introduces another level of quoting you have to worry about. Is there some reason you can't use an array?
MESSAGE=("this is MESSAGE[0]" "this is MESSAGE[1]")
MESSAGE[2]="I can add more, too!"
for (( i=0; i<${#MESSAGE[#]}; ++i )); do
echo "${MESSAGE[i]}"
done
Otherwise you need something like this:
eval 'echo "$MESSAGE'"$i"'"'
and it just gets worse from there.
First, a couple of preliminary problems: MESSAGE23="com."apple".cacng.tokend is present" will not embed double-quotes in the variable value, use MESSAGE23="com.\"apple\".cacng.tokend is present" or MESSAGE23='com."apple".cacng.tokend is present' instead. Second, you should almost always put double-quotes around variable expansions (e.g. echo "$MESSAGE23") to prevent parsing oddities.
Now, the real problems: the shell doesn't allow variable substitution on the left side of an assignment (i.e. MESSAGE$i=something won't work). Fortunately, it does allow this in a declare statement, so you can use that instead. Also, when the sees $MESSAGE$i it replaces it will the value of $MESSAGE followed by the value of $i; for this you need to use indirect expansion (`${!metavariable}').
for i in {1..25}; do
varname="MESSAGE$i"
declare $varname="$(echo "${!varname}" | tr -d '"')"
done
(Note that I also used tr instead of sed, but that's just my personal preference.)
(Also, note that #Mark Reed's suggestion of an array is really the better way to do this sort of thing.)

Environment variable within variable

I have an environment variable called $TEST which refers to a directory
in my bash script I have a variable called $VARTEST which is $TEST/dir/file
now I want to grep the file specified by $VARTEST so I try to do:
grep somestring $VARTEST but it doesn't translate $TEST into it's directory
I've tried different combinations of {}, "" and '' but without success
I think you want
eval grep somestring "$VARTEST"
or even
VARTEST_EVALUATED=$(eval echo $VARTEST)
grep "$SOMESTRING" "$VARTEST_EVALUATED"
but remember (as others already said): If possible use
VARTEST="$TEST/foo/bar"
instead of
VARTEST='$TEST/foo/bar'
use the second one only if you really need kind of 'lazy evaluation'...
Warning, this could be dangerous if $VARTEST contains malicous code.
Have you put single quotes around something? Single quotes will prevent the variables from being translated into their corresponding values. Double quotes will work though. For example:
#!/bin/sh
TEST="/etc"
VARTEST="$TEST/passwd"
grep "$LOGNAME" "$VARTEST"

Resources