Author Archives: vishnupalakkal

About vishnupalakkal

Research Scholar

Useful HTML code Snippets created using “Manage Snippets” plugin for GEdit text editor

When it comes to writing a post using html editor, you will be having some frequently used codes to do some tasks. Life will be easier, if you have some shortcut key or other faster way (like writing a shortcode and press “tab” key) to insert these frequently used codes. There comes the use of “Snippets”.
 
You can use a text editor like GEdit for typing html codes in a faster way, if you have “Manage Snippets” plugin.
 

Some useful HTML code Snippets are created using “Manage Snippets” plugin for GEdit text editor. These snippets are created to help my blogging.

I have exported these snippets in to the file Snippets_exported.tar.gz
You can import it in “Manage Snippets” plugin. But I failed to import it, when i tried. Instead you can extract the zip file in to ~/.config/gedit/snippets folder. This will work.
 

The details of the snippets are given below.

  1. Bold

    Tab trigger: None
    Shortcut key: Ctrl+B
    Snippet code:
    <strong>${1:$GEDIT_SELECTED_TEXT}</strong>$0

    Example:

    • HTML Code
      <strong>Selected Text</strong>
    • Output
      Selected Text
  2. Italics

    Tab trigger: em
    Shortcut key: Ctrl+E
    Snippet code:
    <em>${1:$GEDIT_SELECTED_TEXT}</em>$0


    Example:

    • HTML Code
      <em>Selected Text</em>
    • Output
      Selected Text
  3. color

    Tab trigger: color
    Shortcut key: Shift+Alt+C
    Snippet code:
    <span style="color:${1:#A52A2A}">${2:$GEDIT_SELECTED_TEXT}</span>$0

    Example:

    • HTML Code
      <span style="color:#A52A2A">Selected Text</span>
    • Output
      Selected Text
  4. Block Section

    Tab trigger: block
    Shortcut key: Shift+Alt+B
    Snippet code:
    <div style="background-color:#f0f0f3;padding:.8em;">${1:$GEDIT_SELECTED_TEXT}</div>
    &nbsp;
    $0


    Example:

    • HTML Code
      <div style="background-color:#f0f0f3;padding:.8em;">Here is a new Block Section</div>
      &nbsp;
    • Output
      Here is a new Block Section

       

  5. Wrap Selection as Link

    Tab trigger: ref
    Shortcut key: Shift+Alt+L
    Snippet code:
    <a style="color:#3366ff;" href="${1:$GEDIT_SELECTED_TEXT}" title="${2:$GEDIT_SELECTED_TEXT}" target="_blank">${3:$GEDIT_SELECTED_TEXT}</a>$0

    Example:

    • HTML Code
      <a style="color:#3366ff;" href="https://vishnupalakkal.wordpress.com/" title="https://vishnupalakkal.wordpress.com/" target="_blank">https://vishnupalakkal.wordpress.com/</a>
    • Output
      https://vishnupalakkal.wordpress.com/
  6. Br

    Tab trigger: br
    Shortcut key: Ctrl+R
    Snippet code:
    <br>
    $0


    Example:

    • HTML Code
      Brek after this <br>
      Text in Next line
    • Output
      Brek after this
      Text in Next line
  7. code box1

    Tab trigger: code
    Shortcut key: None
    Snippet code:
    <span style="display:inline-block;color:#fdfdfd;padding:.2em;background-color:#8f8f8f;border:#bbcddc 2px solid;"><strong><code>${1:Paste your code Here!}</code></strong></span>


    Example:

    • HTML Code
      <span style="display:inline-block;color:#fdfdfd;padding:.2em;background-color:#8f8f8f;border:#bbcddc 2px solid;"><strong><code>Paste your code Here!</code></strong></span>
    • Output
      Paste your code Here!
  8. code box2

    Tab trigger: codeX
    Shortcut key: None
    Snippet code:
    <div>
    ${1:Text just above code Here!}<div style="padding:.4em;background-color:#f8f9fa;border:#bbcddc 2px solid;">
    [code language="${2:html}" gutter="${3:true}"]
    ${4:Paste your code Here for Syntax Highlighted Code!}
    [/code]
    </div><br>
    </div>
    $0


    Example:

    • HTML Code
      <div>
      Text just above code Here!<div style="padding:.4em;background-color:#f8f9fa;border:#bbcddc 2px solid;">
      [code language="html" gutter="false"]
      Paste your code Here for Syntax Highlighted Code!
      [/code]
      </div><br>
      </div>
    • Output
       
      Text just above code Here!

              Paste your code Here for Syntax Highlighted Code!
              

About default shell in ubuntu – “sh”, “dash”, “bash” and startup files

The default system shell (not login shell), sh is a symbolic link. By default, in Ubuntu (from Ubuntu 6.10 onwards) it is pointing to /bin/dash. So if you use #!/bin/sh or run a command using sh command , it will not execute as ‘bash’ script, instead it will execute as ‘dash’ script.

dash is a lightweight replacement for bash, but it will not support all the bash commands. For more info, see https://wiki.ubuntu.com/DashAsBinSh
For bash vs dash, see following presentations at
http://princessleia.com/plug/2008-JP_bash_vs_dash.pdf
http://tinyurl.com/3mv8gy

 

Check which “sh” shell

Use below code to check which shell sh is pointing to
ls -l /bin/sh

or, get the full path of link using
readlink -f /bin/sh
or
python -c 'import os.path; print(os.path.realpath("/bin/sh"))'

Change “sh” shell

Create a symlink for sh to bash using the following command
sudo ln -sf bash /bin/sh

You can revert back to the system default by
sudo ln -sf dash /bin/sh

 

Note: Default login shell is still bash. Also when you open a terminal, the default shell for the terminal is bash. you can check this using echo $SHELL or ps

Startup files for bash

Startup files are invoked depends upon the shell category, such as login/non-login, interactive/non-interactive.

see some of the examples under these shell category

