Is it possible to move variables above those upon which they depend?
I want to keep the configuration variables at the very top, but they depend upon other variables which need to be above them. How can I (if that's possible) move a don't touch section below a config section?
To illustrate:
#!/bin/bash
#== don't touch ==
dirpath=$(dirname "$1")
dirname=$(basename $(dirname "$1"))
#== config ==
path="$dirpath" #using the value of $dirpath straight in config would be ugly.
name="$dirname"
echo "$path" + "$name"
..but I want to move the config above everything else:
#!/bin/bash
#== config ==
开发者_Python百科 path="$dirpath" #using the value of $dirpath straight in config would be ugly.
name="$dirname"
#== don't touch ==
dirpath=$(dirname "$1")
dirname=$(basename $(dirname "$1"))
echo "$path" + "$name"
Update
The usual pattern is:
#!/bin/bash
function main()
{
prepare
do_step1 args ...
do_step2 args ...
do_step3 args ...
do_step4 args ...
exit 0
}
function prepare() { .... }
function do_step1() { ....}
function do_step2() { ....}
function do_step3() { ....}
function do_step4() { ....}
// entry point
main
As long as the (sub)function definitions precede invocation, they will be found nicely.
Literal answers:
No, you'd need to have functions for that, or you eval:
export dynamic="interesting"
function getsomething() { echo -n "$dynamic"; }
export dynamic="stuff"
echo "$(getsomething)"
will display "stuff", not "interesting"
The eval approach:
export param=value1
export dependent='$param' # (note the SINGLE quotes)
export param=value2
eval "echo $dependent"
will print, "value2", not "value1"
Cosmetics:
If it's just about don't touch source an include file:
source donot_touch.sh
精彩评论