[Solved] Assigning Shell variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Assigning Shell variable
# 1  
Old 03-10-2014
[Solved] Assigning Shell variable

Hello,

Need a small help to execute below script.

Code:
#!/bin/bash
. new.txt
for no in 3 4
do
echo $((uname_$no))
done

new.txt contains
Code:
uname_1="XXXXXX"
uname_2="YYYYY"
uname_3="ZZZZZ"
.........

I am getting errors if i run the above script. I doubt the syntax is wrong in "echo $((uname_$no))" line. Correct me if i am wrong.
# 2  
Old 03-10-2014
Hello,

Following may help.

Either use the following.
Code:
uname_$no=echo $no

OR try following.
Code:
echo "uname_"$no

Please let us know if you have any queries for same.


Thanks,
R. Singh
# 3  
Old 03-10-2014
That is the wrong syntax. $(( .. )) is used for arithmetic expansion. Try:
Code:
eval echo "\"\$uname_$no\""

If you have control over the source file format, since you are using bash, you could use arrays instead:
Code:
uname[1]="XXXXXX"
uname[2]="YYYYY"
uname[3]="ZZZZZ"

and
Code:
echo "${uname[no]}"


Last edited by Scrutinizer; 03-10-2014 at 06:55 AM..
# 4  
Old 03-10-2014
This will work in modern versions of Bash:
Code:
#!/bin/bash

. ./new.txt

for no in 1 2 3
do
    ref="uname_$no"
    echo ${!ref}
done

nameref provides similar functionality in ksh93
These 2 Users Gave Thanks to fpmurphy For This Post:
# 5  
Old 03-10-2014
Hello Ravinder,

Thanks for your reply.

Code:
echo "uname_"$no

By using your above code, it just displays 2 or 3 ($no) not the actual one which needs to be displayed.


Expected output:
YYYYY
ZZZZZ

My objective is to display the values present in new.txt file.
# 6  
Old 03-10-2014
Hello,

Could you please try the following.

Code:
for no in  3 4
do
echo "uname_"$no
done < "check_out"


output will be as follow.

Code:
uname_3
uname_4


NOTE: Where check_out is the input file as same provided by you.


Thanks.
R. Singh
# 7  
Old 03-10-2014
Thanks fpmurphy, i tried your code and it is working fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Assigning an expression to a variable in shell script

i am trying to assign the following expression to a variable in Unix shell script and want to use that variable in some other expression. But unable to get the required thing done. Please help with this.... This is the expression which i want to provide as input the variable date '+%y:%m:%d' |... (3 Replies)
Discussion started by: ssk250
3 Replies

2. Shell Programming and Scripting

[Solved] Assigning a value to a variable name then running a loop on these values

Hi, I was wondering if anyone could assist me for (what is probably) a very straightforward answer. I have input files containing something like File 1 Apples Apples Apples Apples File 2 Bananas Bananas Bananas Bananas (4 Replies)
Discussion started by: hubleo
4 Replies

3. Shell Programming and Scripting

assigning fields variables value to shell variable

suppose I have a file named abc.txt.The contents of the file is sited below abc.txt maitree,test,test3 Using awk command can I store these 3 values in 3 different variable and in one single line command of awk. suppose variable a b c is there. I don't want like this a=`awk -F"," '{print... (2 Replies)
Discussion started by: maitree
2 Replies

4. Shell Programming and Scripting

Assigning return value of an embedded SQL in a shell script variable

I've a script of the following form calling a simple sql that counts the no of rows as based on some conditions. I want the count returned by the sql to get assigned to the variable sql_ret_val1. However I'm finding that this var is always getting assigned a value of 0. I have verified by executing... (1 Reply)
Discussion started by: MxC
1 Replies

5. Shell Programming and Scripting

Assigning value of SQL output to a variable in shell scripting

I am trying to assgn the output of the select statement to a variable, like this "VARIABLE_NAME=$ db2 "select COLUMN_NAME_1 from TABLE_NAME where COLUMN_NAME_2='VALUE_TO_CHECK'"; " but the value that is getting into VARIABLE_NAME is "COLUMN_NAME_1 ----------------- VALUE 1... (3 Replies)
Discussion started by: sgmini
3 Replies

6. Shell Programming and Scripting

Assigning variable from command outputs to shell

First, this is bash (3.2.17), on a Mac, 10.5.7. What I'm trying to do is look at a list of users, and check to see if each exists. If they do, do some more stuff, if they don't, drop them into an error file. So, my user list is: foo - exists bar - does not exist blah - does not exist ... (2 Replies)
Discussion started by: staze
2 Replies

7. Shell Programming and Scripting

Assigning output of command to a variable in shell

hi, I want to assign find command result into some temporary variable: jarPath= find /opt/lotus/notes/ -name $jarFile cho "the jar path $jarPath" where jarPath is temporary variable. Can anybody help on this. Thanks in advance ----Sankar (6 Replies)
Discussion started by: sankar reddy
6 Replies

8. Shell Programming and Scripting

assigning nawk output to shell variable

Hello friends, I doing the follwing script , but found problem to store it to a shell variable. #! /bin/sh for temp in `find ./dat/vector/ -name '*.file'` do echo $temp nawk -v temp=$temp 'BEGIN{ split(temp, a,"\/"); print a}' done output: ./dat/vector/drf_all_002.file... (6 Replies)
Discussion started by: user_prady
6 Replies

9. Shell Programming and Scripting

assigning command output to a shell variable

I have the sql file cde.sql with the below contents: abcdefghij abcwhendefothers sdfghj when no one else when others wwhen%others exception when others Now I want to search for the strings containing when others together and ceck whether that does not occur more than once in the... (2 Replies)
Discussion started by: kprattip
2 Replies

10. Shell Programming and Scripting

Assigning a shell variable as a float

I'm confused as to how to handle floating point numbers in shell scripts. Is there a way to convert a number (string) read into a shell variable so that it can be used as a floating point decimal for calculation purposes? Or am I stuck with integrating C or Perl into my script? Ex: --input ... (3 Replies)
Discussion started by: spieterman
3 Replies
Login or Register to Ask a Question