Results 1 to 8 of 8

Thread: Bach Script

  1. #1

    Default Bach Script

    Dear all,
    I am bad with bash programming and I need some help how I can make variable names out of a string.

    For example I have a lot of filenames (strings) following the same convention

    m02_+1+7_London_0000$01.cfg
    m02_+1+8_London_0000$01.cfg
    m02_+1+8_London_0000$01.cfg
    m02_+2+7_London_0000$01.cfg
    m02_+2+7_London_0000$01.cfg
    m02_+1+7_London_0000$01.cfg

    What I want to do is to create a script that will interpret the following string and save into variables part of its name
    m02_+1+7_London_0000$01.cfg as
    ------X-Y--City---------

    X=1
    Y=7
    City=London

    after that I want the script to
    mkdir City (this should create a folder called London)
    mkdir City/M'X.Y' (this should create a subfolder called London/M1.7

    then I want to copy the files that go all the files with the same City and X and Y to the same subfolder
    City/MX.Y

    I will need some help start doing that. And I think the first would be to get part of the filenames strings into variables.

    I would like to thank you in advance for your help

    BR
    Alex

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Bach Script

    Well normally I program Mozart scripts, but I'll give this a bash (groan).

    Look into the substring extraction and regular expression features of bash.

    E.g. if $i contains foo2bar3, then ${i:3:1} has the value 2.

  3. #3
    Join Date
    Jan 2009
    Location
    Switzerland
    Posts
    1,529

    Default Re: Bach Script

    You mean something like this:

    Code:
    #!/bin/bash
    #
    # Set base for source dir
    SOURCEDIR="."
    # Set base for destination dir
    BASEDIR="."
    
    FILES=$SOURCEDIR/m02_*.cfg
    for FILE in $FILES ; do
            F=$(basename $FILE)
            echo $F
            X=$(echo $F | cut -d '+' -f 2)
            Y=$(echo $F | cut -d '+' -f 3 | sed -e "s/_.*//")
            CITY=$(echo $F | cut -d '_' -f 3)
    
            if ! test -d $BASEDIR/$CITY/M$X.$Y ; then
                    mkdir -p $BASEDIR/$CITY/M$X.$Y
            fi
    
            cp -a $FILE $BASEDIR/$CITY/M$X.$Y/
    done
    exit 0
    Technology is 'stuff that doesn't work yet.' -- Bran Ferren

  4. #4
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,156

    Default Re: Bach Script

    Dear alaios,

    I see that you get plenty of help here in spite of the fact that you did not ask rthis in the subforum with the name Programming/Scripting under Developement.
    Next time please consider asking your scripting questions there.
    Henk van Velden

  5. #5

    Default Re: Bach Script

    you are divine!

  6. #6

    Default Re: Bach Script

    Quote Originally Posted by hcvv View Post
    Dear alaios,

    I see that you get plenty of help here in spite of the fact that you did not ask rthis in the subforum with the name Programming/Scripting under Developement.
    Next time please consider asking your scripting questions there.
    I am sorry I have missed that! My bad

  7. #7
    Join Date
    Jun 2008
    Location
    Earth - Denmark
    Posts
    10,730

    Default Re: Bach Script

    On 07/13/2011 02:36 PM, ken yap wrote:
    >
    > Well normally I program Mozart scripts


    i only looked in because i so enjoy most of Johann Sebastian's scripts
    (though they are usually called 'works' and not 'scripts').

    --
    DD
    Caveat-Hardware-Software

    openSUSE®, the BMW® of operating systems!


  8. #8
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: Bach Script

    On 2011-07-13 15:36, hcvv wrote:
    >
    > Dear alaios,
    >
    > I see that you get plenty of help here in spite of the fact that you
    > did not ask rthis in the subforum with the name Programming/Scripting
    > under Developement.
    > Next time please consider asking your scripting questions there.


    IMHO, it should be moved, lest it gets long.

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 11.4 x86_64 "Celadon" at Telcontar)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •