bash, variables and a for loop


Results 1 to 8 of 8

Thread: bash, variables and a for loop

  1. #1
    Join Date
    Jan 2003
    Posts
    1,012

    bash, variables and a for loop (SOLVED)

    Writing a script to do the following;

    • download some tarballs
    • use the tarball names to create a name list
    • use name list as part of a path
    • use sed to update specific variables in another script

    Here is what I have so far;

    Code:
    #!/bin/bash --verbose
      set -x
      NEW_KDE=/home/dveatch/kde-update
      OLD_VER="4.7.4"
      NEW_VER="4.8.0"
      SOURCE_URL=ftp://ftp.kde.org/pub/kde/stable/${OLD_VER}/src
      FILE_LIST=`ls -1 *.bz2`
      MODULE_NAME=`ls *.bz2 | sed -e "s:-${OLD_VER}.tar.bz2::"`
      MODULE_LOCATION=`lvu where $MODULE_NAME`
      MODULE_PATH="/home/dveatch/moonbase.git/$MODULE_LOCATION/$MODULE_NAME"
    
      if [ ! -d $NEW_KDE ] ; then
         mkdir -p $NEW_KDE
         cd $NEW_KDE
         wget -c $SOURCE_URL/$FILE_LIST
       else
         cd $NEW_KDE
         wget -nc -cq $SOURCE_URL/$FILE_LIST
      fi
    
      for i in $MODULE_PATH
      do
      sed -i "s:${OLD_VER}:${NEW_VER}:" "$i"/DETAILS
      done
    
      set +x
    The problem is this; it will properly sed the first entry in $MODULE_PATH. In this case it is blinken and the output looks like this;

    Code:
    + for i in '$MODULE_PATH'
    + sed -i s:4.7.4:4.8.0: /home/dveatch/moonbase.git/kde4/games/blinken/DETAILS
    However it doesn't do that for the rest of the items in $MODULE_PATH throwing errors like this;

    Code:
    + for i in '$MODULE_PATH'
    + sed -i s:4.7.4:4.8.0: cantor/DETAILS
    sed: can't read cantor/DETAILS: No such file or directory
    Obviously $MODULE_PATH is not being read correctly but for the life of me the brain is not pivoting around to cypher how to make it get to use the path right for the remaining items.

    In case your wondering, I have a system command called "lvu" and when used like this; lvu where $MODULE, it will output this (using smokeqt as an example;

    Code:
    lvu where blinken
    kde4/development
    Last edited by stumbles; 01-17-2012 at 08:25 AM. Reason: Marking as solved
    You can tuna piano, but you can't tune a fish.

    http://www.lunar-linux.org/
    It's worth the spin.

    http://www.pclinuxos.com/page.php?7
    Puts the rest to shame.

  2. #2
    Join Date
    Sep 1999
    Location
    Cambridge, UK
    Posts
    509
    You already have the right idea by running the script in verbose mode so you can see how the variables are being expanded and what is happening. Look again at MODULE_NAME and MODULE_PATH.

    Hint: MODULE_NAME ends up containing a space-separated list of names. That space-separated list is appended to the base path to form MODULE_PATH. This happens before you do the loop.

  3. #3
    Join Date
    Jan 2003
    Posts
    1,012
    Quote Originally Posted by furrycat View Post
    You already have the right idea by running the script in verbose mode so you can see how the variables are being expanded and what is happening. Look again at MODULE_NAME and MODULE_PATH.

    Hint: MODULE_NAME ends up containing a space-separated list of names. That space-separated list is appended to the base path to form MODULE_PATH. This happens before you do the loop.
    Good you point that out and is where I am falling down. If;
    Code:
    MODULE_NAME=`ls *.bz2 | sed -e "s:-4.7.4.tar.bz2::"`
    is run from cli, it gives (snipped);
    Code:
    ...
    blinken
    cantor
    gwenview
    kalgebra
    kalzium
    kamera
    kanagram
    kate...
    But from the script it is as you say a space separated list is created.

    Oops, think I see the problem now. Your right about a space separation problem. Here is what $FILE_LIST looks like;

    Code:
    + FILE_LIST='blinken-4.7.4.tar.bz2
    cantor-4.7.4.tar.bz2
    gwenview-4.7.4.tar.bz2
    kalgebra-4.7.4.tar.bz2
    kalzium-4.7.4.tar.bz2
    kamera-4.7.4.tar.bz2
    kanagram-4.7.4.tar.bz2
    kate-4.7.4.tar.bz2
    kbruch-4.7.4.tar.bz2
    kcolorchooser-4.7.4.tar.bz2
    kdeaccessibility-4.7.4.tar.bz2
    kdeadmin-4.7.4.tar.bz2
    Here is how $MODULE_NAME is shown;

    Code:
    + MODULE_NAME='blinken
    cantor
    gwenview
    kalgebra
    kalzium
    kamera
    kanagram
    kate
    kbruch
    kcolorchooser
    kdeaccessibility
    kdeadmin
    kdeartwork
    kde-baseapps
    kdegames
    kdegraphics-strigi-analyzer
    But this area here in $MODULE_LOCATION is where it falls down, it looks like this;

    Code:
      MODULE_LOCATION=`lvu where $MODULE_NAME`
    lvu where $MODULE_NAME
    ++ lvu where blinken cantor gwenview kalgebra kalzium kamera kanagram kate kbruch kcolorchooser kdeaccessibility kdeadmin kdeartwork kde-baseapps kdegames kdegraphics-strigi-analyzer kdegraphics-thumbnailers kdelibs kdemultimedia kdenetwork kdepim kdepimlibs kdepim-runtime kdeplasma-addons kde-runtime kdesdk kdetoys kdeutils kde-wallpapers kdewebdev kde-workspace kgamma kgeography khangman kig kimono kiten klettres kmplot kolourpaint konsole korundum kross-interpreters kruler ksaneplugin ksnapshot kstars ktouch kturtle kwordquiz libkdcraw libkdeedu libkexiv2 libkipi libksane marble mobipocket okular oxygen-icons parley perlkde perlqt pykde4 qtruby qyoto rocs smokegen smokekde smokeqt step svgpart
    and then it proceeds with;

    Code:
    + MODULE_LOCATION=kde4/games
      MODULE_PATH="/home/dveatch/moonbase.git/$MODULE_LOCATION/$MODULE_NAME"
    + MODULE_PATH='/home/dveatch/moonbase.git/kde4/games/blinken
    cantor
    gwenview
    kalgebra
    kalzium
    kamera
    kanagram
    kate
    kbruch
    and the names in that list live in different directories. So I need to figure how to get lvu where $MODULE to output differently.
    You can tuna piano, but you can't tune a fish.

    http://www.lunar-linux.org/
    It's worth the spin.

    http://www.pclinuxos.com/page.php?7
    Puts the rest to shame.

  4. #4
    Join Date
    Jan 2003
    Posts
    1,012
    Solved the problem with `lvu where $MODULE_NAME`; double quoting $MODULE_NAME now gives;

    Code:
      MODULE_LOCATION=`lvu where "$MODULE_NAME"`
    lvu where "$MODULE_NAME"
    ++ lvu where 'blinken
    cantor
    gwenview
    kalgebra
    kalzium
    kamera
    kanagram
    kate
    kbruch
    kcolorchooser
    kdeaccessibility
    kdeadmin
    .... snipped
    But that gives me a new problem;
    Code:
    sed: can't read /home/dveatch/moonbase.git/kde4/games/blinken
    cantor
    gwenview
    kalgebra
    kalzium
    kamera
    ....snipped
    
    qyoto
    rocs
    smokegen
    smokekde
    smokeqt
    step
    svgpart/DETAILS: File name too long
    So is there a way to jiggle the for loop or do I need to use another method?
    You can tuna piano, but you can't tune a fish.

    http://www.lunar-linux.org/
    It's worth the spin.

    http://www.pclinuxos.com/page.php?7
    Puts the rest to shame.

  5. #5
    Join Date
    Sep 1999
    Location
    Cambridge, UK
    Posts
    509
    I'd loop on MODULE_NAME and move MODULE_LOCATION and MODULE_PATH inside the loop.

  6. #6
    Join Date
    Jan 2003
    Posts
    1,012
    Quote Originally Posted by furrycat View Post
    I'd loop on MODULE_NAME and move MODULE_LOCATION and MODULE_PATH inside the loop.
    Hmm, well this is what the for loop looks like now;

    Code:
      for i in "$MODULE_NAME"
      do
      MODULE_NAME=`ls -1 *.bz2 | sed -e "s:-${OLD_VER}.tar.bz2::"`
      MODULE_LOCATION=`lvu where "$MODULE_NAME"`
      MODULE_PATH="/home/dveatch/moonbase.git/$MODULE_LOCATION"
      sed -i "s:${OLD_VER}:${NEW_VER}:" $MODULE_PATH/$i/DETAILS
      done
    And it produces this;

    Code:
    lvu where "$MODULE_NAME"
    ++ lvu where 'blinken
    cantor
    gwenview
    kalgebra
    kalzium
    kamera
    kanagram
    ...
    snipped
    ...
    smokegen
    smokekde
    smokeqt
    step
    svgpart'
    + MODULE_LOCATION=kde4/games
    + MODULE_PATH=/home/dveatch/moonbase.git/kde4/games
    + sed -i s:4.7.4:4.8.0: /home/dveatch/moonbase.git/kde4/games//DETAILS
    sed: can't read /home/dveatch/moonbase.git/kde4/games//DETAILS: No such file or directory
    
      set +x
    + set +x
    Not at all sure why it is not picking up the module name.
    You can tuna piano, but you can't tune a fish.

    http://www.lunar-linux.org/
    It's worth the spin.

    http://www.pclinuxos.com/page.php?7
    Puts the rest to shame.

  7. #7
    Join Date
    Sep 1999
    Location
    Cambridge, UK
    Posts
    509
    MODULE_NAME has to stay outside the loop. Inside the loop your variable is i so you need to use that in place of MODULE_NAME when setting the other MODULE_* stuff.

  8. #8
    Join Date
    Jan 2003
    Posts
    1,012

    bash, variables and a for loop (SOLVED)

    Quote Originally Posted by furrycat View Post
    MODULE_NAME has to stay outside the loop. Inside the loop your variable is i so you need to use that in place of MODULE_NAME when setting the other MODULE_* stuff.
    hu hu hurray!

    Code:
      FILE_LIST=`ls -1 *.bz2`
      SHA1SUM=`sha1sum -b $FILE_LIST | cut -d* -f1`
      MODULE_NAME=`ls -1 *.bz2 | sed -e "s:-${OLD_VER}.tar.bz2::"`
      DATE=`date -u +%Y%m%d`
      set -x
      for i in $MODULE_NAME
      do
      MODULE_LOCATION=`lvu where $i`
      MODULE_PATH="/home/dveatch/moonbase.git/$MODULE_LOCATION"
      sed -i "s:${OLD_VER}:${NEW_VER}:" $MODULE_PATH/$i/DETAILS
      sed -i 's/UPDATED=/UPDATED=${DATE}/' $MODULE_PATH/$i/DETAILS
      sed -i 's/\(SOURCE_VFY=sha1:*\).*/SOURCE_VFY=sha1:"$SHA1SUM"/' $MODULE_PATH/$i/DETAILS
      done
    That is what is looks like now and it works. Well except for those last twp sed lines.; they some twiddling.

    The UPDATE sed is supposed to delete all characters after the "=" and replace it with $DATE.

    The SOURCE_VFY sed is supposed to replace the sha1sum after "sha1:".

    Maybe I should make a new thread about how to sed after a certain number of characters or some such.

    Thanks for your pointers, it helped a lot.
    Last edited by stumbles; 01-17-2012 at 08:24 AM. Reason: Marking as solved
    You can tuna piano, but you can't tune a fish.

    http://www.lunar-linux.org/
    It's worth the spin.

    http://www.pclinuxos.com/page.php?7
    Puts the rest to shame.

Posting Permissions

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