D-Link DSN-540 CLI User's Guide for DSN-1100-10 - Page 41

Advanced Scripting Concepts

Page 41 highlights

3.4.3 Advanced Scripting Concepts The CLI provides the ability to perform environment variable substitution. Variable substitution is the process of replacing a reference to the name of a variable with its actual value. Variable substitution is useful in Python, Perl, Bash, or other scripts that invoke predefined CLI script files and perform substitution at runtime. The substitution syntax is ${varName}, with the information typed between the curly brackets specifying the variable name. Any time you use this syntax in a command line, the CLI expects the variable name in curly brackets to be the name of a variable. If you want the dollar sign preceding the first curly bracket to be interpreted as just a simple dollar sign, precede it with the backslash (\) "escape" character. Example 1: The following example shows how variable substitution might work with a bash script. In this example, assume that a bash script has the following lines: #!/bin/bash export VolName="MyNewVolName" cli2 -x DeleteVolume export VolName="Zeus" cli2 -x DeleteVolume Also, assume that DeleteVolume.cli contains the following line: VolumeList.Volumes[${VolName}].Delete In this example, ${VolName} will be replaced with the volume named Zeus, which will be deleted. Example 2: The following example shows how the backslash character can be used to have the CLI interpret a dollar sign character as just a simple dollar sign. In this example, assume that an ENV variable called envvar equals ABC. In this case, abc\${envvar} becomes abc${envvar}, while abc${envvar} becomes abcABC. Command Line Interface User's Guide 31

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

Command Line Interface User’s Guide
31
3.4.3
Advanced Scripting Concepts
The CLI provides the ability to perform environment variable substitution. Variable
substitution is the process of replacing a reference to the name of a variable with its actual
value. Variable substitution is useful in Python, Perl, Bash, or other scripts that invoke
predefined CLI script files and perform substitution at runtime.
The substitution syntax is
${varName}
, with the information typed between the curly
brackets specifying the variable name. Any time you use this syntax in a command line, the
CLI expects the variable name in curly brackets to be the name of a variable. If you want the
dollar sign preceding the first curly bracket to be interpreted as just a simple dollar sign,
precede it with the backslash (\) “escape” character.
Example 1: The following example shows how variable substitution might work with a bash
script. In this example, assume that a bash script has the following lines:
#!/bin/bash
export VolName=
"
MyNewVolName"
cli2 –x DeleteVolume
export VolName="Zeus"
cli2 –x DeleteVolume
Also, assume that
DeleteVolume.cli
contains the following line:
VolumeList.Volumes[${VolName}].Delete
In this example,
${VolName}
will be replaced with the volume named
Zeus
, which will be
deleted.
Example 2: The following example shows how the backslash character can be used to have
the CLI interpret a dollar sign character as just a simple dollar sign. In this example, assume
that an
ENV
variable called
envvar
equals
ABC
. In this case,
abc\${envvar}
becomes
abc${envvar}
, while
abc${envvar}
becomes
abcABC
.