DOS Games Message Board
DOS Games Discussion
A message board to talk about DOS games, get help, and find old games.
Do NOT post warez or abandonware requests please
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Scripts and stuff

 
Post new topic   Reply to topic    DOS Games Discussion Forum Index -> Game Creation
Author Message
wardrich
lawl catz r lawlz
lawl catz r lawlz


Offline
Joined: 14 Sep 2002
Posts: 3361
Location: Ontario Canada

PostPosted: Wed Jun 13, 2007 6:26 pm     Post subject: Scripts and stuff Reply with quote

Since there are scattered posts all over the place about script files, I think it would be best to keep them all confined here from now on. Just to make it easier to follow it all. I'd split the other topics, but then all the posts and stuff would seem a bit out of place.
Back to top
wardrich
lawl catz r lawlz
lawl catz r lawlz


Offline
Joined: 14 Sep 2002
Posts: 3361
Location: Ontario Canada

PostPosted: Wed Jun 13, 2007 8:14 pm     Post subject: Reply with quote

I'm currently trying to work on one that looks at all the files in $PWD and sorts them into ./music/ and ./pictures/ folders depending on their filetypes... And since I'm lazy and don't usually add extensions to my image files, it makes my life that much more fun. lol

Code:

#/bin/bash
#Scattered Media Sorter Upper

if [ $(file "$i" | grep "image data") ]; then
   mv "$i" pictures
fi

if [ $(file "$i" | grep "Audio file") ]; then
   mv "$i" music
fi


I still have to add in the checks to see if ./music/ and ./pictures/ exists... and to make them if they don't.
Back to top
MiniMax
Expert
Expert


Offline
Joined: 25 Jan 2007
Posts: 165
Location: Stockholm, Sweden

PostPosted: Thu Jun 14, 2007 7:11 pm     Post subject: Reply with quote

That is some bad coding there. Better use a case-construct:

Code:
case "$(file "$i")" in

*"image data"* | *"bitmap data"* )
    mv "$i" pictures/
    ;;

*"Audio file"* )
    mv "$i" music/
    ;;

esac


Any fool can write code a computer can understand. It takes an expert to write code humans can understand
Back to top
wardrich
lawl catz r lawlz
lawl catz r lawlz


Offline
Joined: 14 Sep 2002
Posts: 3361
Location: Ontario Canada

PostPosted: Thu Jun 14, 2007 8:51 pm     Post subject: Reply with quote

Yeah, thanks MiniMax. My script still wasn't working, so I knew stuff had to be done. I'm a total newb to Bash scripts. Thanks for the help Happy.
Back to top
MiniMax
Expert
Expert


Offline
Joined: 25 Jan 2007
Posts: 165
Location: Stockholm, Sweden

PostPosted: Fri Jun 29, 2007 3:16 am     Post subject: A useless script I wrote: Reply with quote

A useless script I wrote:
Code:
        @echo off

rem     +====================================================================
rem     | UDm Rerunner - a script to automatically rerun aborted work-units.
rem     |
rem     | @author MiniMax.
rem     +====================================================================
       
        set version=1.0.1 (2006-04-20).

rem     +--------------------------------------------------------------------
rem     | This package needs the new CMD.EXE extensions to be loaded and
rem     | will refuse to run, if they can not be enabled.
rem     +--------------------------------------------------------------------

        verify other 2>NUL:
        setLocal enableExtensions
        if errorLevel 1 (
            echo.
            echo *ERROR*
            echo.
            echo Sorry - this program requires that extensions to CMD.EXE are
            echo enabled.  Please consult your manual for instruction on how
            echo to proceed.

            goto :EOF
        )

        goto main
        goto :EOF

rem     +====================================================================
rem     | help - display help text with usage examples.
rem     +====================================================================

