11

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?

2 Answers2

19

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
Eliah Kagan
  • 119,640
Dawid
  • 419
5

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!

Serg
  • 3
Hey Gary
  • 101
  • 1