Exponential search is an algorithm designed to search for a target value in a sorted array. It is useful in scenarios when dealing with large or potentially infinite datasets and it can also help you find a number, which is not known in advance. Let’s see how it works and how to implement exponential search in bash.
Continue readingCategory: Tips and tricks
Linux tips and tricks with focus on shell scripting and monitoring. Here you will find short texts about solving real world problems with code examples.
dealing with float numbers in bash
Dealing with float numbers in bash can be a bit tricky since bash natively supports only integer arithmetic. However, you can perform floating-point arithmetic using external tools. Here we will walk through few methods to handle floating-point numbers in bash.
Continue readingnegate something in regex using negative lookahead
If you wish to negate something in regex, one of the easiest and most reliable ways to do it is to employ negative lookahead assertion. Having a need to split the set of objects into different categories with some “other” / “default” / “the rest” option is one of the most common use cases where this approach can be used.
Continue readingconvert wildcard in file name into relevant static file name
Sometimes you might encounter cases when files are named somewhat weird and makes it hard to use them for automated analysis purposes. In such cases, wildcard expansion becomes a very useful concept. For example, some applications tend to write their logs into log files with some additional information in file names, like:
[root@linux ~]# ls -l /var/log/demo/
total 76
-rw-r--r-- 1 root root 19125 Mar 15 13:48 2024-03-13.demo.log
-rw-r--r-- 1 root root 29580 Mar 15 13:49 2024-03-14.demo.log
-rw-r--r-- 1 root root 22100 Mar 15 13:49 2024-03-15.demo.log
[root@linux ~]#
Say log file is being written to as long as calendar date matches the current one. And what if you want to set some automated analysis of such log files, but to do it only for the relevant one, to which application currently writes?
Continue readingcheck ownership and permissions for absolute path easily
I couldn’t count times when asking someone to check permissions in Linux for some file I was given answer which was something like:
[root@linux ~]# ls -l /var/log/httpd/access_log
-rw-r--r-- 1 root root 17245040 Mar 13 13:38 /var/log/httpd/access_log
[root@linux ~]#
So all looks good – file is readable. But is it really?
Continue readingcheck if json is valid
Sometimes you might need to check if json is valid directly in CLI. You can validate if json has correct structure easily with command line utility “jq“.
Continue readingredirect both stdout and stderr to same place
Sometimes you might want to redirect both stdout and stderr to same place. You can do it like:
[root@linux ~]# ./script.sh >/dev/null 2>&1
But there is a shorter way…
Continue readingcreate results file only when fully finished
Idea behind this trick is very simple yet powerful enough. It allows you to avoid unexpected consequences when some results file is being appended long enough by your process.
Continue readingrun commands with timeout
Sometimes you might have use cases, when specific command or script should be run no longer than given amount of time. In those cases you should run commands with timeout.
Continue readinggrep to output only needed capturing group
Say you have some text and some pattern that you want to provide for grep. Everything is easy, until you want to extract only the pattern-matching part, not the whole line that has the match. What to do? You can “grep” and use pipe for further processing, like sed or awk. But did you know that you can grep only capturing group output without using anything else? We will be using “grep -P” (Perl Compatible Regular Expressions – PCRE) for that.
Continue reading