:help begin

        echo UDm_rerunner               - Monitor and rerun aborted work-units.
        echo UDm_rerunner /h            - Display help (this text).
        echo UDm_rerunner /v            - Display version number.
        echo UDm_rerunner /c            - Display version number and configuration.
        echo UDm_rerunner /r work-unit  - Re-run a work-unit.
        echo UDm_rerunner /t            - Test log parser.
        echo.
        echo Examples:
        echo.
        echo To monitor and automatically rerun aborted work-units:
        echo.
        echo.  UDm_rerunner
        echo.
        echo To rerun a specific aborted work-unit from a backup:
        echo.
        echo.  UDm_rerunner /r 8606108
:help end
        goto :EOF

rem     +====================================================================
rem     | main - parse command line options, define and check file paths,
rem     |        perform requested actions.
rem     +====================================================================

:main begin( [/? | /h | /v | /r work-unit )
        title UDm Rerunner

        if    "%~1" == "/?" call :help    && goto :EOF
        if /i "%~1" == "/h" call :help    && goto :EOF
        if /i "%~1" == "/v" call :version && goto :EOF

        call :setup || goto error_exit

        if /i "%~1" == "/c" call :config  && goto :EOF

        set file2check=%UDa_dir%\UD.exe
        if not exist "%file2check%" goto xno_file
       
        set dir2check=%UDm_backup_dir%
        if not exist "%dir2check%" goto xno_dir

        set dir2check=%UDm_dir%\UDm Logs
        if not exist "%dir2check%" goto xno_dir

rem     +--------------------------------------------------------------------
rem     | Call the config routine to display the settings picked up by the
rem     | setup routine.
rem     +--------------------------------------------------------------------

        call :config

        set wu_log_cur=%UDm_dir%\UDm Logs\WUs.txt
        set wu_log_old=%Temp%\detect_WUs.txt

rem     +--------------------------------------------------------------------
rem     | If called with /t run a test of the log parsing code, listing all
rem     | the numbered work-units from %wu_log_cur%.
rem     +--------------------------------------------------------------------

        if "%~1" == "/t" (
            call :testcfg
            goto :EOF
        )

rem     +--------------------------------------------------------------------
rem     | If called with /r nnnnnn only restore and re-run the work-unit.
rem     | Else initialise the baseline log file, and start the detection
rem     | loop.
rem     +--------------------------------------------------------------------

        if "%~1" == "/r" (
            call :rerun %2
            goto :EOF
        )

rem     +--------------------------------------------------------------------
rem     | Every 60 seconds, the detection routine compares the current
rem     | log file (WUs.txt) with an older (temporary) copy.
rem     |
rem     | If a difference is found, it is either due to a new completed
rem     | work-unit, or a new aborted WU.  If it is aborted work-unit,
rem     | it is automatically rerun by the detection routine.
rem     +--------------------------------------------------------------------

        copy "%wu_log_cur%" "%wu_log_old%" > NUL:
        echo Monitoring %wu_log_cur% for aborted work-units.
:loop1
        call :sleep 60
        call :detect
        goto loop1

:main end
        goto :EOF

rem     +====================================================================
rem     | detect - monitor the UDm log for aborted work-units, and rerun if
rem     |          found.
rem     +====================================================================

:detect begin
rem     +--------------------------------------------------------------------
rem     | Do a File Compare of the current log and the old, temporary copy.
rem     |
rem     | errorlevel >= 2 means a real error occured.  By repeating the FC-
rem     |                 command with the same arguments, we get a chance to
rem     |                 see the error message.
rem     |
rem     | errorlevel >= 1 means there was a difference in the logs.
rem     |
rem     | errorlevel == 0 means no difference.
rem     +--------------------------------------------------------------------

        fc "%wu_log_cur%" "%wu_log_old%" > NUL:

        if errorlevel 2 (
            fc "%wu_log_cur%" "%wu_log_old%"
            goto error_exit
        )

        if errorlevel 1 (
rem         +----------------------------------------------------------------
rem         | Repeat the FC, but only show the user the lines containing
rem         | completions and abortions events (if that is what caused the
rem         | difference.
rem         +----------------------------------------------------------------

            fc   "%wu_log_cur%" "%wu_log_old%" | findstr /c:"%compl_str%" /c:"%abort_str%"

rem         +----------------------------------------------------------------
rem         | Extract the abortion event(s) into a temporary file (later the
rem         | work-unit number is extracted from this file).  After that is
rem         | done, we can safely update the old log file.
rem         +----------------------------------------------------------------

            fc   "%wu_log_cur%" "%wu_log_old%" | findstr /c:"%abort_str%" > "%Temp%\detect_WUa.log"
            copy "%wu_log_cur%" "%wu_log_old%" > NUL:

rem         +----------------------------------------------------------------
rem         | Extract the work-unit number from the log record, and pass the
rem         | number to the rerun routine.  It is a tricky procedure, since
rem         | the date/timestamp part of the log record can differ depending
rem         | on regional/language settings.
rem         |
rem         | The parser uses '(' and ')' as delimiters, to pick out the
rem         | work-unit number from log-lines like this:
rem         |
rem         | 1111111111111111111111111111111111 2222222 3333333333333 44444444444444 5555555555555
rem         | ----------------------------------+-------+-------------+--------------+-------------
rem         | 4/18/2006 2:53:04 PM    WU Aborted(slot #9)     8606251 (162 KB >> 0 KB)        AUR-B
rem         | 18-04-2006 14:53:04     WU Aborted(slot #9)     8606251 (162 KB >> 0 KB)        AUR-B
rem         |
rem         | The extra for-W-loop is used to strip off the preprended spaces
rem         | from the work-unit number.
rem         +----------------------------------------------------------------

            for /f "usebackq delims=() tokens=3" %%I in (
                "%Temp%\detect_WUa.log"
            ) do for %%W in (%%I) do (
                echo.
                echo Found an aborted work-unit: %%W.
                echo Waiting 5 minutes to allow the UD-agent to complete its send/receive cycle.
                call :sleep 300
                echo Re-running work-unit %%W.
                echo.
                call :rerun %%W || goto error_exit
                echo.
            )
        )

:detect end
        goto :EOF

rem     +====================================================================
rem     | rerun - close the UD agent, find backup of aborted work-unit,
rem     |         restore work-unit, and restart UD agent.
rem     +====================================================================

:rerun begin
rem     +--------------------------------------------------------------------
rem     | Start by closing the agent.  Otherwise we risk the agent changing
rem     | the backup files under our nose.
rem     +--------------------------------------------------------------------

        set wu=%1
:loop2
        echo Stopping the UD agent...
        "%UDm_dir%\ud_con.exe" -project ud -close || goto error_exit
        call :sleep 30

        if exist "%tlist_exe%" (
            "%tlist_exe%" | find "ud_ligfit_Release.exe" && goto loop2
        )

        echo The UD agent has been stopped.
        echo.

rem     +--------------------------------------------------------------------
rem     | Scan through the backup slots (1-99).  Newest slot is always no. 1.
rem     | Note: If there is more than 99 backups, then we are screwed.
rem     +--------------------------------------------------------------------

        for /l %%I in (1,1,99) do if exist "%UDm_backup_dir%\%%I\ud_%wu%" (
            call :slot_info "%UDm_backup_dir%\%%I"
            goto slot_found
        )

        echo No backup slot for WU %wu% was found in "%UDm_backup_dir%".
        goto restart

:slot_found
        echo Newest backup slot for WU %wu% is #%slot_num% (%slot_time%).

        echo Restoring WU from backup slot #%slot_num%...

        "%UDm_dir%\ud_con.exe" -project ud -clientfilename "%UDa_exe%" -backupbasepath "%UDm_backup_dir%" -restore %slot_num%

rem     +--------------------------------------------------------------------
rem     | If the restore was successful, the requested work-unit should exist
rem     | in the main UD-agent directory.
rem     +--------------------------------------------------------------------

        if not exist "%UDa_dir%\ud_%wu%" (
            echo Failed to restore WU %wu% to %UDa_dir%!
            echo %UDa_dir%\ud_%wu% does not exist.
        )

:restart
        echo Restarting the UD agent...

rem     +--------------------------------------------------------------------
rem     | I can not get the ud_con.exe program (v. 4.45 beta 1) to start the
rem     | UD-agent. so as a work-around I use the Windows START command.
rem     |
rem     | "%UDm_dir%\ud_con.exe" -project ud -clientfilename "%UDa_exe%" -run-hidden
rem     +--------------------------------------------------------------------

        start "" /b "%UDa_exe%" || goto error_exit
        call :sleep 30

        if exist "%tlist_exe%" (
            "%tlist_exe%" | find "ud_ligfit_Release.exe" > NUL: || goto restart
        )

        echo The UD agent has been started.
        echo.

:rerun end
        goto :EOF

rem     +====================================================================
rem     | slot_info - delayed evaluation routine for retrieving information
rem     |             about an UD Monitor backup slot.
rem     |
rem     | @param  1     Path to backup slot (a directory path).
rem     +====================================================================

:slot_info
        set slot_path=%1
        set slot_num=%~n1
        set slot_time=%~t1
        goto :EOF

rem     +====================================================================
rem     | psearch - search for a program along the PATH.
rem     |
rem     | @param  1     Name of file (EXE, CMD, ...) to find.
rem     +====================================================================

:psearch begin
        set pspath=%~$PATH:1
        echo 1. %pspath%
:psearch end
        goto :EOF

rem     +====================================================================
rem     | testcfg - tests the parsing of the UD Monitor log.
rem     +====================================================================

:testcfg begin
rem     +--------------------------------------------------------------------
rem     | The parser uses '(' and ')' as delimiters, to pick out the
rem     | work-unit number from log-lines like this:
rem     |
rem     | 1111111111111111111111111111111111 2222222 3333333333333 44444444444444 5555555555555
rem     | ----------------------------------+-------+-------------+--------------+-------------
rem     | 4/18/2006 2:53:04 PM    WU Aborted(slot #9)     8606251 (162 KB >> 0 KB)        AUR-B
rem     | 18-04-2006 14:53:04     WU Aborted(slot #9)     8606251 (162 KB >> 0 KB)        AUR-B
rem     |
rem     | The extra for-W-loop is used to strip off the preprended spaces
rem     | from the work-unit number.
rem     +--------------------------------------------------------------------

        for /f "usebackq delims=() tokens=3" %%I in (
            "%wu_log_cur%"
        ) do for %%W in (%%I) do (
            echo :%%W:
        )
:testcfg end
        goto :EOF

rem     +====================================================================
rem     | setup - initialise some key variables.
rem     |
rem     | NOTE: The defaults maybe overriden by creating a UDm_rerunner.cfg
rem     |       file (in the same directory as UDm_rerunner.cmd).  This file
rem     |       is sourced into the UDm_rerunner.cmd script.  If UD Monitor
rem     |       is installed in E:\My Programs\UDmon, a line of
rem     |
rem     |               UDm_dir=E:\My Programs\UDmon
rem     |
rem     |       will do the trick.
rem     +====================================================================

:setup begin
rem     +--------------------------------------------------------------------
rem     | UDm_dir       - Location where UD Monitor is installed.
rem     |                 Once we know that, we can fetch a lot of information
rem     |                 from the ud_mon.ini file, including the path to the
rem     |                 UD-agent, and the backup slots.
rem     |
rem     | abort_str     - If this string is found in the log file, the WU has
rem     |                 aborted.
rem     |
rem     | compl_str     - If this string is found in the log file, the WU has
rem     |                 completed.
rem     |
rem     | tlist_exe     - Program to list running windows programs.
rem     +--------------------------------------------------------------------

        set UDm_dir=%ProgramFiles%\UD Monitor

        set abort_str=WU Aborted
        set compl_str=WU Completed

        set tlist_exe=%SystemRoot%\system32\tasklist.exe

rem     +--------------------------------------------------------------------
rem     | Scan the optional configuration file, and execute each line as
rem     | a variable assignment.
rem     +--------------------------------------------------------------------

        if exist "UDm_rerunner.cfg" (
            for /f "usebackq tokens=*" %%I in ("UDm_rerunner.cfg") do SET %%I
        )

        set file2check=%UDm_dir%\ud_mon.ini
        if not exist "%file2check%" goto xno_file

rem     +--------------------------------------------------------------------
rem     | Scan the ud_mon.ini file for the location of the UD-agent, and the
rem     | backup slots.
rem     +--------------------------------------------------------------------

        for /f "usebackq tokens=1,2* delims==" %%I in ("%UDm_dir%\ud_mon.ini") do (
            if "%%I" == "UD"            set UDa_exe=%%J
            if "%%I" == "UD"            set UDa_dir=%%~dpJ
            if "%%I" == "Location"      set UDm_backup_dir=%%J
        )

rem     +--------------------------------------------------------------------
rem     | Get rid of the trailing backslash in the UD-agents directory name.
rem     +--------------------------------------------------------------------

        set UDa_dir=%UDa_dir:~0,-1%

        if "%UDm_backup_dir%" == "" set UDm_backup_dir=%UDa_dir%\UDm Backups

:setup end
        goto :EOF

rem     +====================================================================
rem     | sleep - pause execution for approximately N seconds.
rem     |
rem     | @param  1     Number of seconds to pause.
rem     +====================================================================

:sleep begin
rem     +--------------------------------------------------------------------
rem     | Each ping is repeated N times with a 1 second delay.
rem     +--------------------------------------------------------------------

        ping -n %1 127.0.0.1 > NUL:
:sleep end
        goto :EOF

rem     +====================================================================
rem     | version - display program name and version string.
rem     +====================================================================

:version
        echo UDm Rerunner %version%
        goto :EOF

rem     +====================================================================
rem     | config - display program name, version information, and setup
rem     |          information.
rem     +====================================================================

:config
        call :version
        echo.
        echo UD Agent   directory = "%UDa_dir%"
        echo UD Monitor backups   = "%UDm_backup_dir%"
        echo UD Monitor logs      = "%UDm_dir%\UDm Logs"
        echo.
        echo Task Lister          = "%tlist_exe%"
        echo.
        goto :EOF

rem     +====================================================================
rem     | error_exit - force errorlevel = 1.
rem     +====================================================================

:error_exit
        verify other 2>NUL:
        goto :EOF

:echo
        echo %~1
        goto :EOF

:xno_file
        echo File %file2check% does not exist.
        goto error_exit

:xno_dir
        echo Directory %dir2check% does not exist.
        goto error_exit
Back to top
MiniMax
Expert
Expert


Offline
Joined: 25 Jan 2007
Posts: 165
Location: Stockholm, Sweden

PostPosted: Wed Aug 08, 2007 7:49 pm     Post subject: Reply with quote

My awesome Podcast downloader:

Code:
#!/bin/sh

cd "/C/Spool/Podcasts"

get_podcast() # rss.url
{
    [ -r "$1" -a -f "$1" ] || return 1

    dir="`dirname "$1"`"
    file="`basename "$1"`"

    printf 'Doing %s\n' "${dir}"

    (   cd "${dir}"
        curl -q --config ../rss.curl --config "${file}" > "rss.xml"

        sed -n 's#<link>\(.*\.mp3\)</link>.*#\1#p' "rss.xml" |
        while read url; do
            mp3="`basename "${url}"`"
            grep -q "^${mp3}\$" "history.txt" && continue
            printf 'Getting %s\n' "${mp3}"
            if curl -q --config ../podcasts.curl "${url}"; then
                printf '%s\n' "${mp3}" >> "history.txt"
            else
                printf 'Removing %s\n' "${mp3}"
                rm "${mp3}"
            fi
        done
    )
}

case $# in
0 ) for f in */rss.curl; do get_podcast "$f"; done ;;
* ) for f in "$@";       do get_podcast "$f"; done ;;
esac

printf '\nTodays podcasts:\n'

find . -type f -name '*.mp3' -mtime -1
Back to top
Display posts from previous:   
Post new topic   Reply to topic    DOS Games Discussion Forum Index -> Game Creation All times are GMT - 5 Hours
Page 1 of 1

 
Free Windows games


Powered by phpBB © 2001, 2005 phpBB Group and Ask an Expert
Also powered by Darren and the other admin and mods' hard work! ;)