LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-13-2011, 04:32 AM   #1
mcollis
LQ Newbie
 
Registered: Sep 2011
Location: Wales / UK
Distribution: OpenSuse 11.x / Ubuntu
Posts: 7

Rep: Reputation: Disabled
script to read number from config file


Hi All,

I've been making some scripts recently and have gotten to the stage where I want to do the following:
We have a ftp file setup (call it folder A) and I want to make it so that on a nightly basis the files are moved from folder A to an archive (folder B) then once a week I will Tar the contents of this folder.

I have created the following script:
Code:
#get the counters id for a new file
read counter.ini c1

#make a new folder for the archive
cd <Folder B>
mkdir $c1

move all folders into the folder
cd <Folder A>
mv *.* <Folder B>/$c1

#set permissions
setfacl -Rm g:...<this bit works>


#increment the counter by 1
cd /data/scripts/archivescan/
let c2=$c1+1

mv counter.ini counter.old
echo $c2 > counter.ini

setfacl -Rm g:...<this bit works>
The file "counter.ini" contains only a number and nothing else
This is the current method that I have chosen to make the folders unique, Unless there is an option to get the current date and use that in a folder name?

Thanks in advance

Last edited by mcollis; 09-13-2011 at 08:13 AM. Reason: Solved
 
Old 09-13-2011, 04:38 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Yes. Using the date command in command substitution you can create a folder name with date in any format you want, e.g.
Code:
mkdir $(date +%Y-%m-%d_%H%M)
if you use the format yyyymmdd the directories will be sorted alphabetically with the ls command, so that the most recent one is at the bottom of the list.
 
1 members found this post helpful.
Old 09-13-2011, 06:25 AM   #3
mcollis
LQ Newbie
 
Registered: Sep 2011
Location: Wales / UK
Distribution: OpenSuse 11.x / Ubuntu
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Yes. Using the date command in command substitution you can create a folder name with date in any format you want, e.g.
Code:
mkdir $(date +%Y-%m-%d_%H%M)
if you use the format yyyymmdd the directories will be sorted alphabetically with the ls command, so that the most recent one is at the bottom of the list.
Excellent, Thanks for the help there.

Is there a way that I can check if the file is empty before continuing?
So, in my script i'll create the target folder B but if Folder A is empty it stops?
 
Old 09-13-2011, 06:38 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
There are different ways to check if a folder is empty. Here is an example:
Code:
[[ -z $(ls FolderA) ]] && exit
 
Old 09-13-2011, 07:19 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Here's another way purely using bash inbuilts
Code:
shopt -s nullglob
[[ -z "$(printf "%s" *)" ]] && echo "empty"
 
1 members found this post helpful.
Old 09-13-2011, 08:13 AM   #6
mcollis
LQ Newbie
 
Registered: Sep 2011
Location: Wales / UK
Distribution: OpenSuse 11.x / Ubuntu
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kurumi View Post
Here's another way purely using bash inbuilts
Code:
shopt -s nullglob
[[ -z "$(printf "%s" *)" ]] && echo "empty"
That was just the ticket.
Now working perfectly.

If anyone would like a copy of my scripts they are below:

This moves files from Folder A to Folder B/Today's Date (D-M-Y format)
Code:
#!/bin/bash

#make a new folder for the archive
cd <Folder B>
mkdir $(date +%d-%m-%Y)

#check if folder A is empty before continuing

cd <Folder A>
shopt -s nullglob
[[ -z "$(printf "%s" *)" ]] && echo "Scans folder is empty
Checked on $(date +%d-%m-%Y) at $(date +%H-%M)
------" >> /scripts/archivescan/archive.log && exit

#Folder A has Data
	#goto the scans folder
    cd /data/headoffice/departments/scan/
    #log the files to be moved
    echo "Files to be moved to scan_archive:" >> /scripts/archivescan/archive.log
    ls -R >> /scripts/archivescan/archive.log
    echo "------" >> /scripts/archivescan/archive.log
    
    #now move the files
    mv * <Folder B>/$(date +%d-%m-%Y)
    
    #set permissions
    #if required
    
    #log notice to show successful run
    cd /data/scripts/archivescan
    echo "Scan Files successfully moved on $(date +%d-%m-%Y) at $(date +%H-%M)" >> archive.log
    echo "------" >> archive.log
This then creates a GZip Tar of the folders in the archive location:
Code:
#change to the scan_archive folder
cd <Folder B>

#make a list of the folders to be archived into the log file
echo "Files to be archived:" >> /scripts/archivescan/archive.log
ls | grep -v *.tgz >> /scripts/archivescan/archive.log
echo "------" >> /scripts/archivescan/archive.log

#Compress the files into a tar (gzip compressed) file and remove source files
tar cfz archive_$(date +%d-%m-%Y_%H-%M).tgz * --exclude=*.tgz --remove-files

#log notice to show successful run
cd /scripts/archivescan
echo "Scan Files successfully archived on $(date +%d-%m-%Y) at $(date +%H-%M)" >> archive.log
echo "------" >> archive.log
 
Old 09-13-2011, 08:52 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by mcollis View Post
The file "counter.ini" contains only a number and nothing else
This is the current method that I have chosen to make the folders unique, Unless there is an option to get the current date and use that in a folder name?
You can use new_dir="$(mktemp -d /the/path/prefixXXXXXX)" to create a unique directory. Note that the 6 Xs are literal. mktemp will create the directory and echo its name.
Kevin Barry
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Read a number x from a file and write a character in the xth column of other file homexylo Programming 7 08-05-2011 06:28 AM
[SOLVED] read filename from the command prompt and display the number of lines in file hd_pulse Programming 9 05-30-2011 10:39 PM
[SOLVED] read the file, add number and get the average Alkass Programming 4 04-30-2010 01:14 AM
read-only file system error when editing a config file smallfrowne Linux - Newbie 5 02-19-2008 07:06 PM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 04:03 PM


All times are GMT -5. The time now is 07:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration