Substitution Operators in Font

Generation Data Matrix ECC200 in Font Substitution Operators

Substitution Operators
DataMatrix Generation In None
Using Barcode creator for Font Control to generate, create Data Matrix ECC200 image in Font applications.
www.OnBarcode.com
EAN-13 Generation In None
Using Barcode creation for Font Control to generate, create GS1 - 13 image in Font applications.
www.OnBarcode.com
Within a script, it may be important to check whether a variable really has a value assigned to it before the script continues. To do this, Bash offers substitution operators. By using substitution operators, you can assign a default value if a variable doesn t have a value currently assigned, and much more. Table 7-1 provides an overview of the substitution operators with a short explanation of their use. Table 7-1. Substitution Operators
Barcode Creator In None
Using Barcode maker for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Barcode Generation In None
Using Barcode generator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Operator
Drawing EAN / UCC - 13 In None
Using Barcode generator for Font Control to generate, create GTIN - 128 image in Font applications.
www.OnBarcode.com
DataMatrix Encoder In None
Using Barcode generator for Font Control to generate, create Data Matrix 2d barcode image in Font applications.
www.OnBarcode.com
${parameter:-value} ${parameter=value} ${parameter:=value}
Create PDF417 In None
Using Barcode generator for Font Control to generate, create PDF 417 image in Font applications.
www.OnBarcode.com
ISSN - 10 Creator In None
Using Barcode generation for Font Control to generate, create ISSN - 10 image in Font applications.
www.OnBarcode.com
Show value if parameter is not defined. Assign value to parameter if parameter does not exist at all. This operator does nothing if parameter exists, but doesn t have a value. Assign value if parameter currently has no value, or if parameter doesn t exist at all.
Print DataMatrix In VB.NET
Using Barcode creation for VS .NET Control to generate, create ECC200 image in VS .NET applications.
www.OnBarcode.com
ECC200 Recognizer In .NET Framework
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Continued
Data Matrix ECC200 Printer In Java
Using Barcode printer for Android Control to generate, create Data Matrix image in Android applications.
www.OnBarcode.com
Drawing EAN-13 In Objective-C
Using Barcode generator for iPad Control to generate, create EAN / UCC - 13 image in iPad applications.
www.OnBarcode.com
CHAPTER 7 RUNNING IT ANY WAY YOU LIKE
Generating 2D In .NET
Using Barcode drawer for ASP.NET Control to generate, create Matrix 2D Barcode image in ASP.NET applications.
www.OnBarcode.com
Drawing PDF-417 2d Barcode In Java
Using Barcode drawer for Java Control to generate, create PDF-417 2d barcode image in Java applications.
www.OnBarcode.com
Table 7-1. Continued
PDF 417 Reader In .NET Framework
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Generate EAN13 In Visual C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create UPC - 13 image in Visual Studio .NET applications.
www.OnBarcode.com
Operator
PDF417 Encoder In Visual Studio .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
Decode Data Matrix ECC200 In .NET Framework
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
${parameter: value}
Read Code 39 Extended In Visual Basic .NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
1D Barcode Creator In Java
Using Barcode creator for Java Control to generate, create 1D image in Java applications.
www.OnBarcode.com
Show a message that is defined as value if parameter doesn t exist or is empty. Using this construction will force the shell script to be aborted immediately. If parameter does have a value, the value is displayed. If it doesn t have a value, nothing happens.
${parameter:+value}
Substitution operators can be hard to understand. To make it easier to see how they work, Listing 7-13 provides some examples. In all these examples, something happens to the $BLAH variable. You ll see that the result of the given command is different depending on the substitution operator that s used. To make it easier to discuss what happens, I added line numbers to the listing. Notice that when trying this yourself, you should omit the line numbers. Listing 7-13. Using Substitution Operators 1. sander@linux %> echo $BLAH 2. 3. sander@linux %> echo ${BLAH:-variable is empty} 4 variable is empty 5. sander@linux %> echo $BLAH 6. 7. sander@linux %> echo ${BLAH=value} 8. value 9. sander@linux %> echo $BLAH 10. value 11. sander@linux %> BLAH= 12. sander@linux %> echo ${BLAH=value} 13. 14. sander@linux %> echo ${BLAH:=value} 15. value 16. sander@linux %> echo $BLAH 17. value 18. sander@linux %> echo ${BLAH:+sometext} 19. sometext The example of Listing 7-13 starts with the echo $BLAH command, which reads the variable BLAH and shows its current value. Because BLAH doesn t have a value yet, nothing is shown in line 2. Next, a message is defined in line 3 that should be displayed if BLAH is empty. As you can see, the message is displayed in line 4. However, this doesn t assign a value to BLAH, which you see in lines 5 and 6 where the current value of BLAH is asked again. In line 7, BLAH finally gets a value, which is displayed in line 8. The shell remembers the new value of BLAH, which you can see in lines 9 and 10 where the value of BLAH is referred to and displayed. In line 11, BLAH is redefined but it gets a null value. The variable still exists; it just has no value here. This is demonstrated when echo ${BLAH=value} is used in line 12; because BLAH has a null value at that moment, no new value is assigned. Next, the construction echo ${BLAH:=value} is used to assign a new value to BLAH. The fact that BLAH really gets a value from this is shown in lines 16
CHAPTER 7 RUNNING IT ANY WAY YOU LIKE
and 17. Finally, the construction in line 18 is used to display sometext if BLAH currently does have a value. Notice that this doesn t change anything to the value that is assigned to BLAH at that moment; sometext just indicates that it has a value and that s all.
Pattern-Matching Operators
You ve just seen how substitution operators can be used to do something if a variable does not have a value. You can consider them a rather primitive way of handling errors in your script. A pattern-matching operator can be used to search for a pattern in a variable and modify the variable if that pattern is found. This can be very useful because it allows you to define a variable exactly the way you want. For example, think of the situation in which a user enters a complete pathname of a file, but only the name of the file itself (without the path) is needed in your script. The pattern-matching operator is the way to change this. Pattern-matching operators allow you to remove part of a variable automatically. Listing 7-14 is an example of a script that works with pattern-matching operators. Listing 7-14. Working with Pattern-Matching Operators #!/bin/bash # # script that extracts the file name from a file name that includes the complete path # usage: stripit <complete file name> filename=${1##*/} echo "The name of the file is $filename" When executed, the script shows the following result: sander@linux %> ./stripit /bin/bash the name of the file is bash Pattern-matching operators always try to locate a given string. In this case, the string is */. In other words, the pattern-matching operator searches for a /, preceded by another character. In this pattern-matching operator, ## is used to search for the longest match of the provided string, starting from the beginning of the string. So, the pattern-matching operator searches for the last / that occurs in the string and removes it and everything that precedes the / as well. You might ask how the script comes to remove everything in front of the /. It s because the pattern-matching operator refers to */ and not to /. You can confirm this by running the script with /bin/bash/ as an argument. In this case, the pattern that s searched for is in the last position of the string, and the pattern-matching operator removes everything. This example explains the use of the pattern-matching operator that looks for the longest match. By using a single #, you can let the pattern-matching operator look for the shortest match, again starting from the beginning of the string. If, for example, the script in Listing 7-14 used filename=${1#*/}, the pattern-matching operator would look for the first / in the complete file name and remove that and everything before it. You should realize that in these examples the * is important. The pattern-matching operator ${1#*/} removes the first / found and anything in front of it. The pattern-matching
Copyright © OnBarcode.com . All rights reserved.