Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to write shell script that takes an argument, and then applies it to files.

Specifically, I want to give a term, and then have it compile term.as with mxmlc ("mxmlc term.as"), then run term.swf with flashplayerdebugger ("flashplayerdebugger term.swf"). I'm fairly new to shell scripting - any thoughts?

share|improve this question
    
$2 is the first argument, $3 the second, and so on. $1 is reserved for the name of the file. As for the other stuff, I don't quite understand what you mean. Could you please try to explain it a little better or write some psuedocode? –  InkBlend Aug 15 '12 at 23:36
1  
@InkBlend you're off by one. "$1" is the first argument. –  geirha Aug 17 '12 at 13:20
    
@geirha: Ouch. That hurts. You're right, of course. Silly me, I must have been thinking of Python. –  InkBlend Aug 17 '12 at 17:16
add comment

2 Answers

up vote 4 down vote accepted

You could use something like this:

#!/bin/sh
# $0 is the script name, $1 id the first ARG, $2 is second...
NAME="$1"
mxmlc $NAME.as
flashplayerdebugger $NAME.swf
share|improve this answer
1  
Or skip the NAME variable altogether and just use mxmlc "$1".as and flashplayerdebugger "$1".swf. (Quotes can be inside expressions.) –  Eliah Kagan Aug 15 '12 at 23:49
    
but what if the param contains spaces ? –  Dawid Aug 15 '12 at 23:50
    
You're quite right. I've edited my comment to fix this error. –  Eliah Kagan Aug 15 '12 at 23:53
    
Thank you very much! –  southpointingchariot Aug 16 '12 at 0:34
    
@shinnra If the parameter contains spaces, then nxmlc and flashplayerdebugger will get multiple arguments instead of the intended one argument. The expansion of NAME needs to be double-quoted. On a side note, don't use all uppercase variable names; you risk overwriting special shell variables or environment variables. –  geirha Aug 16 '12 at 19:05
add comment

I also recommend you use the variable name delimiter. So the code would look like:

#!/bin/sh
# $0 is the script name, $1 id the first ARG, $2 is second...
NAME="$1"
mxmlc ${NAME}.as
flashplayerdebugger ${NAME}.sw

This allows the use of the variable in any context, even inside other text. For example:

NewName="myFileIs${NAME}and that is all"

This would expand the variable NAME which would be flanked in front by "myFileIs" and at the back with "and that is all" The variable would expand, spaces included, inside the string. if NAME was "inside here" the NewName would be "myFileIsinside hereand that is all".

The command line can take up to 9 variables. They can be quoted strings which contain blanks, each quoted string counts as a variable. Such as:

./myProg var1 var 2 var3

So ${1} is "var1", ${2} is "var", ${3} is "2", ${4} is "var3"

BUT: ./myProg var1 "var 2" var3

has ${1} is "var1", ${2} is "var 2", ${3} is "var3"

Have fun!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.