Shell Invocation Login Interactive
ssh login yes yes
Opening terminal window(default) no yes
with bash -l or -login flag yes *
Shell script (#!/bin/bash) no no

 
A login shell reads startup files in the following order:

  • /etc/profile
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile
Note: By default, the file ~/.profile contain the following lines
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

So ~/.bashrc file also sourced for login shell, if it exists

 
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc
 
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.

For more info, see man page of bash

Note: If you want to set some environmental varibles like PATH globally, you can do it in ~/.bashrc. For more info on “how to set and where to set”, see EnvironmentVariables

 
For more detailed information about the shell, like login/non-login, interactive/non-interactive shells and their start up files, see
http://docs.slackware.com/howtos:cli_manual:shells

Run Matlab script from a shell script – using command line

Suppose you have a matlab script file matlab_func.m inside the directory
matlab_script_directory. Also matlab_func(string_argument,numerical_argument) takes the string_argument in the form ‘some string’ and numerical_argument (like 28, 49.0 etc).

In your bash script, you want to execute matlab_func() with arguments passed from bash script and return to the bash shell for executing further commands following it. Use the following in your bash script

# Create Matlab script file 
temp_mat_script="matlab_script_directory/run_mat_script.m"
string_argument="some string"
numerical_argument="28"
echo "matlab_func('$string_argument',$numerical_argument);" > $temp_mat_script # create new file and write 
echo "exit" >> $temp_mat_script # append to file

# Execute Matlab script file
curr_dir=$PWD
cd matlab_script_directory
matlab -nojvm -nodisplay -nosplash -r run_mat_script 
cd $curr_dir

# Delete temporary file
rm $temp_mat_script

 
Note: Instead of changing directory to matlab_script_directory, you can use
echo "addpath matlab_script_directory" > matlab_script_directory/,
if run_mat_script.m is created in your current directory.

 

Note: If the “matlab” command ( application script file) location is not
included in the PATH variable, then you need to specify as Path_To_Matlab_Command/matlab.Otherwise add it to PATH variable as

export PATH=$PATH:Path_To_Matlab_Command

You can add the above code to ~/.bashrc file, to make it permanent.

Get list of files within directories and subdirectories recursively and Format the display of file list

If you want display a list of files under a directory recursively, see the following example.
I have the following directory structure "some_directory/sudirectory*/*.filetype"
I wanted to display file list in the following format

--------------------------
sudirectory1/1.filetype
sudirectory1/2.filetype
...
sudirectory2/1.filetype
...
...
--------------------------

 

You can use one of the following methods to do the above task.

  1. Using “ls” command

              cd some_directory
              ls -1 */*.filetype
              

    Here -1 option is provided to list one file per line.

    Note: Don’t use the above wild card search pattern in double quotes,
    i.e, ls -1 "*/*.filetype" will not work.

  2.  

  3. Using “find” and “awk” command

    Note: For familiarizing with “awk”, see some “awk” example at awk.examples

    • Method-1: using awk with field separator

                find some_directory -name "*.mfc" -type f | awk -F"some_directory/" '{ print $2 }'
                

      Here we used awk command, for not to display some_directory/ in front of the file names.

    •  

    • Method-2: From current directory, using awk with field separator for “./”

                cd some_directory
                find . -name "*.mfc" -type f | awk -F"[.]/" '{ print $2 }'
                

      Here we used awk command, for not to display “./” in front of the file names.

      Note: If you use "./" for matching, then as per regular expression it will match “any_character/”, ie “a/”, “b/” etc. will be matched. To use dot “.” as such for matching, use “[.]

    •  

    • Method-3: From current directory, using awk with substring substitution

                cd some_directory
                find . -name "*.mfc" -type f | awk '{ gsub("[.]/",""); print }'
                

      Here we used awk command as a way for string substitution, so as not to display “./” in front of the file names.

      Note: If you use "./" for matching, then as per regular expression it will match “any_character/”, ie “a/”, “b/” etc. will be matched. To use dot “.” as such for matching, use “[.]

Some differences between usages of “csh” and “bash” scripting

  1. Useful website links
  2. Shell variables assignment
    bash:

    x=3

     

    csh:

    set x = 3

    Note: spaces are important in bash where as it is not important in csh

  3.  

  4. Environmental variables assignment
    bash:

    export z=5

     

    csh:

    setenv z 5

  5.  

  6. Integer Expression
    bash:

    j=$((i-1))

    Or

    let j=i-1

     

    csh:

    @ j = $i - 1

  7.  

  8. for loop
    bash:

                   for i in 1 2 3
                   do
                   echo Iteration Number $i
                   #let j=i-1
                   j=$((i-1))
                   done
                   

     

    csh:

                  foreach i (1 2 3 )
                    @ j = $i - 1
                  end
                

  9.  

  10. Valid and Invalid Variable Substitution
    ${var} will work in both csh and bash.
    But {$var} work in csh but not in bash.
    See eg:

    csh -c "echo {$HOME}1"

     
    will works in csh but in bash we need the following

    bash -c "echo ${HOME}1"

  11.  

  12. Array: declaration, iterate, first item, count
    bash:

                 snr=(clean1 N1_SNR20 N1_SNR15 N1_SNR10 N1_SNR5 N1_SNR0) # array declaration
                 snr_array_length=${#snr[@]} # get array size
                 snr_first_item=${snr[0]} # get first item, starts at index 0
                 first_item_length=${#snr[0]} # get length of first item
                

     

    csh:

                 set snr = (clean1 N1_SNR20 N1_SNR15 N1_SNR10 N1_SNR5 N1_SNR0) # array declaration
                 set snr_array_length = $#snr # get array size
                 set snr_first_item = $snr[1] # get first item, starts at index 1
                

  13.  

  14. While loop with array
    bash:

                 snr=(clean1 N1_SNR20 N1_SNR15 N1_SNR10 N1_SNR5 N1_SNR0)
                 count=0
                 snr_array_length=${#snr[@]}
                 while [ $count -lt $snr_array_length ]
                 do
                    printf "\n Noise condition: %s\n" ${snr[$count]}
                    let count+=1
                 done
                 

     

    csh:

                 set snr    = (clean1 N1_SNR20 N1_SNR15 N1_SNR10 N1_SNR5 N1_SNR0)
                 set count  = 1
                 while ($count <= $#snr)
                    printf "\n Noise   condition: %s\n" $snr[$count]
                    @ count++
                 end
                 

  15.  

  16. Behaviour of “Exit” command with sourcing
    In bash:
    “exit” command, will exit the shell where the “source” command was executed, as it is that shell which reads each command in turn and executes it. In contrast in a standalone script (if executed, not sourced) the “exit” command will exit only out of the shell/interpreter which was started to execute the commands in the script. Therefore the executed script file will just stop and return to the shell which called it.
    The danger is therefore if a standalone script is sourced instead of being executed. If in that script file there is an “exit” command then the shell which called the script will be exited and not just the script.

    For example, content of ‘sample_bash.sh’ is

                 #!/bin/bash
                 echo "bash-Shell script started"
                 exit
                 echo "bash-Shell script not exited from sample_bash.sh"
                 

    then output of

                 bash -c 'bash sample_bash.sh
                  echo "not exited completely"'
                 

    is

    bash-Shell script started
    not exited completely

     
    But output of

                 bash -c 'source sample_bash.sh
                  echo "not exited completely"'
                 

     

    is

    bash-Shell script started

     

    In csh:
    “exit” command in sourced script, will not behave as expected. It just stop and return to the shell which called it.
     
    For Example, output of

                 csh -c 'source sample_csh.csh
                  echo "not exited completely"'
                 

    is

    C-Shell script started
    not exited completely

  17.  

Useful website links containing linux and shell script tips

  1. Shell script tutorial links
  2.  

  3. String Manipulation

     

    • Substring Removal

      ${string#substring}
      Deletes shortest match of $substring from front of $string.
      ${string##substring}
      Deletes longest match of $substring from front of $string.
      ${string%substring}
      Deletes shortest match of $substring from back of $string.
      ${string%%substring}
      Deletes longest match of $substring from back of $string.

     

    • Substring Replacement

      ${string/substring/replacement}
      Replace first match of $substring with $replacement.
      ${string//substring/replacement}
      Replace all matches of $substring with $replacement.
      ${string/#substring/replacement}
      If $substring matches front end of $string, substitute $replacement for $substring.
      ${string/%substring/replacement}
      If $substring matches back end of $string, substitute $replacement for $substring.

  4.  

  5. Website hosting on local host

Useful website links containing wordpress tricks

  1. For scroll boxes and useful text formatting tips

    see http://wpbtips.wordpress.com/

     

    Note

    • Code for vertical scroll box
      <div style="height:150px;white-space:pre-wrap;overflow:auto;padding:8px;">
      
    • Code for horizontal scroll box
      <div style="white-space:pre;overflow:auto;padding:8px;">
      

Set as block of text by light gray background

To set block of text in separate background.

Use the below code

<div style="background-color:#f0f0f3;padding:0.8em;">
Your block of Text here
Your block of Text here
Your block of Text here
Your block of Text here
</div>

It will perceive as a separate note or block like this

Your block of Text here
Your block of Text here
Your block of Text here
Your block of Text here

Display lines containing a pattern from a file using “grep”

To display lines which contains “pattern” from a file. Use grep command as

grep pattern file_name

Use -v option to display lines that does not contain pattern

Eg:-
Here I want to include lines containing “testa/N1_SNR” pattern, but exclude lines containing “testa/N1_SNR-” pattern and concatenate with content of other file.

I use below code

{ grep "testa/N1_SNR" all_hcopy.scp | grep -v "SNR-" ; cat CleanTR08_hcopy.scp ; } > only_testa_N1.scp

Use { cmd1; cmd2; } to execute all commands in the current shell
see http://mywiki.wooledge.org/BashSheet#Command_Lists

Grep recursively, but only certain file extensions

Use the below code
grep -r --include="file_pattern" "Pattern_To_search" Directory_Path
For example:
To search all the files with extension “.m”, and find lines in these files containing the word gmm
grep --include="*.m" -nRHIi "gmm" .

Note: Here the options specified are
-r or -R for recursive search
-n Prefix each line of output with the line number within its input file.
-H Print the filename for each match
-I Process a binary file as if it did not contain matching data; this is equivalent to the –binary-files=without-match option
-i for case insensitive search

 

See more usages of grep command at using-grep-regular-expressions-to-search-for-text-patterns-in-linux