File naming in array adds additional extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File naming in array adds additional extension
# 1  
Old 06-09-2010
Question File naming in array adds additional extension

Hi,

I have a bash script that reads the directories from a jboss/server directory in
order to start specified server instances. Part of the script archives older logs and creates new ones if required. To do this, it reads the array of directories and in a loop archives, and creates new ones depending on the file size and existence.

The new files are are named as <array_entry>.log which works fine except the last array_entry is always created as <array_entry>.log.log I have experimented by removing and adding entries to the array and it always creates the last entry with a .log.log extension.

The loop is like this:


Code:
function processLogs {


	printf "\n"
	printf "Checking the log files exist and are writeable \n"
	printf " \n"
	printf "contents of $LOG_DIR are: \n"
	ls $LOG_DIR
	printf "\n"

	# check to see there are any run.sh process running
	if [ `/bin/pidof -x run.sh | wc -l` = 0 ]
	then
   	for log in "${dir_array[@]}".log
    	do
       	 	if [ -e "$LOG_DIR/$log".log ]
		then 
			# file exists, so archive it and create a new version 
	        	mv -f $LOG_DIR/$log.log $LOG_DIR/archive/`eval date +%Y%m%d-%k:%m`-$log.log
			printf "Moved $log.log to archive/$log.log \n"
			# let's make it non-writeable too so it can't be re-used by accident
			chmod -w $LOG_DIR/archive/`eval date +%Y%m%d-%k:%m`-$log.log
			touch $LOG_DIR/$log.log # create new files
	   		chmod a+w $LOG_DIR/$log.log
		elif [ ! -e "$LOG_DIR/$log".log ] 
		then
			# log file doesn't exist so create and make it writeable
			printf "Log file $log.log doesn't exist, creating it \n"
			touch $LOG_DIR/$log.log 
	   		chmod a+w $LOG_DIR/$log.log
		elif [ ! -w "$LOG_DIR/$log".log ]
		then
	   		# log file exists but isn't writeable
				# printf "logfile $log isn't writeable, correcting it \n"
		   	chmod a+w $LOG_DIR/$log.log
		fi
    	done
	else
		printf "there are running processes so left log files alone \n"
		#printf "processes are: \n"
	    	#ps -ef | grep "run.sh" | grep -v grep | awk '{print $2}'
	fi
	printf "contents of $LOG_DIR are: \n"
	ls $LOG_DIR
	printf "\n"
}

Obviously the array and other variables are set further up, the array is definitely filled. This is bash on Ubuntu 10.04.

[edit] just to be clear, the touch $LOG_DIR/$log.log command in both the archive and create elif conditions do the same
thing, ie create the last .log.log file.

Thanks

Last edited by Scott; 06-09-2010 at 05:22 AM.. Reason: Please use code tags
# 2  
Old 06-09-2010
Hi,

I think that when you do:
Code:
for log in "${dir_array[@]}".log

the las log has the .log suffix.

Try with:
Code:
for log in "${dir_array[@]}"
do
   log=$log.log
   ...and the rest

This User Gave Thanks to albertogarcia For This Post:
# 3  
Old 06-09-2010
good grief, can't believe I missed that! thanks, that was it of course.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with naming the file

Hi, I have a folder that contains files abc.txt def.txt ....and so on Inside abc.txt, I have @<TRIPOS>MOLECULE 4|Chelerythrine|abcb11_earlyIdentification_Stronginhib_washed_ligprep|sdf|1|dock Inside def.txt, I have @<TRIPOS>MOLECULE... (6 Replies)
Discussion started by: rossi
6 Replies

2. Shell Programming and Scripting

file naming in a script

#!/bin/bash while read inputline do what="$inputline" echo $what; if ; then exit fi $reextend $what $print ls -a done this is my code i am trying to change all of the file types of a certain directory to another file type but im not all the way there can someone help please (3 Replies)
Discussion started by: domdom110
3 Replies

3. Shell Programming and Scripting

File naming format

Hi, su - oracle<<EOC export PATH=${PATH}:${ORACLE_HOME}/bin exit EOC set `sqlplus -S $user_name/$password@$tns<<EOS set head off select min(time),max(time) from products; exit; EOS` var1=$1 var2=$2 su - oracle -c "exp user/pass@localdb... (7 Replies)
Discussion started by: milink
7 Replies

4. Shell Programming and Scripting

m4 adds extra space at top of file.

I have used m4 in the past to generate source code where aesthetics and space were of no consequence . Now I am using it to generate script and program templates . So here is an excerpt from my m4 file for producing a generic bash script: dnl `$Id$' define(`START_SCRIPT',`#!/bin/bash... (8 Replies)
Discussion started by: Bubnoff
8 Replies

5. Shell Programming and Scripting

File splitting, naming file according to internal field

Hi All, I have a rather stange set of requirements that I'm hoping someone here could help me with. We receive a file that is actually a concatenation of 4 files (don't believe this would change, but ideally the solution would handle n files). The super-file looks like:... (7 Replies)
Discussion started by: Leedor
7 Replies

6. Shell Programming and Scripting

file naming question

Hi, I need some help! I have a file in which i im splitting into 20 different files each called model_001.in model_002.in etc... i would like to make directory for each file using only the name and not the extension so that the directory names are model_001 model_002 etc. ... (8 Replies)
Discussion started by: olifu02
8 Replies

7. Shell Programming and Scripting

#file naming

hi all, Please advise at what circumstance those file will become -rwxr-xr-x 1 psa psa 1969088 Aug 18 2006 #libaa.sl -rwx------ 1 psa psa 2166784 Jul 25 2006 #libcrypto.sl.0.9.7 -rwx------ 1 psa psa 904040 Jul 25 2006 #libxxx.sl -rwx------ 1 psa ... (2 Replies)
Discussion started by: rauphelhunter
2 Replies

8. Shell Programming and Scripting

issue in naming a file

Hi, I want to create a file named 'abc(+1)' and append the data of file 'abc' to it. But getting error as unexpected'(' when i tried to use the following command. cat abc > abc(+1) Is there any other way to include brackets along with +1 in the file name? TIA. (3 Replies)
Discussion started by: vimalr
3 Replies

9. Solaris

6120 Array. Additional physical Disks and ZFS

Hi; I have 4 new disks in a 6120 Array attached to a SUN server running zfs. There are already two virtual disks on the array comprising of 3 disk raid 5 for each Vdisk. I need to add two more disks to each vdisk making each a 5 disk raid 5 Vdisk. If ZFS already has the original... (3 Replies)
Discussion started by: myjess
3 Replies

10. Shell Programming and Scripting

naming a file to hostname

I am running a script remotely to another client. after it runs it places the file in /tmp. I need the file in /tmp to be renamed to the local hostname. but when i set the variable it names the file to my local hostname. how do i fix that (4 Replies)
Discussion started by: deaconf19
4 Replies
Login or Register to Ask a Question