ShellScript + Xcode
I'm using Texture Packer and a shell script to generate sprite sheets at each "clean" or "build" of my Xcode's project. But, I would like to generate them on a conditional way (compi开发者_如何学Pythonle flag or command line).
I'm not use to work with shell script and i didn't know how to get flag from project settings or command line in my shell script.
Is it other variable as ${ACTION} usable ? How to set them with project flag or command argument ?
Thx
People usually think of passing flags as arguments to a program, i.e.
myShellScript -o (options) flag1 flag2 file1 file2 file...n
This requires management of the arguments as options, either with a case statement or call to getopts
and other specialized functions. Searching here at SO for getopts should give you all the info you need on that.
What I'm discussing below assumes you can modify your scripts to put in ${ACTION} vars, which I refer to as flag variables. As you mention conditional actions, that is the reason I talk about flag variables using 'true' or 'false' to control running a block of code. If you understand these concepts, it shouldn't be hard to modify you code to conditionally run processes, with the least amount of code modifications. If you give us a specific small example, 'I need my code to conditional do ...', I and others here can easily help you resolve your issues.
So another way, possibly more suitable to you needs, is to set up your script's environment so any variables required are visible to the shellscript process. You can do this in 2 different ways.
set standard environment variables and export them before you call your script, i.e.
flag1=true ; flag2=false ; export flag1 flag2 myShellScript file1 file2 file...n
(file1 -> file..n may not be required for your shell script)
OR 'pass in' the flag variables to your shellscript as part of the invocation of your script
flag1=true flag2=false myShellScript file1 file2 file...n
To make either of these methods useful, you need to use the flag variables like
if ! ${flag1:-false} ; then
print "Skipping flag one code"
else
print "Inside flag one code, doing stuff"
fi
The :-
is a shell parameter operator that says 'If flag1 has doesn't exist or is set to "", temporarily use the value right after this :-
, in this case false.
With our 2 numbered techniques above, flag1 does have a value, 'true', so the temporary substituion does not happen.
Note that if you use technique 1, you have set flag1 as having a value (true), and the only way to 'turn it off' is to set it with a different value. the :-
notation will never take effect. You would have to unset flag1 from the command line to see the :-
notation work again. This can be confusing when you are using the command line to test code, so be sure to always unset variables that you expect to be processed with any of the parameter modifiers (there are others).
If you're not used to using statements that resolve to if true then ; print "true" ; fi
, here is a little to explain that.
The shell's order of evaluation looks for variables i.e. ${flag1} and substitutes any values it finds for those variables. So using if ${flag1:-false} ; then
... the var is found, it has no value, so then the value false is substituted. The value false resolves to a command /bin/false
, so that command is executed. It's only purpose is to return a non-zero exit code.
You can check out a lot of this by using your command line, 'turning on debugging', with set -vx
and playing around. Here's a quick sample.
**# Shell built-ins**
/home/Shellter :>which true
true
/home/Shellter :>which false
false
# which point to
/home/Shellter :>srchpath true
-rwxrwxr-x 1 Shellter None 17 Jan 13 2009 /usr/bin/true
/home/Shellter :>srchpath false
-rwxrwxr-x 1 Shellter None 17 Jan 13 2009 /usr/bin/false
#turn on debugging mode
/home/Shellter :>set -vx
# test true and false at the most basic level
# then what is the exit code?
/home/Shellter :>true
true
[1]+ true
/home/Shellter :>echo $?
echo $?
[1]+ echo 0
0
/home/Neil_2 :>false
false
[1]+ false
/home/Neil_2 :>echo $?
echo $?
[1]+ echo 1
1
/home/Shellter :>unset myFlag
unset myFlag
[1]+ unset myFlag
/home/Shellter :>if ${myFlag:-false} ; then print "found true value" ; else print "found false value" ; fi
if ${myFlag:-false} ; then print "found true value" ; else print "found false value" ; fi
[1]+ false
[1]+ print 'found false value'
found false value
# set myFlag
/home/Shellter :>myFlag=true
myFlag=true
[1]+ myFlag=true
/home/Shellter :>if ${myFlag:-false} ; then print "found true value" ; else print "found false value" ; fi
if ${myFlag:-false} ; then print "found true value" ; else print "found false value" ; fi
[1]+ true
[1]+ print 'found true value'
found true value
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, or give it a + (or -) as a useful answer
精彩评论