Skip to main content
deleted 2 characters in body
Source Link
Amit
  • 195
  • 7

Action (or execution in this case) always speaks louder, so let's look at what this script does when executed (excuse the liberty taken to make the output more verbose):

while read -r line do if [ "${line#*'Caused by'}" != "$line" ]; then echo "Line contains string Caused by" else echo "Line does not contain string Caused by" fi done Input: String with Caused by Output: Line contains string Caused by Input: Just a normal string Output: Line does not contain string Caused by 

The pattern matching used in this script "${line#*'Caused by'} is replacing all string (owing to the wildcard *) from the beginning to the end of Caused by in the inputted line and then it compares it with the original $line parameter to see whether they are equal or not. Simple stated, all it does is a check whether the line contains the string Caused by. Finally it prints Line contains string 'Caused by'Caused by if the line does contain Caused by.

Now, a few words about the shell parameter expansion for the ${parameter#word} format with some examples:

If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the "#'' case) or the longest matching pattern (the "##'' case) deleted.

$ test=aabbcc $ echo ${test#*bb} $ cc $ test=aabbcc $ echo ${test#a*b} $ bcc 

An example of the longest matching pattern format:

$ test=aabbcc $ echo ${test##a*b} $ cc 

Reference: man bash: ${parameter#word}

Action (or execution in this case) always speaks louder, so let's look at what this script does when executed (excuse the liberty taken to make the output more verbose):

while read -r line do if [ "${line#*'Caused by'}" != "$line" ]; then echo "Line contains string Caused by" else echo "Line does not contain string Caused by" fi done Input: String with Caused by Output: Line contains string Caused by Input: Just a normal string Output: Line does not contain string Caused by 

The pattern matching used in this script "${line#*'Caused by'} is replacing all string (owing to the wildcard *) from the beginning to the end of Caused by in the inputted line and then it compares it with the original $line parameter to see whether they are equal or not. Simple stated, all it does is a check whether the line contains the string Caused by. Finally it prints Line contains string 'Caused by' if the line does contain Caused by.

Now, a few words about the shell parameter expansion for the ${parameter#word} format with some examples:

If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the "#'' case) or the longest matching pattern (the "##'' case) deleted.

$ test=aabbcc $ echo ${test#*bb} $ cc $ test=aabbcc $ echo ${test#a*b} $ bcc 

An example of the longest matching pattern format:

$ test=aabbcc $ echo ${test##a*b} $ cc 

Reference: man bash: ${parameter#word}

Action (or execution in this case) always speaks louder, so let's look at what this script does when executed (excuse the liberty taken to make the output more verbose):

while read -r line do if [ "${line#*'Caused by'}" != "$line" ]; then echo "Line contains string Caused by" else echo "Line does not contain string Caused by" fi done Input: String with Caused by Output: Line contains string Caused by Input: Just a normal string Output: Line does not contain string Caused by 

The pattern matching used in this script "${line#*'Caused by'} is replacing all string (owing to the wildcard *) from the beginning to the end of Caused by in the inputted line and then it compares it with the original $line parameter to see whether they are equal or not. Simple stated, all it does is a check whether the line contains the string Caused by. Finally it prints Line contains string Caused by if the line does contain Caused by.

Now, a few words about the shell parameter expansion for the ${parameter#word} format with some examples:

If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the "#'' case) or the longest matching pattern (the "##'' case) deleted.

$ test=aabbcc $ echo ${test#*bb} $ cc $ test=aabbcc $ echo ${test#a*b} $ bcc 

An example of the longest matching pattern format:

$ test=aabbcc $ echo ${test##a*b} $ cc 

Reference: man bash: ${parameter#word}

added 149 characters in body
Source Link
Amit
  • 195
  • 7

The pattern matching used in this script "${line#*'Caused by'} is replacing all stringAction (owing to the wildcard *) from the beginning to the end of Caused byor execution in the inputted line and then it compares it with itself to see whether they are equal or not. In the end all it does it checks whether the line contains the string Caused by. Finally it prints Yes if the linethis case) always speaks louder, so let's look at what this script does containwhen executed Caused by.

I have taken(excuse the liberty to modify the script a little bittaken to make itthe output more verbose, with sample inputs as follows):

while read -r line do if [ "${line#*'Caused by'}" != "$line" ]; then echo "Line contains string 'CausedCaused by'"by" else echo "Line doesn'tdoes not contain string 'CausedCaused by'"by" fi done $Input: TestString with Caused by $Output: Line contains string 'CausedCaused by'by $Input: TestJust randoma normal string $Output: Line doesn'tdoes not contain string 'CausedCaused by'by 

The pattern matching used in this script "${line#*'Caused by'} is replacing all string (owing to the wildcard *) from the beginning to the end of Caused by in the inputted line and then it compares it with the original $line parameter to see whether they are equal or not. Simple stated, all it does is a check whether the line contains the string Caused by. Finally it prints Line contains string 'Caused by' if the line does contain Caused by.

The pattern matching used in this script "${line#*'Caused by'} is replacing all string (owing to the wildcard *) from the beginning to the end of Caused by in the inputted line and then it compares it with itself to see whether they are equal or not. In the end all it does it checks whether the line contains the string Caused by. Finally it prints Yes if the line does contain Caused by.

I have taken the liberty to modify the script a little bit to make it more verbose, with sample inputs as follows:

while read -r line do if [ "${line#*'Caused by'}" != "$line" ]; then echo "Line contains string 'Caused by'" else echo "Line doesn't contain string 'Caused by'" fi done $ Test Caused by $ Line contains string 'Caused by' $ Test random $ Line doesn't contain string 'Caused by' 

Action (or execution in this case) always speaks louder, so let's look at what this script does when executed (excuse the liberty taken to make the output more verbose):

while read -r line do if [ "${line#*'Caused by'}" != "$line" ]; then echo "Line contains string Caused by" else echo "Line does not contain string Caused by" fi done Input: String with Caused by Output: Line contains string Caused by Input: Just a normal string Output: Line does not contain string Caused by 

The pattern matching used in this script "${line#*'Caused by'} is replacing all string (owing to the wildcard *) from the beginning to the end of Caused by in the inputted line and then it compares it with the original $line parameter to see whether they are equal or not. Simple stated, all it does is a check whether the line contains the string Caused by. Finally it prints Line contains string 'Caused by' if the line does contain Caused by.

added 218 characters in body
Source Link
Amit
  • 195
  • 7

I have taken the liberty to modify the script a little bit to make it more verbose, with sample inputs as follows:

while read -r line do if [ "${line#*'Caused by'}" != "$line" ]; then echo "Line contains string 'Caused by'" else echo "Line doesn't contain string 'Caused by'" fi done $ Test Caused by $ Line contains string 'Caused by' $ Test random $ Line doesn't contain string 'Caused by' 

Now, a few words about the shell parameter expansion for the ${parameter#word} format with some examples:

>$ test=aabbcc >$ echo ${test#*bb}  >$ cc >$ test=aabbcc >$ echo ${test#a*b}  >$ bcc 
>$ test=aabbcc >$ echo ${test##a*b}  >$ cc 

Now, a few words about the shell parameter expansion for the ${parameter#word} format with some examples:

> test=aabbcc > echo ${test#*bb}  > cc > test=aabbcc > echo ${test#a*b}  > bcc 
> test=aabbcc > echo ${test##a*b}  > cc 

I have taken the liberty to modify the script a little bit to make it more verbose, with sample inputs as follows:

while read -r line do if [ "${line#*'Caused by'}" != "$line" ]; then echo "Line contains string 'Caused by'" else echo "Line doesn't contain string 'Caused by'" fi done $ Test Caused by $ Line contains string 'Caused by' $ Test random $ Line doesn't contain string 'Caused by' 

Now, a few words about the shell parameter expansion for the ${parameter#word} format with some examples:

$ test=aabbcc $ echo ${test#*bb} $ cc $ test=aabbcc $ echo ${test#a*b} $ bcc 
$ test=aabbcc $ echo ${test##a*b} $ cc 
added 45 characters in body; added 12 characters in body
Source Link
Amit
  • 195
  • 7
Loading
deleted 9 characters in body
Source Link
Amit
  • 195
  • 7
Loading
deleted 644 characters in body
Source Link
Amit
  • 195
  • 7
Loading
added 12 characters in body
Source Link
Amit
  • 195
  • 7
Loading
added 369 characters in body
Source Link
Amit
  • 195
  • 7
Loading
Source Link
Amit
  • 195
  • 7
Loading