Code Snippets

Obtain a pointer to the struct that contains the struct member

Check this link for more detail: The Magical container_of() Macro

show code

#ifndef container_of
#include 
#define container_of(ptr, type, member) ({ 
		const typeof( ((type*)0)->member ) 
		* __mptr = ((void*)(ptr)); 
		(type*)( (char*)__mptr - 
				offsetof(type, member) ); 
		})
#endif

Check If A File Contains An Image

Use grep, Check With https://en.wikipedia.org/wiki/List_of_file_signatures

show code

grep -a -o -e $'GIF8' -e $'\xFF\xD8\xFF' -e $'\x89PNG' filename  

Shell Append Line By Sed

Use sed to append command to insert corresponed line into file

show code

function CreateNewDay()
{
	LineNo=`grep -n content index.html |head -1| cut -b 1,2`
		sed -i "$LineNo a${1}" index.html
		let LineNo=LineNo+1
		sed -i "$LineNo a${2}" index.html
		let LineNo=LineNo+1
		sed -i "$LineNo a${3}" index.html
}

Shell Selection Choice

bash select [y/n] different ways

show code


      while read -p 'Select one (y/n): '; do
        [[ "${REPLY}" =~ ^[YyNn]$ ]] && break || echo 'Invalid reply'
      done
      echo $REPLY