Hi all,
How does string concatenation work in shell scripting?
I basically have a variable called "string" and I want to add the strings "aaa" "bbb" "ccc" "ddd" to the variable "string". These strings would be added based on some conditions and separated by spaces . So "string" might look like
string = aaa bbb ccc
Vgersh is very good at this type of problem and I don't want to but in, but most cases, in most shells, using an array to store the data and exploding the array is a simple way to form a concatenated string of disparate variables.
Short example with bash and gawk.
function retrandstr() {
gawk '
function retrandchars(str, sz,rval,i) {
sz = int(1 + rand() * length(str))
i =0
#print sz, i;
while (i < sz) {
rval = rval substr(str,int((1 + rand() * length(str))),1);
i++;
}
return rval
}
BEGIN {
srand();
print retrandchars("abndhcv784893ndyvurfgh3ery348infy7478")
}'
sleep 1
}
Test--
while test "$i" -gt 0; do arr[$i]=`retrandstr`; i=`expr $i - 1`; done
list=${arr[@]}
Vgersh, I have posted a sample in comment #1.
The only thing is that data values will be added to the "string" variable based on a condition. So for example string = aaa ccc ddd.
For example in the first loop "string" would contain no data then based on a condition "aaa" will be added to it. In the next loop if the condition satisfies "ccc" will be added, but "string" should contain "aaa" as well, so the end value would be "aaa ccc". Hope this helps.