JCShell - Variables

The JCShell has global and local variables for shell command execution. You can use variables for storing values, referencing and value substitution. Variables can be set using the /set-var command or through direct assignment (i.e name=abc).

Variable references have the following form:

${[plugin:]name[;modifier...]}

A variable reference starts with a $ character and the variable body is enclosed in curly braces. An optional plugin nick name refers to a special variable maintained by the plugin. The nick name is separated by the variable name by a colon. The variable name may be followed by a sequence modifiers. Each modifier starts with a semicolon. The variable body inside the curly braces is subject to variable expansion.

Refer to the following sections for description of variable handling:

Examples:

-  /set-var x1 Hello
-  /set-var x2 1
-  /set-var -g g1 ${x1}":its my text"

-  /list-var
Global:____________________________________________
   g1=Hello:its my text
   x1=Hello
   x2=1

-  /echo ${x1}
Hello
-  /echo ${x${x2}}   
Hello
	

Variables beginning with 0x are interpreted as HEX-Values.

Examples:

-  /set-var N1 0A0A
-  /set-var N2 ${N1} ^ 0xFFFF
jcshell: Illegal expression: java.lang.NumberFormatException: 0A0A at: 0xFFFF...
-  /set-var N2 0x${N1} ^ 0xFFFF
-  /set-var N3 0x${N1} & 0xFFFF
-  /list-vars
Global:____________________________________________
   N1=0A0A
   N2=62965
   N3=2570

Variable modifiers

Modifiers can be used to change the substitution results of variables. A variable and its modifier are separated by a ";" character. Avoid spaces between modifier and separator.

The following modifiers are defined:

Modifier Description Example
dirname path of a canonical filename (content of the variable must be a filename)
>  myscr=${script.name;qq}
>  /echo Script: ${myscr}
Script: D:/eclipse/test/test.jcsh
>  /echo Dir=${myscr;dirname}

Dir=D:/eclipse/test
    
filename filename of a canonical filename (content of the variable must be a filename)
>  myscr=${script.name;qq}
>  /echo Script: ${myscr}
Script: D:/eclipse/test/test.jcsh
>  /echo Fn=${myscr;filename}

Fn=test.jcsh
    
basename basename of a canonical filename (content of the variable must be a filename)
>  myscr=${script.name;qq}
>  /echo Script: ${myscr}
Script: D:/eclipse/test/test.jcsh
>  /echo Base=${myscr;basename}

Base=test
    
extension extension (including dot) of a canonical filename (content of the variable must be a filename)
>  myscr=${script.name;qq}
>  /echo Ext=${myscr;extension}

Ext=.jcsh
    
l Length of the variable content or number of elements of a variable array
>  name=12345678abc
>  /echo ${name;l}

11
    
q quote variable content with "
>  /set-var name "this is my test"
>  /list-var
Global:_________________
   txt1=this is my test
>  /set-var txt2 ${txt1}
jcshell: Bad expression: is

>  /set-var txt2 ${txt1;q}quoted
>  /list-var
Global:_________________
   txt1=this is my test
   txt2=this is my testquoted
    
qq quote variable content with '
>  /echo The script name is: ${script.name;qq}

The scipt name is: D:\eclipse\test\test.jcsh
    
lc Change variable content to lowercase
>  name=john PAUL Ringo
>  /list-var
Global:_________________
   name=john PAUL Ringo
>  /echo ${name;lc}

john paul ringo
    
uc Change variable content to uppercase
>  name=john PAUL Ringo
>  /list-var
Global:_________________
   name=john PAUL Ringo
>  /echo ${name;uc}

JOHN PAUL RINGO
    
h[x] Change the variable content to hex-characters. The output length (x) specifies the number of characters in the output. The output will be padded with '0' characters if the length is less or equal to 8.
>  ins=21
>  /echo FF${ins;h2}FF
FF15FF
>  /echo FF${ins;h4}FF
FF0015FF
 
sOFF[,LEN] Return the substring from position <OFF> in length <LEN>. The first character is offset 0. If you omit length, the rest of the string is returned. If LEN is greater than the length of the string, only the length of the string is returned.
> text=this is my test
> /echo ${text;s5,7}

is my t

The special use of the modifier ":" allows access to JCShell-Plugin variables. (see JCShell Plugins) The form is <plugin_nickname>:<varname>

Array of variables

Setting a variable with a range creates an array. Different range definitions are possible. The first array position is zero

 Description
#max --> first free position at the end (equal to length)
*all elements from first to last
numberindex of specific element
number..numberrange of elements including these positions
..numberrange of elements from first to (including) defined position
number..range of elements from (including) defined position to last element
number+countrange of elements from (including) defined position (number) for (count) elements

Examples:

> myArr[*]=v1 v2 v3 v4 v5

> /echo ${myArr[*]} 
v1 v2 v3 v4 v5

> /echo ${myArr[0]} 
v1

> /echo ${myArr[2+2]} 
v3 v4

> /echo ${myArr[2..]} 
v3 v4 v5

> myArr[#]=v6 v7
> /echo ${myArr[*]}
v1 v2 v3 v4 v5 v6 v7

Special Variables

The JCShell will maintain a set of special variables. The following variables are always declared.

VariableDescription
channel If a card is connected returns the result of "card.getChannel()" otherwise ""
JCOPDIR Path to the JCOP directory (only when JCShell was started directly from console ).
last.error Returns the content of the error message of the last command. If the command has been successful this variable evaluates to an empty string. (<lastErrorMsg>)
last.errorlog Returns the content of the last script error <lastErrorLog>
last.return Returns the content of the last return <returnValue>
path A semicolon separated list of directories. The JCShell checks these directories for script files.
plugin The nickname of the current plugin or an empty string if no plugin active.
random Returns a 16 byte random value in HEX
[max=nn|len=mm] are optional modifiers
${random;len=12} returns a 12 byte random value
${random;max=4378266} returns a numeric random value lower then the given maximum
response Returns the last APDU-response
script.line If a script is executing returns the line-number + 1 otherwise 0
script.name Name of the executing script file
script.dir Path of the executing script file
argv Arguments given to the script execution
terminal If a terminal connected returns the result of "openTerminal.toString()" otherwise ""
Example
- /term Remote
--Opening terminal
> /echo ${terminal}
Remote(Socket[addr=localhost/127.0.0.1,port=8050,localport=2116]/JCOP (built: Jul 12 2006 17:51:03))
time Returns the current system timestamp (default fmt="y-m-d H:M:S")
Format modifiers are possible. They must start with "fmt=" and may contain the key-values {y, d, m, H, M, S}
Example
> /echo ${time;fmt=d.m.y H:M}
09.05.2006 15:04
*name Returns the value of the system environment variable <name> (result of "System.getenv(name)" or "System.getProperty(name, "")")
0 Returns the name of the script which is currently executing
1, ..., N If the variable name is a number then it refers to the positional as passed to the script or shell process.

Related information