Visit Our UNIX and Linux User Community


Trouble with variable assignment and reading in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble with variable assignment and reading in shell scripting
# 1  
Old 05-16-2013
Trouble with variable assignment and reading in shell scripting

Hi,
I have an inputfile with some table names in it.
For ex:
Code:
t_arnge
t_profl
t_fac

I need a script which reads the line one by one and need to assign to some dynamic variable. If to explain the above example:

i need some
Code:
a=table_name1
table_name1=t_arnge

in first time and
Code:
a=table_name2
table_name2=t_profl

while running second time and
Code:
a=table_name3
table_name3=t_fac

.


can any one please help me in this matter. It's a kind of urgent. I am using shell scripting.

Last edited by Scrutinizer; 05-16-2013 at 02:54 PM.. Reason: code tags
# 2  
Old 05-16-2013
Using array is the best option for this requirement.

You could code something like below:
Code:
#!/bin/bash

typeset -a table_name
typeset -i c

while read tn
do
        (( ++c ))
        table_name[c]="${tn}"
done < inputfile

Now you have each table names stored in the indexed array.

For further reference refer: Bash Array (link removed)
This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-16-2013
Few questions:
I need table_name[c] value in another variable. for ex:a
SO, if i echo $a , it should give table_name1
if i echo $table_name1 it should give the first table name in the i/p file.



Can't we do it in simple scripting? i mean using loops?
# 4  
Old 05-16-2013
Quote:
Originally Posted by Ravindra Swan
Few questions:
I need table_name[c] value in another variable. for ex:a
SO, if i echo $a , it should give table_name1
Once you have the values stored in indexed array, then why you want them in a different variable?

By the way you can visit each array element using a for loop and assign it to another variable if required:
Code:
for tn in ${table_name[@]}
do
        echo "$tn"
        # you can assign a="$tn" here
done

# 5  
Old 05-16-2013
Quote:
Originally Posted by Ravindra Swan
Can't we do it in simple scripting? i mean using loops?
We get asked for dynamic variable names many times a week, but these requests remain ill-considered... How could you even tell how many you had when you were done? How would you use it? And other such questions. It's extremely awkward, error-prone, and insecure in more ways than I can describe in a short post.

In short -- variables don't work that way. Not in shell, not in any other language that springs to mind. Either arrays or simple strings more than suffice for the purpose, that being what they were meant to do.

Last edited by Corona688; 05-16-2013 at 02:56 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #333
Difficulty: Easy
Linux was first developed as an alternative to Windows XP.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trouble with variable and command assignment

I have a section of a script where I want to check a log file for a certain oracle error and if there is only one error (and it is ORA-39152) then I want to email that script is complete. Otherwise email failure and log. Somehow with this while the log only has one error and it is ORA-39152, I... (5 Replies)
Discussion started by: cougartrace
5 Replies

2. Shell Programming and Scripting

Error in reading variable in shell script

Hello all, I have small script: # SCRIPT COMMONFILEPATH=$WORKDIR/samples/... (4 Replies)
Discussion started by: emily
4 Replies

3. Shell Programming and Scripting

Trouble understanding shell scripting (mostly wsname)

Am still learning Scripting and I come across a build command that I don't really understand if /local/bin/wsname 2>/dev/null; then base="`/local/bin/wsname`" export base fi if ; then /local/bin/wsname exit 1 fi WSNAME="$base"/ can some one in light me to what... (1 Reply)
Discussion started by: Wpgn
1 Replies

4. Shell Programming and Scripting

Trouble reading content of file from a variable

Hi , i have a parameter which has path of a file. Now i need to have another parameter with the content of that file. I tried the belwo script , can any one please help. I dont want to use cat command to read. Can we do it with out using cat command. while read line do... (9 Replies)
Discussion started by: Ravindra Swan
9 Replies

5. Shell Programming and Scripting

Reading a file and sending mail by shell scripting?

hi I need help urgently...i need to write a shell script which can solve the following problem....its urgent plz help me out coz m totally newbie in shell scripting.... the problem is: Suppose I have a folder called logs. whenever some error occurs some correspondence error file is generated. I... (4 Replies)
Discussion started by: sukhdip
4 Replies

6. Shell Programming and Scripting

Shell Scripting Reading List

Hello Everyone, Over the last few months I have begun to expand my programing skills from windows, Java and SQL / PL-SQL programing into the wonderful world of shell scripting. With little training budget my only options for training are books, Internet and this site (BTY... (1 Reply)
Discussion started by: caddis
1 Replies

7. Shell Programming and Scripting

Reading multi lines variable using shell script

Hi, I am using #!/bin/sh shell. I have a variable which contains multi line data. I want to read that variable line by line. Can someone post the code to read the multi line variable line by line? Any help is much appreciated. (2 Replies)
Discussion started by: gupt_ash
2 Replies

8. Shell Programming and Scripting

reading fixed length flat file and calling java code using shell scripting

I am new to shell scripting and I have to to the following I have a flat file with storename(lenth 20) , emailaddress(lenth 40), location(15). There is NO delimiters in that file. Like the following str00001.txt StoreName emailaddress location... (3 Replies)
Discussion started by: willywilly
3 Replies

9. Shell Programming and Scripting

Reading a path (including ref to shell variable) from file

Hi! 1. I have a parameter file containing path to log files. For this example both paths are the same, one is stated directly and the second using env variables. /oracle/admin/orcl/bdump/:atlas:trc:N ${ORACLE_BASE}/admin/${ORACLE_SID}/bdump/:${ORACLE_SID}:trc:N 2. I try to parse the path... (1 Reply)
Discussion started by: lojzev
1 Replies

10. Shell Programming and Scripting

variable assignment in new shell

Hi, I'm writing a KSH script, and at one point, I have to call a new shell and perform some variable assignments. I noticed that the assignment is not working. Please see two samples below: Command 1: #>ksh "i=2;echo I is $i" Output: #>I is Command 2: #>ksh <<EOF > i=2 > echo I... (7 Replies)
Discussion started by: maverick80
7 Replies

Featured Tech Videos