LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell scripting (https://www.linuxquestions.org/questions/programming-9/shell-scripting-452232/)

adamuk 06-06-2006 05:36 PM

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.

spirit receiver 06-06-2006 06:23 PM

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.

eltbee 06-06-2006 07:39 PM

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.

adamuk 06-07-2006 01:49 AM

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 :D

Thanks again!

adamuk 06-07-2006 04:07 PM

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 :D

i can only get better now.

spirit receiver 06-08-2006 03:04 AM

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.


All times are GMT -5. The time now is 12:52 PM.