Bash variable recursion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash variable recursion
# 1  
Old 08-08-2013
Bash variable recursion

Not sure how to ask this question. I want concatenate strings and variable recursively into new variable. For example:
Code:
infile01=/dir/subfolder/file01.txt
infile02=/dir/subfolder/file02.txt
infile03=/dir/subfolder/file03.txt
for i in {01..03}
do
u=${"infile"$i}
echo $u
done

I got error message:
Code:
-bash: ${"infile"$i}: bad substitution

How do I get output?
Code:
/dir/subfolder/file01.txt
/dir/subfolder/file02.txt
/dir/subfolder/file03.txt

Thanks a lot!
# 2  
Old 08-08-2013
Whatever you are trying to do is a bad programming practice.

I would highly recommend using arrays instead which is pretty much straightforward:
Code:
infile[1]="/dir/subfolder/file01.txt"
infile[2]="/dir/subfolder/file02.txt"
infile[3]="/dir/subfolder/file03.txt"

for i in {1..3}
do
        echo "${infile[$i]}"
done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 08-08-2013
Or, I tried:
Code:
for i in {01..03}
do
u=$"infile"$i
echo $u
done

and got
Code:
infile01
infile02
infile03

but I was expecting
Code:
/dir/subfolder/file01.txt
/dir/subfolder/file02.txt
/dir/subfolder/file03.txt

---------- Post updated at 05:58 PM ---------- Previous update was at 05:55 PM ----------

Thanks Yoda!
I should have thought of that way.
# 4  
Old 08-08-2013
You can use eval to acheive it, but using it is a security risk and hence a bad programming practice.
Code:
infile01=/dir/subfolder/file01.txt
infile02=/dir/subfolder/file02.txt
infile03=/dir/subfolder/file03.txt
for i in {01..03}
do
        u=$( printf "infile%02d" "$i" )
        eval echo \$"$u"
done

# 5  
Old 08-08-2013
You need to use eval....here is a simple example
Code:
[root@localhost cgi-bin]# x1="foo"
[root@localhost cgi-bin]# n=1
[root@localhost cgi-bin]# eval echo \${x$n}
foo

This User Gave Thanks to blackrageous For This Post:
# 6  
Old 08-08-2013
Quote:
Originally Posted by Yoda
You can use eval to acheive it, but using it is a security risk and hence a bad programming practice.
A little perspective is in order.

Would you pass untrusted data to find . -exec $untrusted_data \;? Or rm $untrusted_data?

Simply using eval is not a security risk nor is it bad practice.

Trusting untrustworthy data is the real problem and it is bad practice regardless of the tool involved.

Regards,
Alister
# 7  
Old 08-08-2013
Hi Yoda!
Another question not about the concatenation but related to the array index when it is "08". The problem is with 8 & 9 that caused problem as
Code:
 "bash: 08: value too great for base (error token is "08"). "

I am aware this is related to octal for 01~07 but not 08~09.. etc. I want this leading 0 for 1~9 for the filenames to be nicely aligned. What is the trick to include a leading zero to the number as array index, if any? Thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Recursion in a bash script

So the problem I am having is recursion with in bash. Specifically the program ideally takes either the current or provided directory, lists out everything in that directory, if it finds another directory the script should call itself and go into that directory. I managed to get it to dive into the... (6 Replies)
Discussion started by: mistsong1
6 Replies

2. Shell Programming and Scripting

script recursion

Can someone please explain me why the following script calls it self recursively: #!/bin/bash echo Called $0 while this not: #!/bin/bash echo Called $($0) Thanks (6 Replies)
Discussion started by: superpointer
6 Replies

3. Programming

C Recursion (explain)

Hi, Question: how come the output is like that? Can explain to me abit. I am learning C. Thanks! #include <stdio.h> #include <string.h> void printit(char line_of_char, int index); int main() { char line_of_char; int index = -1; strcpy(line_of_char, "This is a string."); ... (5 Replies)
Discussion started by: seede
5 Replies

4. Shell Programming and Scripting

bash - Variable made of variable

Hello, I am struggling with using variable made using "eval". a=4 eval b$a=20 echo $b$a ??? As shown above, I am trying to call back the variable "bX" assuming I do not know the value of "a". How can I do that? I tried several combinations but nothing worked. Thanks (10 Replies)
Discussion started by: jolecanard
10 Replies

5. Programming

Recursion

I want to halt a tail recursive function after certain validation. I want to come out of entire recursion without unwinding phase. How can i achieve that . The coding is done in C language. (5 Replies)
Discussion started by: joshighanshyam
5 Replies

6. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

7. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

8. Shell Programming and Scripting

Help Help Help in recursion

Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me factorial() { if then y=`expr $1 - 1` x=$(( $1 \* factorial $y ))... (6 Replies)
Discussion started by: murtaza
6 Replies

9. Shell Programming and Scripting

recursion too deep

I am running a korn shell script which has a recursive function. The script ran for 117 iterations and ended up with the following error "recursion too deep". what should be done to avert this? Thanks in advance Swamy p.s. I am on UNIX MPRAS V4 (3 Replies)
Discussion started by: swamy455
3 Replies

10. Shell Programming and Scripting

recursion

I'm using the UNIX csh and i wish to use recursion to nav my way up (or down as it is) a given folder. My little test script is called "r" and takes a folder as argv (or $1) #!/bin/tcsh -f set allFiles = `ls -A $argv` cd $argv while ($#allFiles) if (-d... (1 Reply)
Discussion started by: gsjf
1 Replies
Login or Register to Ask a Question