LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to concatenate two string variables (alphanumeric) (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-concatenate-two-string-variables-alphanumeric-4175646371/)

azheruddin 01-17-2019 02:10 AM

how to concatenate two string variables (alphanumeric)
 
hello,

below my piece of code initializing two queue names variables
when iam running for loop with counter varibale it is not giving the value of queue1 and queue2 to my command.

how to solve this?

queue1=ABCD
queue2=PQRS

for i in {1..2}
do
echo "DISPLAY Q(*)"| runmqsc $queue$i

done

Turbocapitalist 01-17-2019 03:12 AM

The pipe sends data via stdin. So if your utility is expecting to read that, then the pipe is the way to go. Otherwise, xargs is the way to go.

About the variables, if what you are aiming for is an indirect reference where the name of a variable is held in a second variable and the value accessed via that, then :

Code:

#!/bin/sh

queue1=ABCD
queue2=PQRS

for i in $(seq 1 2)
do
        a=$(eval echo -n \$queue$i)
        echo "DISPLAY Q(*) " | echo runmqsc $a
done


Also [code] [/code] will help keep your script readable.

l0f4r0 01-17-2019 03:19 AM

^ Interesting but I prefer ${!} notation.
So I would say:
Code:

queue1=ABCD
queue2=PQRS

for i in queue{1,2}
do
  echo "DISPLAY Q(*)" | runmqsc "${!i}"
done


azheruddin 01-17-2019 07:40 PM

provided both feedback not working .

below script used
Code:

queue1=ABCD
queue2=PQRS

for i in queue{1,2}
do
  echo "DISPLAY Q(*)" | runmqsc "${!i}"
done

when i ran my full script and check with sh -x actually to runmqsc is passing null.
when script run its take single quote but no value inside .
Code:

+ runmqsc ''

berndbausch 01-17-2019 07:55 PM

Quote:

Originally Posted by azheruddin (Post 5950085)
Code:

queue1=ABCD
queue2=PQRS

for i in {1..2}
do
        echo "DISPLAY Q(*)"| runmqsc $queue$i

done


When you run this for loop, you will call runmqsc twice. Once with a parameter ${queue}1, a second time with ${queue}2. If queue is not initialized, the result is runmqsc 1 followed by runmqsc 2.

To concatenate the variables:
Code:

runmqsc $queue1$queue2
If, on the other hand, you want to call runmqsc twice, with queue1 and queue2, respectively, and you absolutely need to do this in a loop, use arrays.
Code:

queue[1]=abcd
queue[2]=efgh
for i in {1..2}
do runmqsc ${queue[i]}
done


Turbocapitalist 01-18-2019 01:58 AM

Since the OP seems to be using Bash, maybe an array would be more useful.

TB0ne 01-18-2019 06:55 AM

Quote:

Originally Posted by azheruddin (Post 5950366)
provided both feedback not working .
below script used
Code:

queue1=ABCD
queue2=PQRS

for i in queue{1,2}
do
  echo "DISPLAY Q(*)" | runmqsc "${!i}"
done

when i ran my full script and check with sh -x actually to runmqsc is passing null. when script run its take single quote but no value inside .
Code:

+ runmqsc ''

This seems to be a pattern:
https://www.linuxquestions.org/quest...2/#post5655038

...where you post (when asked) a small piece of a 'script', get a few suggestions, then come back asking folks to finish it for you. How about this: you take the advice you've been given thus far, put it to use in a script, and post that script back here if it doesn't work?


All times are GMT -5. The time now is 01:05 PM.