ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello
I would like some help in making a bash script for copying a file every one minute
from one location to another. I would like the "target" file to have a different
name from the "source" by adding a digit (which increases) in the end of it.
For example I have a file "test.txt"
The first minute it will be "test_01.txt"
The second minute "test_02.txt" and so on
I thought (since I know nothing about bash scripting) to write to a third file the
number (01, 02, ...), then read it and append it to the "target" file.
Is this possible? Is there an easier way? I have some experience in C. Can I use C
to achieve this?
A effective solution for this will be creating a deamon by using C program which will sleep for say 1 minute and resume its operation and create the new file name (by setting a counter this can be easily achived) and then copy it to the destination. fork/exec pair can achive all these. Refer Advanced Unix programming by R.Stevens for creation of deamon.
I don't know and I don't want to create a daemon.
Isn't it easier to read the number from the file, append it to the filename,
increase it by one and write it back to the file? Crontab can easily run the script
every minute.
Maybe perl can make things easier?
Hello
I would like some help in making a bash script for copying a file every one minute
from one location to another. I would like the "target" file to have a different
name from the "source" by adding a digit (which increases) in the end of it.
For example I have a file "test.txt"
The first minute it will be "test_01.txt"
The second minute "test_02.txt" and so on
I thought (since I know nothing about bash scripting) to write to a third file the
number (01, 02, ...), then read it and append it to the "target" file.
Is this possible? Is there an easier way? I have some experience in C. Can I use C
to achieve this?
Nice idea!
Unfortunately I don't have python installed (i try 'python script.py' but nothing
happens).
I will give it a try to translate it in bash.
But there is still one problem. I want the file to be copied 24/7. That means that
the number will increase greatly and the loops are going to be longer and longer.
That's why I thought of the seperate file acting as a kind of counter.
Great job! Thank you a lot!
Can I ask for a small correction?
The numbers now are two-digit (01,02,....,10,20). How can I make them 6-digit?
I changed this 'new_num="0"${old_num}' to 'new_num="0000000"${old_num}' but no more digits were added.
Any idea? I just want more digits so when I sort the images to be in the right order.
old="/var/www/cam/webcam.jpg"
base_name=`basename $old .jpg`
dire='/home/lugubrious/webcam_photos/'
old_num=`cat /home/lugubrious/webcam_photos/num.txt`
(( old_num += 1 ))
if [[ $old_num -lt 10 ]]
then
new_num="000000"${old_num}
else
new_num=$old_num
fi
new=${dire}${base_name}"_"${new_num}".jpg"
cp $old $new
echo $old_num >num.txt
Here's the complete script in it's final shape.
It reads and writes in a specific directory and has a 6-digit number appended.
Thanks a lot for you instant help.
Last edited by macabre_sunsets; 09-13-2006 at 12:25 AM.
You need to add extra tests for -lt 100, -lt 1000 etc up to the number of digits you require.
It's a bit of a hack, but pure shell wasn't really designed (originally) for this sort of thing.
You could prob do a clever num format via something like awk or even sed I'd guess.
Personally, if this prog/daemon is going to do much more (or is really part of something else), I'd do it in Perl.
YMMV
.
.
.
Actually, this works
No it is not going to do anything else and is not a part of some other program. I just want to capture
images from my webcam and store them. Maybe perl would be better but since it works it is just fine.
What do you mean about the extra test for -lt 100?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.