Sponsored Content
Top Forums Shell Programming and Scripting dynamic variable value assignmnet Post 302542516 by animesharma on Thursday 28th of July 2011 12:44:48 AM
Old 07-28-2011
dynamic variable value assignmnet

QUERY IN BRIEF
Listing the query in short
Code:
#! /bin/csh -f
#say i have invoked the script with two arguments : a1 and 2
set arg = $1 # that means arg = a1
echo "$arg" #it prints a1
#now what i want is:
echo "$a1" 
#it will give error message :a1 undefined.
#however what i need is that the echo statement with a1 should print 2 (=$2)

QUERY IN DETAIL
consider the script code below

script name: dyn_par
Code:
#!/bin/csh -f
awk '{for(i=1;i<=NF;i++)printf("%s=%s\n",$i,$i)}' script_name.txt    
#the awk line reads the script and prints 
#script1=script1
#a1=a1
#

while ( $1 != "" )
  set arg = $1                   #That is arg = a1
  echo " arg ==== $arg "
  shift
  # I need to have a1 = $1
  echo "a1 = $a1" # should print value at  $2
  shift
end

content of script_name.txt is
Code:
script1 a1

at terminal, the script is invoked as below:

Code:
$ ./dyn_par a1 2

What i am trying to do is, to create the variable "a1" dynamically and assigning it a value a1= $2 =(which is here 2).
Rather than directly declaring the variable in dyn_par script, i wish to
1) dynamically declare variables using the data in script_name.txt file (so that any entries made to script_name file will automatically declare the variables)
2) use arguments ,($1) to identify the variable a1 and $2 to store the value(=2 here) to it.

Last edited by animesharma; 07-28-2011 at 02:37 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic Variable Declatation

Evening all, Have been trying to create the following environment variable: ${MD_SYSTEM}_ZZ_EMAIL_SUPPORT="myname@domain.com" However when the script that contains the above is executed it returns: ksh: MDQA_ZZ_EMAIL_SUPPORT=myname@domain.com: not found Is what I'm trying to do... (2 Replies)
Discussion started by: Cameron
2 Replies

2. UNIX for Dummies Questions & Answers

Dynamic variable values

Bit of a newbie :D with regard to unix scripting and need some advice. Hopefully someone can help with the following: I have a predefined set of variables as follows: AAA_IP_ADD=1.1.1.1 BBB_IP_ADD=2.2.2.2 I have a funnction call which retrieves a value into $SUPPLIER which would be... (3 Replies)
Discussion started by: ronnie_uk
3 Replies

3. Shell Programming and Scripting

Dynamic variable assignment

Hi All, I have the below scenario: A file test.cfg with three fields>> DATA1 DATA2 DATA3 In the script I need to assign each of the fields to variables. The number of fields will not be constant (this case we have three). Im trying to do something like this: NUM=1 OUT_DAT_NO=3 ... (4 Replies)
Discussion started by: deepakgang
4 Replies

4. Shell Programming and Scripting

dynamic variable name

I found one post in another site with a solution for my problem the below solution should explain what I want. #!/bin/sh first="one" second="two" third="three" myvar="first" echo ${!myvar} But this gives error 'bad substitution' System info SunOS sundev2 5.9... (3 Replies)
Discussion started by: johnbach
3 Replies

5. Shell Programming and Scripting

Dynamic variable assignment

Hi all, I’m very new to UNIX programming. I have a question on dynamic variable 1. I’m having delimited file (only one row). First of all, I want to count number of columns based on delimiter. Then I want to create number of variables equal to number of fields. Say number of... (5 Replies)
Discussion started by: mkarthykeyan
5 Replies

6. Shell Programming and Scripting

Help with Dynamic variable

I need some variable help TEMP1=Jane TEMP2=Sue X=1 eval USER=TEMP${X} echo $USER This gives output USER1 I would like to get Jane I have tried eval USER='TEMP${X}' eval USER="TEMP${X}" eval USER=`TEMP${X}` (3 Replies)
Discussion started by: Jotne
3 Replies

7. Shell Programming and Scripting

Dynamic file name in variable

Hi guys. i have a requirment as below. I have a scripts which perform for loop for i in /backup/logs -- it will give all the logs file SC_RIO_RWVM_20120413064217303.LOG SC_RIO_RWXM_20120413064225493.LOG SC_RIO_RXXM_20120413064233273.LOG ... do open script.sh ---- in this file... (3 Replies)
Discussion started by: guddu_12
3 Replies

8. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

9. Shell Programming and Scripting

Dynamic variable assignment

My Code : -------------------------------------------- #!/bin/bash for i in `echo server1 server2` do eval ${i}_name = "apache" echo ${i}_name done -------------------------------------------- Current output : >./test.sh ./test.sh: line 5: server1_name: command not found... (3 Replies)
Discussion started by: sameermohite
3 Replies

10. Shell Programming and Scripting

Dynamic variable name in bash

Hello, I'm trying to build a small script to store a command into a dynamic variable, and I have trouble assigning the variable. #!/bin/bash declare -a var1array=("value1" "value2" "value3") var1arraylength=${#var1array} for (( i=1; i<${var1arraylength}+1; i++ )); do mkdir... (7 Replies)
Discussion started by: alex2005
7 Replies
set_color(1)							       fish							      set_color(1)

NAME
set_color - set_color - set the terminal color set_color - set the terminal color Synopsis set_color [-v --version] [-h --help] [-b --background COLOR] [COLOR] Description Change the foreground and/or background color of the terminal. COLOR is one of black, red, green, brown, yellow, blue, magenta, purple, cyan, white and normal. o -b, --background Set the background color o -c, --print-colors Prints a list of all valid color names o -h, --help Display help message and exit o -o, --bold Set bold or extra bright mode o -u, --underline Set underlined mode o -v, --version Display version and exit Calling set_color normal will set the terminal color to whatever is the default color of the terminal. Some terminals use the --bold escape sequence to switch to a brighter color set. On such terminals, set_color white will result in a grey font color, while set_color --bold white will result in a white font color. Not all terminal emulators support all these features. This is not a bug in set_color but a missing feature in the terminal emulator. set_color uses the terminfo database to look up how to change terminal colors on whatever terminal is in use. Some systems have old and incomplete terminfo databases, and may lack color information for terminals that support it. Download and install the latest version of ncurses and recompile fish against it in order to fix this issue. Version 1.23.1 Sun Jan 8 2012 set_color(1)
All times are GMT -4. The time now is 04:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy