Friday, July 23, 2021

Split a string by a given delimiter

 I was really amazed to find out that C++ string class has no split method. There are plenty of accasions I needed to split a string by a given delimiter. Lets see how to split a string and put substrings in a to vector. 

If you think incrementing varable start is ugly, you can use function find_first_not_of for it

Wednesday, June 30, 2021

modify files using sed in linux

 sed is a powerfull tool and you can find all details from here

delete a line containing a substring ("pattern")

sed -i '/pattern/d' file
replace a string
sed -e s/pattern//g -i file
using i.backup will keep a backup file of original and it is safer.  

sed -e "s/pattern//g" -i.backup file




Get parent directory name of shell script

 pwd | rev | cut -d/ -f1 | rev


lets see arguments for cut used with above command

spliting delimeter is given to -d, in this case \

required argument is given to -f, in this case 1


rev is used to reverse the string because parent directory name is the first argumet from reverse of pwd.

Get substring of a string in shell

 I wanted to get a only file names from the output of  command find . -maxdepth 1 -type d

This returns a strings contains "./" at the begining.

./1-case

./2-case

./3-case

./4-case

./5-case

./6-case


to remove first two characters i used command cut. as follows

echo "./1-case" | cut -c3-


-c means return characters

3- means from 3rd character to end


other examples

-c3 will return only 3rd character

-c3-4 will return characters form 3rd to 4th


 



Friday, June 25, 2021

Get the all folder names in current directory

 I wanted to copy a file from one location to multiple folders. We'll have to copy multiple times if we do it manally. Lets see how to use shell script to copy to every folder in one go using shell scripting.

main parts we need to do.

1. get all directories.

    find . -maxdepth 1 -type d

2. loop through every directory name, returned from 1, and copy file to each folder.

using foreach loop in s shell (.csh) 

> foreach dir ( ` find . -maxdepth 1 -type d` )
> foreach? cp ~/Scripts/convert.py $dir
> foreach? end

using for loop in bash shell (.sh)

Other commands to get all directory names

  1. ls -d */
  2. ls -l | grep `^d'

Monday, June 21, 2021

Overview of TDD and BDD

I wanted to know what are the differences between Test Driven Development and Behaviour Driven Development. So I went through many youtube videos and found following videos useful. 

Hello world of shell scripts

 

Just started recalling shell scripting again and I thought it would be good the post those somewhere.

So I started with following hello world. Not the simplest script, but it doesnt contain complex stuff. this script contains,

  • How to comment,
  • Output values using echo,
  • Execute some simple commands,
  • Assign output of a command to a variable,
  • print variable values using echo.
Just ran above script to break the inertia and get me started.

lets see some basics about shell scripting.

Most people are confused with the extension of shell scripts at the begining. some scripts have .sh extension some have .csh extension. 

I found below stackoverflow answers are nicely explained about that.


Content from link 1
bash and sh are two different shells. Basically bash is sh, with more features and better syntax. Most commands work the same, but they are different.Bash (bash) is one of many available (yet the most commonly used) Unix shells. Bash stands for "Bourne Again SHell",and is a replacement/improvement of the original Bourne shell (sh).

Content from link 2

.sh is indicative of a script for the Bourne shell (sh) or the Bourne Again shell (bash), which is generally a superset of its predecessor.

.csh is indicative of a script for the C shell (csh), which, being a shell, is largely similar, but significantly different as soon as you start doing anything much more complex than running a series of static commands.

Both shells are generally available on any POSIXy environment, and indeed both are often preinstalled, though bash (and its cousin sh) are a little more ubiquitous in my experience.

I say 'indicative' above, because the idea of a file extension doesn't technically exist and has no semantic or syntactical meaning. A file could be named script.steve or documentation.exe or include.h and still actually be a shell script. For more definitive confirmation, look at the first line of the file. For a script designed to be executed directly, it should start with a shebang line, which starts with an octothorpe (#), a bang (!), and the path to the executable which should run the script. For example:

#!/usr/local/bin/bash

or

#!/usr/bin/python3

 

Saturday, January 2, 2021

Declare const functions in cpp

 I was writting some code in c++ and I ran in to following issue with keyword const. At that point I realised that my knowledge about usage of const in C++ is very low. Here I'm writing this post with things I leaned after this mistake.

This is a simple Message class and I tried to override it's operator < so that it will be easier for sorting a container with Message objects. 


I got following error after compiling full code.


After a quick search with the help of google I found this SOF answer and according to that reason for error is, I'm calling non-const function getId which doesn't promise not to modify const Message object.

But as you can see, I had used const keyword with function getId. Guess what.. ! it is a wrong usage. It return a constant integer and return value cannot be changed. But here what I wanted is a function that gurantee calling object's safety. For that we'll have to use the const keywaord after the parameter list of function declaration.

This SOF question contains some nice explanations about the difference between const positioning with function declarations.



Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...