In DevOps world, when most of the servers are using Linux-based operating systems, shell scripting is a tool that can save a lot of time and efforts. In such times, many of us usually end up in something very complex and less efficient. We may achieve the required output but we often overlook the load that we have just put on the host machine.
“Complex code decreases and affects, not only efficiency but readability and reliability too”
Further on this topic, we will see few use cases on the parameters and frequently used commands in shell scripting.
Parameters (variables)
Using Parameters
Common Use : echo $var #where $ ”dollar” is used for calling the variable 'var'
# However, above does not ensure that the variable was expanded to its actual value.
Recommended Use: echo ${var}
Parameter expansion
Indirection
Notation : ${!var}
In this case, the variable 'var' is not the variable itself but representing the value of another variable say 'tmp', let tmp=cde
Example 1:
Example 2:
$# represents the number of arguments and ${!#} represents the last argument.
set 1 2 3 four

set 1 2 3 four
Case Modification
Case modification is something we often need in our scripts to decorate and format the notes and informational messages.
Example 1: Lowercase modification using ',' and Uppercase modification using '^', see how they are used in this example below:
Variable Name Expansion
This expands to a list of all set variable names beginning with the string PREFIX.
Example 1: Here PREFIX is BASH and HOST
Sub-string Removal
Consider a used case of usage function in your shell script, you may have often wondered if you could simply use something that can omit the absolute path from $0 to just the script name. Below is something that does that very clearly for you.
Remove From The Beginning:
Example 1: ${var##pattern} # where a single # removes the pattern just once
And ## removes all occurrences of that pattern from the beginning.
Remove From The End:
Example 2: ${var%%pattern} # where a single % removes the pattern just once
And %% removes all occurrences of that pattern from the end.
Search and Replace
In below examples, a string can either be replaced or omitted depending upon the pattern used.
Notation : ${var/search_for/replace_with} # / replaces the searched just once
${var//search_for/replace_with} # // replaces all occurrences of the searched pattern.
Example 1:
Example 2:
Using Offset Values
Further, we will see how to get a specific part of string using offset. However, if an offset is negative, it's taken as a second offset into the string, counting from the end of the string.
Notation : ${var:start:end} # No end value results in considering end of line
Example 1: Positive and Negative offsets (putting random values)
Default Values
When the parameter is null or unset, this utility gives us the power to use or assign a default value.
Use or Assign a default value, when a parameter is undefined or unset:
Example1: “:-” and “:=” or “=”
Throw error when the parameter is unset or undefined:
Example 2: “:?”
Alternate Value
When the parameter is not null or set, an alternate value can be used in place of the actually defined value using ':+', however, an alternate value will not be used if the parameter is unset or null.
Beware and note the colon ':' before ‘+’ here. Using only ‘+’ without ‘:’ would anyways use the alternate value irrespective of parameter's state.

Beware and note the colon ':' before ‘+’ here. Using only ‘+’ without ‘:’ would anyways use the alternate value irrespective of parameter's state.
String Length
While $# gives us the number of arguments, coupling this pair with the parameter called ${#var} gives us the string length. This does works in arrays.
Example 1: Get string length
Example 2: Get number of array elements
Useful and Useless Uses Of Shell Commands and Components
CAT, AWK, SED, FOR, WHILE, CUT, GREP, WHICH, FIND, LS, LESS are few to list but among the most widely, uselessly used commands by the Linux professionals. Most professionals make useless use of these commands not because they do not know that this is an incorrect way but because they don't know the right way.
“It is always good to learn what is right than to keep doing the wrong”
Let us see a simple example, then followed by a number of useless and useful uses.
Create a file
Let's take an example of creating files in shell scripting, the first way shown below is what should be a recommended way used while writing scripts and not something else using pipes.
The second way shown is via terminal with cat command which uses ctrl+d to be pressed to save the file once after the changes are done. However, when you are in a terminal you can use the editor of your choices such as vim or gedit or Emacs.

The second way shown is via terminal with cat command which uses ctrl+d to be pressed to save the file once after the changes are done. However, when you are in a terminal you can use the editor of your choices such as vim or gedit or Emacs.
Useless and Useful
Example 2: Suppose, test is a file.
Example 3:
I have created a directory for test files, let's see how to traverse through this directory by not using any useless usage fo commands.
I have created a directory for test files, let's see how to traverse through this directory by not using any useless usage fo commands.
Example 4:
Example 5: Do an 'echo $n' yourself at last and see what you get.
No comments:
Post a Comment