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