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'
Related
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"
I have a variable with multiple values seperated by ':' , how can I get those fetched seperately to receive values of abc, cde story, bjd in a bash script?
abc: 10
cde story: 123abc
bjd: I have some values
I am a little confused about your question. Are these variables stored in a separate file, like 'vars.txt'? It would be very helpful for you to provide some more context.
That said, check out https://linuxhint.com/bash_split_examples/ for some examples of how to split a string in bash. The TL;DR is this:
If you have a bash variable:
text="Hello:World"
and you want to split it by a delimiter, ":", you must use the IFS variable.
IFS=":"
This seems to be a special bash variable that affects how the read command works.
So for you, it might look something like this:
some_var="10:123abc:i have some values"
IFS=":"
read -a var_array <<< "$some_var"
abc=${var_array[0]}
cde_story=${var_array[1]}
bjd=${var_array[2]}
echo "$abc, $cde_story, $bjd"
The output of which is:
10, 123abc, i have some values
I have a bash string which looks like
TEST="tags/1.2.3-abc"
from which I want to extract the part after the slash, i.e. "1.2.3-abc" if and only if the string starts with the phrase "tags/". If the latter is not the case, I want to the string as it is.
Examples:
Input: tags/1.2.3-abc Output: 1.2.3-abc
Input: 1.2.3-abc Output: 1.2.3-abc
Input: trunk Output: trunk
Sounds like a job for the substitution operators in Bash.
Try something like:
TEST="tags/1.2.3-abc"
echo ${TEST#tags/}
You can read more about this syntax here: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
Beside cut command you could also use bash string manipulation for your problem
TEST='tags/1.2.3-abc';
if test ${TEST#tags/*}; #Test that $TEST contains "tags/*" expression
then
TEST=${TEST:5}; #Re-assign to $TEST the substring after the "tags/"
fi
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.)
the problem I met is like this:
given a string dimensioning-inspection_depth-0rules_20120306-084158
I'd like to get the first part of the string: dimensioning-inspection_depth-0rules, so to speak, get rid of the time stamp part. In perl, it's like a piece of work to do the job using regular expression. But since I'm new to bash, I'd like to know
the best-practice way of doing it in Bash.
If you don't want to pipe the string to sed, you can use Bash variable mangling:
$ string=dimensioning-inspection_depth-0rules_20120306-084158
$ echo ${string%_*}
dimensioning-inspection_depth-0rules
More information on variable mangling here.
You can do this:
str=dimensioning-inspection_depth-0rules_20120306-084158
echo ${str%_*}
This will remove anything after and including the last _ in the string, so if your string always has the form something_date-time, you will always be left with something part.
More info here:
http://tldp.org/LDP/abs/html/string-manipulation.html
a="dimensioning-inspection_depth-0rules_20120306-084158"
echo $a | head -c -16
So as the timestamp will always be equal width (16 characters that you are trying to trim off the end in this case), head -c -16 should work for you