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

./script
manager.log1
./script
manager.log2
./script
manager.log3

I started with this but to no evail.


#!/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.

count=$((count + 1))

I think some shells support something like what you had, but I've never used that syntax.

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:

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.

#!/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

./script
manager.log1
./script
manager.log1
./script
manager.log1

?

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.

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.

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

thank you Corona688. Unfortunately the script stays in the loop

#!/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
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

I forgot a line. My bad.

#!/bin/bash

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

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

Got it with a little help

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

man_log=/home/user/manager.log

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

	inc_log

	echo "finished"
fi 
exit
./script
./script
./script
./script
./script

ls -ltr
-rw-r--r--  1 root  root    23 Oct 31 15:04 manager.log1
-rw-r--r--  1 root  root    23 Oct 31 15:04 manager.log2
-rw-r--r--  1 root  root    23 Oct 31 15:04 manager.log3
-rw-r--r--  1 root  root    23 Oct 31 15:05 manager.log4
-rw-r--r--  1 root  root    23 Oct 31 15:05 manager.log5

I needed to add the wildcard in the "for" statement, otherwise only the current manager.log is taken in account. Thanks for all the help.

Just be careful before you run it a second time :wink:

(vis-�-vis Corona688's suggestion)

There's no point using a global variable when you're using a function, just pass it into the function. Try my code again with the fix please.