For/While Loop to Increment Filenames in a Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For/While Loop to Increment Filenames in a Script
# 1  
Old 10-31-2012
For/While Loop to Increment Filenames in a Script

Daily stupid question. I want to increment the file name everytime the script is run. So for example if the filename is manager.log and I run the script, I want the next sequence to be manager.log1. So to be clear I only want it to increment when the script is executed. So

Code:
./script
manager.log1
./script
manager.log2
./script
manager.log3

I started with this but to no evail.

Code:
#!/bin/bash

inc_log()
{
for file in $man_log
do
    (( count++ ))
cp $man_log $man_log$count
done
}

count=0
man_log=/home/user/manager.log

        if [ -s "manager.log" ]; then

        inc_log

        echo "finished"
fi
exit

Thanks in advance.
# 2  
Old 10-31-2012
Code:
count=$((count + 1))

I think some shells support something like what you had, but I've never used that syntax.
# 3  
Old 10-31-2012
Counting up is going to have a problem. Imagine you have log, log1, 2, 3
  • Copy log to log1, destroying the old log1.
  • Copy log1 to log2, destroying the old log2.
  • Copy log2 to log3, destroying the old log3.
So you just end up with three copies of the latest log.

I'd do this:

Code:
inc_log() {
        set -- manager.log*
        while [ "$#" -gt 0 ]
        do
                old=$#
                let new=old+1
                echo cp manager.log$old manager.log$new
        done

        echo cp manager.log manager.log1
        echo ":>" manager.log # Truncate manager.log
}

Remove the echo, and the quotes from ":>", once you've tested and can see it does what you want.
# 4  
Old 10-31-2012
Code:
#!/bin/bash

inc_log()
{
for file in $man_log
do
    (( count++ ))
cp $man_log $man_log$count
done
}

count=$((count + 1))
man_log=/home/user/manager.log

        if [ -s "manager.log" ]; then

        inc_log

        echo "finished"
fi
exit

but it only increments once. So

Code:
./script
manager.log1
./script
manager.log1
./script
manager.log1

?
# 5  
Old 10-31-2012
You did it in the wrong place.

You should have done that in the function instead of ((count + 1)).

Besides, while I may have answered your question, Corona688 has given a better solution to your problem.
# 6  
Old 10-31-2012
why should it run more than once? manager.log only exists once, and you haven't told it to look for anything else but manager.log.

If you want to pass things into a function, pass them into a function, don't use a global variable.

Code:
inc_log() {
        local M=$1 # Save base name for later
        set -- ${1}* # Set $1, $2, ... into the list of 
        while [ "$#" -gt 0 ]
        do
                old=$#
                let new=old+1
                echo cp $m$old $m$new
        done

        echo cp $m ${m}1
        echo ":>" $m # Truncate manager.log
}

inc_log manager.log

# 7  
Old 10-31-2012
thank you Corona688. Unfortunately the script stays in the loop

Code:
#!/bin/bash

inc_log() {
        set -- manager.log*
        while [ "$#" -gt 0 ]
        do
                old=$#
                let new=old+1
                echo cp manager.log$old manager.log$new
        done

        echo cp manager.log manager.log1
        #echo ":>" manager.log # Truncate manager.log
}

man_log=/home/saint/manager.log

        if [ -s "manager.log" ]; then

        inc_log

        echo "finished"
fi
exit

Code:
cp manager.log1 manager.log2
cp manager.log1 manager.log2
cp manager.log1 manager.log2
cp manager.log1 manager.log2^C


I was told by the administrator of the server that his script only runs while booting up the server and this is what he wanted. I had a brain hickup and forgot the basic stuff in shell script(loops). thanks for the help

---------- Post updated at 01:32 PM ---------- Previous update was at 01:19 PM ----------

Will give this a shot. Many Thanks

Last edited by Scott; 10-31-2012 at 02:42 PM.. Reason: Fixed code tag
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parallel increment of nested for loop

Hi, I am using solaris 5.10 environment and need help on doing parallel increment of nested for loop. Samples #inside the code the values assigned to a variable by another awk command will be like a=/xyz/pg/as /xyz/pg/as2 /xyz/pg/as3 b=/xyz/sd/fd1 /xyz/sd/fd2 /xyz/sd/fd3 for q in... (1 Reply)
Discussion started by: ananan
1 Replies

2. Windows & DOS: Issues & Discussions

Batch file loop and increment value for condition

I am trying to have the below batch file do following two things: 1. only allow the values YES,yes,Y,y, or NO,no,N,n 2. increment the counter %var1 only if answer to question 2 is "y" and not able to get the syntax correct. If %var1%=1 then I am trying to display function :end. Thank you :).... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Find and increment at each occurence of string (loop)

I created script (sh shell) to generate vlc playlist based on some data files. All works fine so far except one string I do not know how to handle with. VLCSTART='<vlc:id>' VLCV=0 VLCEND='</vlc:id>' echo -e $'\n'$'\t'$'\t'$'\t'$'\t'\$VLCSTART$VLCV$VLCENDOutput file contains several occurences... (10 Replies)
Discussion started by: TiedCone
10 Replies

4. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

5. Programming

[Xquery] How to do a increment in a For loop

Hello men. How can i build a simple increment for $a by Xquery such as ? let $a := 0 for $i in (1 to 10) let $a := $a + 1 return $a why a in this loop always is '1' Thank you for reading, its will really helpful for my job if i can solve this problem :D:D (3 Replies)
Discussion started by: tien86
3 Replies

6. Shell Programming and Scripting

Whitespace in filenames in for loop in bash script

I'm trying to search all .odt files in a directory for a string in the text of the file. I've found a bash script that works, except that it can't handle whitespace in the filenames. #!/bin/bash if ; then echo "Usage: searchodt searchterm" exit 1 fi for file in $(ls *.odt); do ... (4 Replies)
Discussion started by: triplemaya
4 Replies

7. Shell Programming and Scripting

loop through numbered filenames

Hi I'm very new to this script thing, so please be gentle. I am trying to get a command - the mach2qtl command in the code below - to loop through a set of files. Each command should take the same two .dat and .ped files, but the .mlinfo and .mlprob files with filenames including 'chrom1' ... (7 Replies)
Discussion started by: polly_falconer
7 Replies

8. Shell Programming and Scripting

Increment nested for loop parllely

Hi , I am trying to increment the nested for loops parellely,but i cant ,i used continue 2 but the second loop not getting increment. no1="1 6 5 4 8" no2="4 7 8 0 1" for var1 in $no1 ; do for var2 in $no2 ; do line1 line 2 line 3 continue 2 done done Please help on this (4 Replies)
Discussion started by: nmahendran
4 Replies

9. Shell Programming and Scripting

the given code goes in infinite loop and does not increment variable i

code is as #!/bin/sh i=1; while do welcome $i times; i='expr $i+1'; done exit 0; (6 Replies)
Discussion started by: mrityunjay22
6 Replies

10. Shell Programming and Scripting

Increment date in 'for' loop?

Hi Guys, My first post..:) Right...I want to move existing files (with some date in their name) currently in $mainftp, to $mainfolder/$foldate/system1. I'd like to be able to increment date in the for loop? Is this possible or should I use a different technique. The script return the... (4 Replies)
Discussion started by: SunnyK
4 Replies
Login or Register to Ask a Question