Find the answer to your Linux question:
Results 1 to 2 of 2
Hi there - I have some code I re-use quite often in a script and I was hoping to either define it as a function or source it in... now ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2004
    Posts
    10

    BASH: breaking apart commands and reassembling them...


    Hi there - I have some code I re-use quite often in a script and I was hoping to either define it as a function or source it in... now the problem is I need some custom code in the middle of it to perform a different function...

    For example the assembled code would be:

    Code:
    let "CNT=0"
    let "CNT++"
    while [ "$CNT" -le "$DOMCNT" ] ; do
            printf "%q" "DOMS=" >amu.tmp
            printf "$" >>amu.tmp
            printf "%q" "DOM$CNT" >>amu.tmp
            source amu.tmp
            rm -f amu.tmp
            CUSTOM COMMAND
    let "CNT++"
    done
    Now i have tried breaking it into 2 functions like:

    Code:
    function start() {
    let "CNT=0"
    let "CNT++"
    while [ "$CNT" -le "$DOMCNT" ] ; do
            printf "%q" "DOMS=" >amu.tmp
            printf "$" >>amu.tmp
            printf "%q" "DOM$CNT" >>amu.tmp
            source amu.tmp
            rm -f amu.tmp
    }
    
    function end() {
    let "CNT++"
    done
    }
    then having my main script as:

    Code:
    start
    CUSTOM COMMAND
    end
    
    exit 0
    And that doesn't seem to work...

    I have also tried sourcing the require start and end code - to the same results.

    Code:
    ./2test: start_dom_cycle: line 9: syntax error: unexpected end of file
    ##############
    # DOMAIN 
    ##############
    
    ./2test: end_dom_cycle: line 2: syntax error near unexpected token `done'
    ./2test: end_dom_cycle: line 2: `done'
    Yet assembled - the code works fine.

    ideas? Better way of doing this?

  2. #2
    Just Joined!
    Join Date
    May 2004
    Posts
    10
    ok - I think i know why... it was the start and end functions did not include all of the required statment (since I had split the "while" loop between the 2 functions.

    So I ended up doing it this way:

    Code:
    function cmd01() {
            echo "##############"
            echo "# DOMAIN 1
            echo "##############"
            echo
    }
    
    function cmd02() {
            echo "##############"
            echo "# DOMAIN 2"
            echo "##############"
            echo
    }
    
    function generic() {
    let "CNT=0"
    let "CNT++"
    while [ "$CNT" -le "$DOMCNT" ] ; do
            printf "%q" "DOMS=" >amu.tmp
            printf "$" >>amu.tmp
            printf "%q" "DOM$CNT" >>amu.tmp
            source amu.tmp
            rm -f amu.tmp
            $TMPCMD
    let "CNT++"
    done
    }
    
    
    
    TMPCMD="cmd01"
    generic
    TMPCMD="cmd02"
    generic
    This code just shows how i was going to format it.

Posting Permissions

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