Advertisment

Save Time While Shell Scripting in Linux Using Bash

author-image
PCQ Bureau
New Update


Advertisment

1. You are editing a few files on the command-line while being logged in remotely to a Linux system. You need to use the Indian Rupee symbol in files that you are about to edit. Since you are logged in remotely, chances are you will not have access to a GUI. What do you do?



The latest version of Bash allows support for the echo and printf commands to make use of 4-digit Unicode escape sequences in a convenient manner. For example, the 4-digit Unicode escape sequence for the Indian Rupee symbol is 20b9.

Advertisment



This single line in your shell script will display the Rupee symbol.



printf “\u20b9”



That's it. You no longer need to be bothered about the hexademical/octal conversion using the latest version of Bash. However, this command does not contain a trailing newline and hence if this is the only command in your shell script, the command prompt will be displayed immediately after the Rupee symbol, without beginning it on a new line, as shown in the image.

Advertisment



You can use this method for many other Unicode symbols which you may need to use. Making use of the piping and redirection features of the shell, you can easily use this symbol in your files without needing to use a GUI or clipboard or pointing device. It is to be kept in mind however that your terminal must be set to use UTF-8 encoding, else this method will return garbled text.



2. You have implemented a recursive function that is computationally expensive and has the potential to deliver incorrect results if the recursion is not controlled. But explicitly checking for limiting values inside the recursion is cumbersome. What do you do?



Let the system monitor and control the recursion. The latest version of Bash allows you to define a variable called as FUNCNEST , which will set limits on how many times a given function is to invoke itself. Once you define this variable's value, you shall no longer be affected by changes to limiting values caused as a result of changes to the business logic used inside the recursive function. You only wanted to control how many times the function will call itself, irrespective of what the function actually does or how it changes values of data items, which you can now precisely control.

Advertisment

Let's say we have a function as follows:

recursive()

{

if < $1 -ne 10 > ;then

echo $1

let i=$1+1

let i=$i*$1

recursive $i

fi

exit

}





We start off with a seed value of 1 and call this function accordingly in our shell script. In the absence of a FUNCNEST value defined, the script will loop indefinitely and go beyond the arithmetic limitations of the microprocessor, hence returning incorrect values as a result. However, when you do make use of the FUNCNEST variable, not only will it stop when desired but also the shell will notify you that the limit has been reached, as shown in the image.





This eliminates the need for testing absolute values of variables to determine exit criteria from recursion.



3. You are running a long-running shell script and have lost track of the absolute time at which it started. Sure, you can easily use counters to keep track of the time passed since the script began execution but wouldn't it be more convenient if one simple command used within the script anywhere could return the time when the script was started?



The latest version of Bash adds a highly convenient format specifier for time to the printf command. This now uses strftime-like formatting for time values. E.g. if at any point of time you need to display on screen when the shell script currently being run began its execution in terms of an absolute time value, you can use the following line in your shell script:



printf “%(%c)T” -2



The -2 argument stands for the time of beginning the execution of the script in which this command lies. An argument of -1 would mean the current time (similar to the output which you would get by simply using the date command), any non-negative numerical value would mean the number of seconds having passed according to Epoch time in Unix. It is to be noted that the time of beginning the script will be maintained by subsequent printf commands identical to the one mentioned above even if a sleep command has been executed in the midst of any two printf commands.



There are many such features of shell scripting using Bash, through which you will be able to get the system to perform most repetitive tasks on its own.

Advertisment