LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script to read number from config file (https://www.linuxquestions.org/questions/programming-9/script-to-read-number-from-config-file-902733/)

mcollis 09-13-2011 03:32 AM

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

colucix 09-13-2011 03:38 AM

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.

mcollis 09-13-2011 05:25 AM

Quote:

Originally Posted by colucix (Post 4470398)
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?

colucix 09-13-2011 05:38 AM

There are different ways to check if a folder is empty. Here is an example:
Code:

[[ -z $(ls FolderA) ]] && exit

kurumi 09-13-2011 06:19 AM

Here's another way purely using bash inbuilts
Code:

shopt -s nullglob
[[ -z "$(printf "%s" *)" ]] && echo "empty"


mcollis 09-13-2011 07:13 AM

Quote:

Originally Posted by kurumi (Post 4470496)
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


ta0kira 09-13-2011 07:52 AM

Quote:

Originally Posted by mcollis (Post 4470395)
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


All times are GMT -5. The time now is 04:03 AM.