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.
- 05-15-2004 #1Just 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:
Now i have tried breaking it into 2 functions like: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
then having my main script as: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 }
And that doesn't seem to work...Code:start CUSTOM COMMAND end exit 0
I have also tried sourcing the require start and end code - to the same results.
Yet assembled - the code works fine.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'
ideas? Better way of doing this?
- 05-16-2004 #2Just 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:
This code just shows how i was going to format it.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


Reply With Quote
