Repeat for different variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Repeat for different variable
# 1  
Old 01-22-2014
Repeat for different variable

Hey,

I've created following script:

Code:
var1=test1
setA=testA

if
   ...
   touch $setA/$var1 
   ...
fi

I would like now the repeat the command touch (in this example) for different variables.
So below, the varX should run 3 times (var1, var2, var4). Var3 is skipped in this example since it's not filled in.

Code:
var1=test1
var2=test2
var3=
var4=test4

setB=testB
setC
setD=testD


if
   ...
   touch $setX/$varX 
   ...
fi


What's the best way to do this?
I can of course copy the if x-time and change the $setX/$varX. But I suppose there is a better way (since the script is about 30 lines, and the variables are about 10 values)...

Last edited by brononius; 01-22-2014 at 03:28 AM..
# 2  
Old 01-22-2014
with bash, you can do something like this.

Code:
set | grep -E '^var[0-9]+=' | while IFS== read VAR VAL
do
 if [ ! -z $VAL ]; then
    touch $VAL
 fi
done



EDIT : Original requirement changed. Still you can modify the script accordingly. Always re-post the updated requirement instead changing the original.
# 3  
Old 01-22-2014
Quote:
Originally Posted by clx
Always re-post the updated requirement instead changing the original.
Sorry, I hoped/wished that nobody seen this already. But you guys are fast here. Smilie

I'm fighting with your example to get the 2 values in. But for one of the other reason, i can't figure out how to use the second variable in the same script.
Should/can I do that in the same 'set' command? With a kind of 'and' command?

For the moment i've got:
Code:
SensorID1='test1'
SensorValue1='watt'
SensorID2='test2'
SensorValue2='liter'

set | grep -E '^SensorID[0-9]+=' | while IFS== read VAR SensorID 

do
 if [ ! -z $SensorID ]; then
    touch $SensorID.$SensorValue
fi
done

# 4  
Old 01-22-2014
Please test and try
Code:
set | grep -E '^SensorID[0-9]+=' | while IFS== read VAR SensorID 
do
 SensorValue=$(eval echo \$${VAR/SensorID/SensorValue})
 if [ ! -z $SensorID ] && [ ! -z $SensorValue ] ; then
    touch $SensorID.$SensorValue
fi
done


You can remove the AND part in the "if" statement if not required.
# 5  
Old 01-22-2014
In above, you don't touch $SensorValue as it is undefined. On top, you can't make sure that the variables will be listed in the necessary order by set.
You request is not clear. Let me try to paraphrase it: You need to touch m files in n directories, i.e. (n x m) files in total, and directories and files are held in a set of variables each.
Use two nested loops, and check if it's possible to hold the variables in two arrays.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to use loop to repeat task?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. How can i use loop to repeat task. 2.shirt=15 black=13.50 echo "how many shirt you want" read num echo echo "Please enter a choice" echo "1 ---> normal... (5 Replies)
Discussion started by: Tauatioti
5 Replies

2. Shell Programming and Scripting

Repeat a command for one sec

How to repeat the execution of a simple command like the following for 1 sec ? echo Hi The completion time for the command is not known, but we need to calculate the number of times this commans executes successfully within 1 sec. Thanks Kumarjit (5 Replies)
Discussion started by: kumarjt
5 Replies

3. Shell Programming and Scripting

Repeat command with new variable for each line in txt file

Well here is my question. Let's say I have this Script: find /var/mobile/ maxdepth -2 name "$x" >> /"$x".txt The thing is I want to repeat this script and pull out a variable from a text file like this (each line = new variable $x and another run of the whole command) Thanks for... (27 Replies)
Discussion started by: pasc
27 Replies

4. Shell Programming and Scripting

Ping and repeat ?

How do i write a loop ping to see if it get timeout or hang ? it should loop every 30 second to ping a server ? ping -c 5 -t 15 www.google.com if ]; then date '+%Y-%m-%d %H:%M:%S Connection Unavailable' >> /home/sabercats/checkconnection.log else date '+%Y-%m-%d %H:%M:%S Connection... (3 Replies)
Discussion started by: sabercats
3 Replies

5. Shell Programming and Scripting

Repeat using for loop

I have a file like this 2011-10-10 10:46:00,1-1-13-1-1,151510,ALCLA0A84D2C 2011-10-10 10:46:00,1-1-13-1-1,151520,65537 2011-10-10 10:46:00,1-1-13-1-1,151515,46932 2011-10-10 10:46:00,1-1-13-1-1,151521,32769 2011-10-10 10:46:00,1-1-13-1-1,151522,32769 2011-10-10... (4 Replies)
Discussion started by: LavanyaP
4 Replies

6. UNIX for Dummies Questions & Answers

repeat each record n times

I have: aa01 aa02 aa03 aa04 ab01 ab02 ab03 ab04 I would like each record printed 5 times: aa01 aa01 aa01 aa01 aa01 aa02 aa02 (6 Replies)
Discussion started by: kenneth.mcbride
6 Replies

7. Shell Programming and Scripting

to copy and repeat

Hi All, I have done some looking at other threads but haven't found quite what I am looking for. I am a newbie to scripting and haven't got to where I want to you but here is my basic question. I have a script to copy a file and send it to another file with a date and time stamp. What I want to... (4 Replies)
Discussion started by: falcondown01
4 Replies

8. UNIX for Dummies Questions & Answers

Repeat Commands

On my system I use Escape "k" to go back in commands. I read on tutorials that it is ctrl p, but that does not work on my system. Anyone know what the command to go foward is? (6 Replies)
Discussion started by: dereckbc
6 Replies

9. UNIX for Dummies Questions & Answers

any idea to repeat a action in VI

Any idea to repeat an action to all the lines in vi... suppose i want to delete the first word from all the lines in VI .. how would i do it ? in general i am also looking for a way to apply a action to all the lines in VI . (6 Replies)
Discussion started by: myelvis
6 Replies

10. UNIX for Dummies Questions & Answers

Repeat a command in a shell

Hi there, i would like to repeat a command in a shell sript (bash) the script starts with a menu to choose a menu point to do something .... on the end of the script i would like to restart the programm to choose the menu points on the beginning. I would also make a sript that send... (2 Replies)
Discussion started by: scotty
2 Replies
Login or Register to Ask a Question