LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 06-06-2006, 05:36 PM   #1
adamuk
LQ Newbie
 
Registered: Mar 2004
Posts: 10

Rep: Reputation: 0
Question shell scripting


Hello folks.
Long time no post. Ive googled and googled but this is all to new for me so im kind of not entirely sure what i need to.

Anyways. I manage a HLTV server which records demos of half life based games. Linux based (Fedora)

The server records them and puts them in a .dem extension here is an example of the file names it outputs the recordings to

x-0606061517-dod_saints.dem
x-0606061542-dod_saints.dem
(x is what we give, the nubmers are a timestamp and the dod_saints is a map name) for those that dont know what I am on about its a recording a match on a computer game called Day of Defeat

when the recordings have completed its a right pain a the but to do zip them up, move them to web server etc. What i want to do is to build a script to do it for me and at the same time learn some .sh scripting.

here is what i want to do
1. script searches /hltv/dod for *.dem and returns the folder. 2
2. I then want one of the files to be returned into a variable (ill explain why)
3. I the want to move the results of the find to /wwwdocs/13-demos
4. Then want to zip up the .dem files in /wwwdocs/13-demos and name the zip from the name in the variable i declared in step 2 so i end up with this: /wwwdocs/13-demos/x-0606061542-dod_saints.dem.zip
5. I then want to delete the .dem files in /wwwdocs/13-demos and /hltv/dod

now I can do all this manually but putting it into a script is a little beyond me

Now im not looking for people to code this for me or anything. Linux shell scripting is as new to me as meat is to bugs bunny. Im looking for decent online resources, code hints, tips etc how I can do this.

I can put everything into cron, I can do the 5 steps manually. Its just how do I loop through the file folders. see if *.dem can be found, put the last one returned into a variable etc

Thanks for your time and efforts to help me with this.
Also where do i go to make a donation here? Ive asked questions in the past but never returned very much back. I rarley have time to post on my own websites.

ahhh never mind about the donations. I found it. Hope it helps.

Last edited by adamuk; 06-06-2006 at 05:39 PM.
 
Old 06-06-2006, 06:23 PM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Just to get you started, you'll probably want to use something like the following loop:
Code:
#! /bin/bash
for FILENAME in $(find /hltv/dod -type f -name "*.dem")
do
    echo "Found $(basename $FILENAME) at $(dirname $FILENAME)"
done
Let's see how far you get, and just go ahead asking more specific questions if things don't work out.
 
Old 06-06-2006, 07:39 PM   #3
eltbee
LQ Newbie
 
Registered: Feb 2006
Posts: 2

Rep: Reputation: 0
Applaud your efforts but let me encourage you to skip the shell scripting and go straight for perl or python. It's a lot easier, more fun, and you'll make more money probably. I know lots of sysadmins who are "still" trying to learn "shell scripting", whatever that is. The rest of us have moved on.

So building on what spirit receiver said, have a look at the output from: find2perl /hltv/dod -type f -name "*.dem" and then delve into the ever present and extremely useful File::Find module. You will have something robust built in no time.

Last edited by eltbee; 06-06-2006 at 07:41 PM.
 
Old 06-07-2006, 01:49 AM   #4
adamuk
LQ Newbie
 
Registered: Mar 2004
Posts: 10

Original Poster
Rep: Reputation: 0
Many thanks for your replies

spirit receiver: that has been a fantastic help and has given me a good start to acheive my target

eltbee: I never really thought about perl/python to be honest for this task. Ive looked at perl in the past for web use, but didnt get very far. I will have a search about and see if its going to be better use to me.

i got to go off to work now (oh joy) so ill have a play tonight

Thanks again!
 
Old 06-07-2006, 04:07 PM   #5
adamuk
LQ Newbie
 
Registered: Mar 2004
Posts: 10

Original Poster
Rep: Reputation: 0
Talking

spirit receiver: Thank you so much for that little snippit.

I have a working script now that does everything I need. Addmitingly its pretty bastic at the moment. But it works like a charm!

Here is my final code
Code:
#! /bin/bash

#ok then lets clear the screen. make it look nice
clear


#lets decoare our directories to work with
current_demo_location="/hltv/dod/"
move_demos_to="/demos2/"
web_server_path="/wwwdocs/13-demos"

#loop through the folder and check for .dem files
for FILENAME in $(find $current_demo_location -type f -name "*.dem")
do

    #just tell the user whats been found
    echo "Found $(basename $FILENAME) at $(dirname $FILENAME)"

    #lets move the file for zipping
    mv $(dirname $FILENAME)/$(basename $FILENAME) $move_demos_to

    #declare the variable to name the zip
    demo=$(basename $FILENAME)

#terminate the loop
done

echo "

++++++++++++++++++++++++++++++
The finall zip will be called $demo.zip
++++++++++++++++++++++++++++++

"

#lets change directory to where the demos are
cd $move_demos_to


#lets zip the files
zip $demo.zip *.dem

echo "
++++++++++++++++++++++++
ALL DONE!

Unless you saw any errors above your demos are zipped and posted to the webserver. The file is called $demo.zip

+++++++++++++++++++++++
"
#lets delete the demos from the zip
rm -rf ./*.dem

#lets move the .zip to the webserver
mv $demo.zip $web_server_path/$demo.zip
It probably breaks every rule in the coding law book. but it does the job

i can only get better now.
 
Old 06-08-2006, 03:04 AM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Congratulations, looks fine to me. I only want to mention that special characters (like whitespace) in filenames will break your script, you could change that using the internal variable IFS and by placing double quotes around the names where appropriate.
 
  


Reply

Tags
move, search, zip


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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
Shell Scripting kidestranged Linux - General 5 07-27-2004 11:46 PM
Shell Scripting - Need some help The Grepper Programming 1 03-29-2002 04:10 PM


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

Main Menu
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