Bash for loop with two variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash for loop with two variables
# 1  
Old 09-11-2013
Bash for loop with two variables

Hi,

I have the following folder structure:

Code:
TST500-1000
TST500-2000
TST500-3000
TST700-1000
TST700-2000
TST700-3000
TST900-1000
TST900-2000
TST900-3000

I would like to store the numbers in the first column (considering "-" as column separator) into a variable then the numbers in the 2nd column into another variable.

When I run :

Code:
for i in $(ls -1 -d TST* | awk '{print substr($0,4,3)}') ; do for j in $(ls -1 -d TST$i* | awk -F- ' {print $2}'); do echo $i $j ; done ; done

it duplicate all values:

Code:
500 1000
500 2000
500 3000
500 1000
500 2000
500 3000
500 1000
500 2000
500 3000
700 1000
700 2000
700 3000
700 1000
700 2000
700 3000
700 1000
700 2000
700 3000
900 1000
900 2000
900 3000
900 1000
900 2000
900 3000
900 1000
900 2000
900 3000

Would be possible to adjust the command to get the same number of values as folders:

Code:
500  1000
500  2000
500  3000
700  1000
700  2000
700  3000
900  1000
900  2000
900  3000

Thanks in advance,

Alex
# 2  
Old 09-11-2013
Code:
for x in $(ls TST*);do i=${x:3:3} j=${x:7:6};echo $i $j;done
500 1000
500 2000
500 3000
700 1000
700 2000
700 3000
900 1000
900 2000
900 3000

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 09-11-2013
Code:
for i in TST*
do 
  n=${i#TST}
  echo "${n%-*}" "${n#*-}"
done

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 09-11-2013
Thank you so much, it worked like a charm.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given. Please help me to understanding why node_port values are... (5 Replies)
Discussion started by: tmalik79
5 Replies

2. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

4. Shell Programming and Scripting

Bash for loop with awk and variables

I'm relatively new to bash scripting and am trying to use awk inside a bash for loop but am having a problem with the variables. for i in $(seq 30 5 60) do # Define variables up and down in AWK eval $(awk 'BEGIN{ print "up=$((i+1))"}' < /dev/null) eval $(awk 'BEGIN{ print... (2 Replies)
Discussion started by: lily-anne
2 Replies

5. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. 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

10. Shell Programming and Scripting

using variables outside a while loop

Hi Guys, I have a scripts that uses a while loop to read a file and set 2 variables. How can I do this so the variables can be used outside the while loop ? Below is an example....# ./junk2 -m -e user EXE=user master=TRUE DB_TAG=PRODUCT In loop MST=MST=testsvr1:3110 In loop ARGS=... (2 Replies)
Discussion started by: Tornado
2 Replies
Login or Register to Ask a Question