Find the Length of a String Variable in Bash
I forget this one all the time! so making a record $ STRING=”test” $ echo “${#STRING}” 4 $
Indirectly reference bash variable
If you know the name of a variable which contains a value of interest, you need indirect referencing to retrieve the value, this is how you do it. #!/bin/bash j=1 #variable containg a value i=j #variable containing the name of the variable which holds the value eval k=\$$i #k now contains the value of j [...]
Bash based Busy Handler for Sqlite
Sqlite is a serverless database and hence doesn’t allow multiple synchronous write requests. Sqlite API for most programming languages have a busy handler functionality which lets you define a busy handler function or a timeout value, thus enabling your program to wait for a certain amount of time and then access the database again.
Bash Tricks: Deal with a Flaky Internet Connection
Its been about 9 months in Vietnam now. One problem that I have to deal with on a day to day basis here is the flaky Internet connection. There are good days and bad days and bad days are incredibly frustrating. The Internet is ON and OFF every five minutes. I wrote this simple script [...]
Bash Tricks: Get the Value of a Variable whose Name is Stored in Another Variable
If want to find out the value of a variable whose name is stored in another variable, you need to dereference the variable. This is how you do it. Lets say the name of the variable is test123 and it has a value equal to 123. Also the name test123 is stored in another variable [...]
Bash Tricks: Delete the Last Substring from a Delimited String
I wrote a post about how to print just the last substring of a delimited string. Instead if you’d want to delete just the last substring, and keep rest of the string intact, thats a little trickier. Here’s how you do it. @~$ echo “abc:cde:efg” | awk ‘BEGIN {FS=ORS=”:”} {for(i=1;i<NF;i++) print $i}’ | sed ‘s/$/\n/’ [...]
Bash Tricks: Print the Last Substring from a Delimited String
@~$ echo “abc:cde:efg” | awk ‘BEGIN {FS=”:”} {print $NF}’ efg The variable NF holds the number or records in the string, which is 3 in this case. So when you’re do a print $NF, you are actually executing print $3, and thus printing the last record/substring.
Bash Tricks: Split / Cut a String with Multi Character Delimiters Using AWK
Some time back I wrote this post showing how to split a string into substrings separated by multi character delimiters. Didn’t realize then that there’s a much easier solution using awk. Using the same example as used in the previous post, here’s the solution. echo “abcdefghijklmn opqr stuv wxyz” | awk ‘BEGIN {FS=”"} {for(i=1;i
Copy a File to Multiple Directories on Linux
find . -type d | while read dir; do cp PATH_TO_FILE $dir; done
Find Duplicate Lines in a File
sort PATH_TO_FILE | uniq -d Continuously watch for duplicate lines as you work on a file watch “sort PATH_TO_FILE | uniq -d”
Recent Comments
(1 weeks ago)
(1 weeks ago)
(1 weeks ago)
(1 weeks ago)