For loop like execution in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop like execution in shell
# 1  
Old 01-05-2012
For loop like execution in shell

Hi,

In the script that i work now, I wrote the script such a way that the values retrieved using a tcl script is passed into a shell script to specify the path where the specific file needs to be sourced exists.

For eg: I got a path from the tcl script given below,

/A/B/C/D3/E/<File_name>

Here A, B, C, all are folders.

In the above example "D3" is the only variable keeps changing rest all are static.

My query is, some times the Variable retrieved from the tcl script "D3" might not exists so we are supposed to search for the lower version of "D3" that is "D2" and to fetch the file.

I dont know whether it can be achieved using for Loop, I need your guidance to resolve this issue.
# 2  
Old 01-05-2012
D3 is the actual name? If not, what are the rules for this directory?
How many alpha bates? numbers? and upto which number you want check for the lower versions? like .. If not in D3, check D2, if again not, check D1?
This User Gave Thanks to clx For This Post:
# 3  
Old 01-05-2012
you don't need a for loop, you need a while loop, you should test for the existence [ -e $directory_name ] of the D3 value in the while loop, and if not decrement the value, as we don't have the details of the contents of D3 (I'm assuming it's a hex value, for no good reason, and so easy to decrement)
This User Gave Thanks to Skrynesaver For This Post:
# 4  
Old 01-05-2012
anchal_khare:

D3 is the name i used for example;

3 alphabets and 4 numbers will be there altogether in the name of the directory .


eg: siv4018 ; this will be formed initially based on the input from the TCL script, in case if this directory doesn't exists it should search for siv4017 like wise it should be decremented one by one; in this eg upto "siv401" will be fixed and only the last numeric value alone should be considered.
# 5  
Old 01-05-2012
Try this construct
Code:
while [ ! -e $filename ] && [ $(( ${#filename} - 1 )) -ne 0 ] 
   do  
      filename=${filename:0:$(( ${#filename} - 1 ))}$(( ${filename:$(( ${#filename} - 1 )) } -1))
   done
echo $filename

It decrements the final character until it equals zero, you'll have to check again for the existence of the file after the loop of course...
This User Gave Thanks to Skrynesaver For This Post:
# 6  
Old 01-05-2012
Better yet, you could do:
Code:
last_version=`ls -d siv[0-9]* | sort -n | tail -n 1`

That is,
Code:
dir0=siv4210
dirNoVer=${dir0%%[0-9]*}  #'siv'
dirLastVer=$(ls -d ${path}/${dirNoVer}[0-9]* | sort -n | tail -n 1)

This User Gave Thanks to mirni For This Post:
# 7  
Old 01-05-2012
mirni's suggestion is fine if there won't be a case where
Code:
ls siv*
siv4211 siv4215 siv4217
dir0=siv4216
...

This User Gave Thanks to Skrynesaver For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execution of loop :Splitting a single file into multiple .dat file

hdr=$(cut -c1 $path$file|head -1)#extract header”H” trl=$(cut -c|path$file|tail -1)#extract trailer “T” SplitFile=$(cut -c 50-250 $path 1$newfile |sed'$/ *$//' head -1')# to trim white space and extract table name If; then # start loop if it is a header While read I #read file Do... (4 Replies)
Discussion started by: SwagatikaP1
4 Replies

2. Shell Programming and Scripting

Execution of Shell Commands

I have a question: Where would I put the Command line (of any command) so that it executes every time I log on? Where would I put it if I want it to execute every time I start a new shell? (5 Replies)
Discussion started by: Nabeel Nazir
5 Replies

3. Shell Programming and Scripting

Problem in loop execution

Below is my code. I want a loop in this way that if folder has tag sub-folder then it will show the list of tags otherwise it will show the subfolders of that folder and then again user will select sub-folder and if tags found then show the tag list otherwise show all subfolders till he finds the... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

Shell execution

hi, Pls bare with me if this is trivial. Is it possible to run a korn shell with out specifying the name with its extensiion? Do i need to make any changes to the script make it work? Thanks (3 Replies)
Discussion started by: dvah
3 Replies

5. Shell Programming and Scripting

Execution of SQLs thru loop

Hi, I have a file in which I have a number of insert statements (number is not fixed, it may have 10 statements or may be 1000....not fixed). I want to execute these statements using a loop (1 statement per iteration). For ex: Input file has following statements: Insert statement #... (6 Replies)
Discussion started by: ustechie
6 Replies

6. UNIX for Advanced & Expert Users

tar command loop during execution

I've some problem concerning tar command. Sometime tar command submitted to create a tar file, in execution loop over the same group of files. Can anyone help me? Tanks (15 Replies)
Discussion started by: Jocker
15 Replies

7. Shell Programming and Scripting

Questions on shell execution

Hi, I have a question regarding Korn shell script execution in HP-UX 11.11. What sort of environmental settings do I need to run a Korn shell script such as below without entering "./" at the begining of the command? cat test.ksh date I am able to do this with a user called infodba who... (6 Replies)
Discussion started by: stevefox
6 Replies

8. Programming

how to stop execution in for loop

Hi all, I am working on a c source code nearly 2000 line . it contains one big for( i=0; i< 200 ; i++ ) loop of around 600 lines could any tell me how to break the execution of prog when the value of i is 50 in for loop so that i can check inside the loop. Thanks.. (1 Reply)
Discussion started by: useless79
1 Replies

9. Shell Programming and Scripting

Please help on shell scripts execution

I have already posted the question. Because previous post has sinked so that I have to ask the question again. I created the script and chmod it as 755. The ksh shell is in bin. Now I typed ./script_name.ksh to execute the script in the directory of that script. The return message was: Ksh:... (13 Replies)
Discussion started by: duke0001
13 Replies

10. Shell Programming and Scripting

execution of shell script

How can I execute another shell script from one? Malay (5 Replies)
Discussion started by: malaymaru
5 Replies
Login or Register to Ask a Question