LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-17-2019, 02:10 AM   #1
azheruddin
Member
 
Registered: Dec 2011
Posts: 91
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 01-17-2019, 03:12 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.
 
Old 01-17-2019, 03:19 AM   #3
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
^ 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

Last edited by l0f4r0; 01-17-2019 at 03:49 AM.
 
Old 01-17-2019, 07:40 PM   #4
azheruddin
Member
 
Registered: Dec 2011
Posts: 91

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
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 ''
 
Old 01-17-2019, 07:55 PM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by azheruddin View Post
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

Last edited by berndbausch; 01-17-2019 at 08:05 PM.
 
Old 01-18-2019, 01:58 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Since the OP seems to be using Bash, maybe an array would be more useful.
 
Old 01-18-2019, 06:55 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by azheruddin View Post
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i am not able to concatenate both the string in shell script as below string spatil20 Linux - Newbie 16 04-24-2016 02:59 AM
[SOLVED] Trying to concatenate two variables and a slash ($var1/$var2). jbulgier Linux - General 2 12-13-2011 10:52 PM
Concatenate two variables with underscore - question laki47 Linux - Newbie 3 06-04-2010 05:02 AM
Bash script: concatenate variables with period character the182guy Linux - Software 4 12-28-2009 01:41 PM
How to concatenate two strings into one string in B-shell? jimmyjiang Red Hat 5 01-08-2008 01:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration