Sponsored Content
Top Forums Shell Programming and Scripting Variable substitution with arrays Post 303002465 by Don Cragun on Thursday 24th of August 2017 11:11:45 PM
Old 08-25-2017
I don't know if you can do what you're trying to do with bash, but you can do it with a 1993 or later version of ksh by using a name reference variable. You can't do it quite the way you were trying to do it because you can't currently create arrays of nameref variables, but the following seems to produce results similar to what I think you were trying to do:
Code:
#!/bin/ksh
animals=(dog cat horse penguin cow)
fruits=(orange apple grapes peach mango)
drinks=(juice milk coffee tea coke)
cities=(toronto paris london glasgow sydney)
countries=(canada france england scotland australia)
sparse[1]=first
sparse[10]=tenth
sparse[33]='thirty-third'
sparse[100]=hundredth

typeset -A associative
associative["a b"]='A B'
associative["x y z"]='X Y Z'
associative["SomeOtherString"]='Anything you might want'

arrays=(animals fruits drinks cities countries sparse associative)
typeset -n arrayname

for arrayname in "${arrays[@]}"
do	for subscript in "${!arrayname[@]}"
	do	printf '%s[%s]=%s\n' "${!arrayname}" "$subscript" "${arrayname[$subscript]}"
	done
done

which produces the output:
Code:
animals[0]=dog
animals[1]=cat
animals[2]=horse
animals[3]=penguin
animals[4]=cow
fruits[0]=orange
fruits[1]=apple
fruits[2]=grapes
fruits[3]=peach
fruits[4]=mango
drinks[0]=juice
drinks[1]=milk
drinks[2]=coffee
drinks[3]=tea
drinks[4]=coke
cities[0]=toronto
cities[1]=paris
cities[2]=london
cities[3]=glasgow
cities[4]=sydney
countries[0]=canada
countries[1]=france
countries[2]=england
countries[3]=scotland
countries[4]=australia
sparse[1]=first
sparse[10]=tenth
sparse[33]=thirty-third
sparse[100]=hundredth
associative[SomeOtherString]=Anything you might want
associative[a b]=A B
associative[x y z]=X Y Z

These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Substitution in a variable

Hey All, I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)
Discussion started by: spragueg
3 Replies

2. UNIX for Dummies Questions & Answers

variable substitution

Hi everyone, I have a simple question to ask : In a script that I'm writting, I need to create variables on-the-fly. For instance, for every iterartion of the following loop a var_X variable should be generated : #!/bin/ksh a="1 2 3" for i in $a do var_${i}=$i echo "${var_$i}" done ... (1 Reply)
Discussion started by: ck-18
1 Replies

3. Shell Programming and Scripting

Variable Substitution

I have run into a wall with my iptables firewall scripting. I am blocking all of the private side IP addresses on the WAN interface on systems running NAT. However, if the system is not running NAT and needs to allow access to the local LAN on the WAN interface, I need to block all but one of... (2 Replies)
Discussion started by: garak
2 Replies

4. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

5. UNIX for Dummies Questions & Answers

Variable substitution

Hi, That might be pretty simple. How can I generate a variable name and get their value ? Thanks a lot. Something like: >CUSTOMER_NF=26 > object=CUSTOMER > echo ${object}_NF CUSTOMER_NF > echo ${${object}_NF} ksh: ${${object}_NF}: 0403-011 The specified substitution is... (7 Replies)
Discussion started by: Leo_NN
7 Replies

6. Shell Programming and Scripting

variable substitution

file1.ksh #!/bin/ksh test5_create="I am a man" # test5 will be dynamic and the value will be passed from command line a=${1}_create echo $a # i need the output as "I am a man" ./file1.ksh test5 # i run the script like this any suggessions guys... (1 Reply)
Discussion started by: giri_luck
1 Replies

7. Shell Programming and Scripting

bash: combine arrays with weird substitution/references

Hi all. I'm trying to finish a bash script with the following elements: ARRAY="blah $ITEM blah blah" ARRAY="blah blah $ITEM blah bluh" #ARRAY="...." # ...the ARRAY elements represent a variable but defined # syntax and they're all hard-coded in the script. #(...) ITEMS='1.0 2.3... (2 Replies)
Discussion started by: yomaya
2 Replies

8. Shell Programming and Scripting

Variable Substitution

Hi , I have a variable as follows, Temp=`cat ABC.txt | cut -c5-` This will yeild a part of the date. say , 200912. I would like to substitute this variable's value in a filename. eg: File200912F.zip when i say File$TempF.zip , it is not substituting. Any help ? Thanks in... (2 Replies)
Discussion started by: mohanpadamata
2 Replies

9. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

10. Shell Programming and Scripting

Variable substitution

Hi, I have to write a shell script in which I have to substitute a variable within a variable. For example, var1=aaa var2=file.$var1.txt The output should be, echo $var2 file.aaa.txt Can someone please help me in getting this. I tried using eval, but it didnt work. I might be using it... (2 Replies)
Discussion started by: grajp002
2 Replies
COURIERUUCP(8)						      Double Precision, Inc.						    COURIERUUCP(8)

NAME
courieruucp - Sample Courier mail filter SYNOPSIS
makeuucpneighbors DESCRIPTION
The Courier mail server is capable of sending and receiving mail via UUCP. Courier does not implement UUCP directly, but instead uses third-party UUCP software, which you must install separately. Courier's UUCP support is provided automatically by the courieruucp module, which is compiled and installed by default. Courier sends mail via UUCP by issuing a request via uux to execute the rmail command on the remote UUCP node. Courier passes the message envelope sender to rmail via the -f flag, and lists the message envelope recipients as the remaining arguments. Message envelope addresses and E-mail addresses in the headers of the message will be appropriately rewritten. Courier will only send mail via UUCP if it knows about the recipient's UUCP node. The configuration file /etc/courier/uucpneighbors is a plain text configuration file. The makeuucpneighbors script compiles this configuration file into a binary database that is read by Courier. Changes to /etc/courier/uucpneighbors do not take effect until makeuucpneighbors is executed, and Courier is restarted. /etc/courier/uucpneighbors may also be a subdirectory instead of a plain text file, in which case the contents of all text files in this subdirectory is automatically concatenated by the makeuucpneighbors script. Contents of /etc/courier/uucpme This configuration file must be initialized to contain the UUCP node name that this machine is known to its neighbors. If /etc/courier/uucpme does not exist, Courier uses the first node name of the hostname specified by the /etc/courier/me configuration file. Contents of /etc/courier/uucpneighbors Note that in addition to creating this configuration file you must also configure your UUCP software to appropriately route UUCP mail to the specified nodes, which is a completely separate task. Blank lines in this file are ignored. Lines that start with the # character are comments, and are also ignored. The remaining lines are in the following format: node<TAB>flags node is a UUCP node, such as "apple" or "food!fruits!apple". <TAB> is a single tab character, and flags is zero or more of single character flags described below. The tab character can be ommited if there are no flags. If there is an entry for node in /etc/courier/uucpneighbors then Courier will accept mail for any address of the form "node!user", but not "node!nodule!user". In order to accept mail for "node!nodule!user" you must enter both "node" and "node!nodule", unless you use the F or the G flag. The flags are as follows: F Unrestricted forwarding. This flag will accept any UUCP path that starts with the specified node. Example: food<TAB>F This will accept "food!fruits!apple", "food!fruits!banana", "food!drinks!orangejuice", and so on. You may simply list all your immediate UUCP neighbors using the F flag, and accept mail to any address that's reachable via any neighbor. G This is a gateway. This flag is identical to the F flag, except that rmail is executed on the specified node. Normally, Courier will issue a uux request to run rmail on the last node, for example "uux food!fruits!rmail apple". Using G instead of F will run rmail on the listed node, for example "uux food!rmail fruits!apple". R This is a relay. This flag is used where neither F or G flags are appropriate, but you want to run rmail on this node. For example: food!fruits food<TAB>R In this example mail to "food!fruits!apple" is sent by executing "uux food!rmail fruits!apple". If the R flag is not specified, then Courier will request "uux food!fruits!rmail apple". Contents of /etc/courier/module.uucp This is the standard Courier module configuration file. MAXDELS Maximum simultaneous UUCP deliveries. This setting is a bit misleading. From Courier's viewpoint, a delivery consists only of running uux to queue up the message via UUCP. Once uux returns, the message is delivered, as far as Courier is concerned. UUCP is responsible for getting the message the rest of the way. Therefore, UUCP deliveries tend to be fairly quick, and it is not necessary to configure a large number of concurrent courieruucp processes, since each one will terminate quickly. Once uux terminates, the UUCP software is responsible for queueing and sending the message. MAXHOST Maximum simultaneous UUCP deliveries to the same UUCP node. Since UUCP deliveries tend to be fairly quick, it is not usually necessary to use MAXHOST to limit concurrent deliveries to the same UUCP node, so MAXHOST should generally be set to the same value as MAXDELS. MAXRCPT Maximum recipients per message. When the same message is sent to multiple recipients on the same UUCP node, Courier is capable of asking to run rmail only one, and listing all the recipients together. MAXRCPT specifies the upper limit on the number of recipients that are batched together. You want to set a limit based on any limitations on the length of command lines that's imposed by the operating system. Sending the same message to different UUCP nodes is always handled as separate deliveries. HEADER AND ADDRESS REWRITING
Courier is certainly capable of relaying mail between the Internet - via ESMTP - and UUCP-land, rewriting headers and addresses appropriately. The address rewriting rules are fixed, but they are expected to work in nearly every case. If you find yourself in some oddball situation, you'll just have to invest the time to add custom header rewriting code. If you are example.com, and your UUCP node is example, then <user@example.com> gets rewritten as example!user. Internet mail may be sent via ESMTP to your UUCP neighbor apple!john by sending it to <apple!john@example.com>. When that's forwarded via UUCP the return address of <someone@domain.com> will be rewritten as example!domain.com!someone. apple!john will get the mail with this return address, which should be repliable back to the sender. Courier's UUCP module will receive the UUCP address domain.com!someone, and translate it back as <someone@domain.com>. BUGS
Courier does not support defining multiple UUCP node names in /etc/courier/uucpme. SEE ALSO
esmtpd(8)[1], couriertcpd(8)[2], courier(8)[3]. AUTHOR
Sam Varshavchik Author NOTES
1. esmtpd(8) [set $man.base.url.for.relative.links]/esmtpd.html 2. couriertcpd(8) [set $man.base.url.for.relative.links]/couriertcpd.html 3. courier(8) [set $man.base.url.for.relative.links]/courier.html Courier Mail Server 04/04/2011 COURIERUUCP(8)
All times are GMT -4. The time now is 05:57 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy