Small help required to create For loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Small help required to create For loop
# 1  
Old 06-04-2012
Small help required to create For loop

Hi,
I am new to UNIX and have an issue.
I need to check that whether a perticular file name exists or not. I wan to do that in loop to avoid number of IF statements.
I need to check that
if file10.txt exists rename that to file11.txt.
if file9.txt exists rename that to file10.txt.
:
:
if file1.txt exists rename that to file2.txt.

Please help.
Thanks.
# 2  
Old 06-04-2012
Assuming you never go higher than 11:
Code:
cnt=10
while [ $cnt -gt 0 ]
do
   fname=file${cnt}.txt
   if [ -r $fname ];  then
         next=$(( $cnt + 1 ))
         tmp=file${next}.txt
         mv $fname $tmp
   fi
done

next and tmp added for clarity.
# 3  
Old 06-04-2012
Thanks a lot. But it seems we are not reducing the value of the CNT in this logic.How will we do that?
# 4  
Old 06-04-2012
MySQL

It is ideally
Code:
next=$(( $cnt - 1 ))

Looks like a typo to me.
# 5  
Old 06-04-2012
No, the increment is correct... Try:

Code:
cnt=11
while [ $((cnt-=1)) -gt 0 ]

# 6  
Old 06-04-2012
Thanks a lot for the help. Yes the increment was for moving the file name to new one. I was not able to decrease the count. Thanks again to both of you.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

Create a file with input values required

Hi Guys Please can you help me to create a file using the following inputs 2351 first input 2339 second input all this rows need to have the value 0 in front 2338 third input 2333 fourth input all this rows need to have the value 1 in front count all the rows in the file and insert the... (10 Replies)
Discussion started by: jiam912
10 Replies

3. Shell Programming and Scripting

how to create a loop in an if statement

Hey guys, a=`cat abc | wc -l` b=`cat def | wc -l` if $a== $b then echo "a" else echo "b" fi I want the if condition to retry itself , untill a==b. I can't use goto statemt. Please help. Thanx in advance. Please use next time code tags for your code and data (5 Replies)
Discussion started by: jaituteja
5 Replies

4. Shell Programming and Scripting

Small Help required for merging two files per line!!!

Hi Guys, Sorry to bother everyone again here, but I ran into a small problem that has been after me for some time now. I have a file called file1.txt and has following content - /folder1/fold/folder 10k /folder167fold/folder 10MB /folder2/fold/folder 10G etc.. etc.. ... (1 Reply)
Discussion started by: rockf1bull
1 Replies

5. Shell Programming and Scripting

Loop script required within an active log file

I need some help please... This is the scenario what I am sitting with. 1 - I am kicking of a batch script. 2 - This batch script is writing to an active log file which is also use by other batch scripts. 3 - I need to monitor this active log file and grep for the script name as well as the... (3 Replies)
Discussion started by: Henk Trumpie
3 Replies

6. UNIX for Dummies Questions & Answers

A small script required!!

Hi All, I am using korn shell sun solaris 10 What i need is i have to grep one file and if there are any finding then i have set one flag. otherwise continue Please help Regards Arun (9 Replies)
Discussion started by: annyarun
9 Replies

7. Shell Programming and Scripting

Small shell script help required

Hi Guys, Please can some one explain me the below part of code. In this code what is the use of the line in Bold. COPY=0 if ; then echo "$CONF exists and is non-empty - backing it up" SUFFIX=`date +%Y%m%d%H%M%S` echo "cp -p $CONF $CONF.$SUFFIX" cp -p $CONF... (4 Replies)
Discussion started by: max29583
4 Replies

8. Shell Programming and Scripting

End of loop condition required???

Hi i have a variable with lots of tokens seperated with spaces. e.g VAR="ABC DEF GHSD GHQS TUTSD JHDTQ QDHQ CDQKDGQ WQUTQD DQUTQD DQJGDQ QDTQD WDQUTQDU QDUGQD QDJGQD DQUTDUQ QDUIDTQ" i want to separate all of the above tokens and call a script with each of the token e.g sh script.sh <TOKEN>... (4 Replies)
Discussion started by: skyineyes
4 Replies

9. UNIX for Advanced & Expert Users

Compress - How small can a file before it will create the .Z

I am just curious. Compress by itself will not compress a file of zero bytes. (I know gzip does) Without using compress -f, at what point will compress work. In other words, what is the smallest the file can be before it will create the .Z file? Some of us here are just wondering... Thx (4 Replies)
Discussion started by: app4dxh
4 Replies
Login or Register to Ask a Question