Sponsored Content
Top Forums Shell Programming and Scripting Variable within variable expansion Post 303044728 by hburnswell on Monday 2nd of March 2020 04:15:37 PM
Old 03-02-2020
Variable within variable expansion

Hi,

I have been trying to get variable within variable expansion to work but have not been able to succeed. It seems that the two suggestions I am seeing are using 'eval' and using the built in bash capability of "${!test}". Here is a snippet of what I"m trying to accomplish:

Code:
#!/bin/bash
b2b_qa_site=1234
b2c_qa_site=4321
b2b_prd_site=5678
b2c_prd_site=8765

grp=`echo "${1: -2:1}"`
env=`echo "${1: -3:1}"`


if [[ $env -eq 4 ]]; then

        env=prd

else

        env=qa

fi

if [[ $grp -eq 0 ]]; then

        grp=b2b

else

        grp=b2c

fi

test=${grp}_${env}_site
eval echo \$test

This would be used as:

Code:
# ./script box400

And ideally receive:

Code:
5678

Can anyone shed some light on what I am doing wrong here? I appreciate any guidance.

Thanks,
HB
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

ksh on HP-UX -- variable expansion

We have a script that runs in ksh on HP-UX 11.11. It takes three arguments. The last argument can be a filename or wildcard character. For example: script -s hello -t goodbye '*.d*' In a case such as this, I would wrap single quotes around the final argument because I dont want the shell to... (4 Replies)
Discussion started by: dangral
4 Replies

2. Shell Programming and Scripting

Basic variable expansion not working...

#!/usr/bin/bash if then echo "Not valid arguments entered. Just username should be entered." else USER_NAME=$1 FILE_NAME=$USER_NAME.info UNN=STUDIN\\\\$1 echo $UNN last STUDIN\\\\$1 last UNN If I type `last STUDIN\\eip060` it works but if I try to expand it with variable it is... (5 Replies)
Discussion started by: Zammy_bg
5 Replies

3. Shell Programming and Scripting

bash - delay expansion of variable

Hello - I have a bash script which does some logging, and I'd like to include the line number of the echo statement that pipes into $LOGGER: MYPID=$$ MYNAME=`basename $0` LOGGER="/usr/bin/logger -t $MYNAME($LINENO) -p daemon.error" ... echo 'this is an entry into the log file' | $LOGGER ... (3 Replies)
Discussion started by: scandora
3 Replies

4. UNIX for Dummies Questions & Answers

Variable brace expansion

I'm in the habit of using the following type of loop structure: for num in `seq $1 $2` do command doneWhile `seq $1 $2` is not exactly a huge resource hog, I would like to learn a better way. It seems that brace expansion is a good way to go: for num in {3..10}The problem, though, is... (2 Replies)
Discussion started by: treesloth
2 Replies

5. Shell Programming and Scripting

Bash variable delayed expansion?

i write a batch file , here is the content. dirname='date +%Y-%m-%d' mkdir dirname but it doen's work, it just create a folder named date and +%Y-%m-%d. i have tried run the command seperately in the bash prompt. after the first sentence executed , i use $dirname to watch the value of... (4 Replies)
Discussion started by: premotheus
4 Replies

6. Shell Programming and Scripting

Variable expansion in sed

The objective of the code below is to create sed script to be later executed. However, it bonks because $ARCHIVENAME expands to a directory specification so the forward slashes cause problems. I can think of a few solutions that would involve redesigning the process, but I'm hoping there might be... (4 Replies)
Discussion started by: tiggyboo
4 Replies

7. Shell Programming and Scripting

delay variable expansion

Hi forum, in my bash script I've many lines executing commands with redirection to log files. ... xyz_cmd 2>&1 > $BASENAME.$LINENO The trailing part of these lines doesn't look nice and I like to put it into a variable. The (not working) idea is something like that ... (3 Replies)
Discussion started by: wolfi089
3 Replies

8. UNIX for Dummies Questions & Answers

Can I use a variable with brace expansion?

So, I was bored on the train today, and was thinking of ways to loop through elements of an array. I came up with the following simple script, but it doesn't work as brace expansion doesn't seem to work with variables. Is there something I'm missing, or does the shell just not work like this? ... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

9. Shell Programming and Scripting

Help required regarding variable expansion in UNIX

Hello, I have one variable coming from one file: abc=$xyz/filename.txt where $xyz is defined in .profile file as say, /usr/dev/src i am passing abc variable to one perl script as input parameter. perl 123.pl -s $abc But inside the perl script execution, the variable $abc is just... (1 Reply)
Discussion started by: vikas_trl
1 Replies

10. Shell Programming and Scripting

Bash variable expansion

Hello. The file /etc/fstab contains UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4 noatime,discard,acl,user_xattr 1 1 I fill a variable SOME_LINE=$( cat /etc/fstab | grep \/\..*ext4 | grep noatime,discard )echo $SOME_LINE... (3 Replies)
Discussion started by: jcdole
3 Replies
<grp.h>(P)						     POSIX Programmer's Manual							<grp.h>(P)

NAME
grp.h - group structure SYNOPSIS
#include <grp.h> DESCRIPTION
The <grp.h> header shall declare the structure group which shall include the following members: char *gr_name The name of the group. gid_t gr_gid Numerical group ID. char **gr_mem Pointer to a null-terminated array of character pointers to member names. The gid_t type shall be defined as described in <sys/types.h> . The following shall be declared as functions and may also be defined as macros. Function prototypes shall be provided. struct group *getgrgid(gid_t); struct group *getgrnam(const char *); int getgrgid_r(gid_t, struct group *, char *, size_t, struct group **); int getgrnam_r(const char *, struct group *, char *, size_t , struct group **); struct group *getgrent(void); void endgrent(void); void setgrent(void); The following sections are informative. APPLICATION USAGE
None. RATIONALE
None. FUTURE DIRECTIONS
None. SEE ALSO
<sys/types.h> , the System Interfaces volume of IEEE Std 1003.1-2001, endgrent(), getgrgid(), getgrnam() COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 <grp.h>(P)
All times are GMT -4. The time now is 07:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy