Sunday, 14 December 2025

Why Learning Faster Matters More Than Ever


In today’s fast-moving tech world, learning efficiently is more important than learning everything. Most professionals struggle not because they lack intelligence, but because they use ineffective learning strategies.

Experienced engineers, architects, and high performers rely on proven learning principles that maximize results while minimizing wasted effort.

This article summarizes the most popular and effective learning principles—especially useful for technology, engineering, and skill-based learning.


1. Pareto Principle (80/20 Rule)

Definition: 80% of results come from 20% of effort.

How it applies to learning:
A small set of core concepts delivers most real-world value. Focus on mastering those first.

Example:

Best for: Fast skill acquisition, professionals with limited time


2. Feynman Technique

Definition: If you can’t explain something simply, you don’t truly understand it.

How it works:

  1. Learn a concept
  2. Explain it in simple language
  3. Identify gaps
  4. Simplify again

Example:
Explaining REST APIs as “a way applications communicate using URLs and HTTP verbs.”

Best for: Deep understanding and clarity


3. First Principles Thinking

Definition: Break complex ideas down to fundamental truths and rebuild from scratch.

Example:
Instead of memorizing Kubernetes YAML, understand:

Best for: System design, architecture, complex technologies


4. Active Recall

Definition: Actively retrieving information strengthens memory better than rereading.

Example:

Best for: Long-term retention and interviews


5. Spaced Repetition

Definition: Review information at increasing intervals just before you forget it.

Typical pattern:

  • Day 1
  • Day 3
  • Day 7
  • Day 21

Best for: Commands, definitions, vocabulary


6. Just-in-Time Learning

Definition: Learn concepts exactly when you need them.

Example:
Learning Docker volumes only when persistence becomes a requirement.

Best for: Developers working on real projects


7. Learning by Doing (Experiential Learning)

Definition: Skills improve fastest through hands-on practice.

Example:
Deploying a broken CI pipeline and debugging it instead of watching tutorials.

Best for: Engineering, DevOps, tooling


8. Bloom’s Taxonomy

Learning depth levels:

Level Description
RememberRecall facts
UnderstandExplain concepts
ApplyUse knowledge
AnalyzeCompare and contrast
EvaluateJudge best solutions
CreateBuild something new

Goal: Aim for Apply → Analyze → Create, not just “understand”.


9. Cognitive Load Theory

Definition: The brain has limited capacity—too much information reduces learning quality.

Example:
Learn Git basics before Git internals.

Best for: Beginners and complex topics


10. Deliberate Practice

Definition: Focused practice with feedback on weak areas.

Example:
Refactoring poorly written code into clean, maintainable architecture.

Best for: Mastery and senior-level growth


Recommended Learning Stack (Best Combination)


Final Thoughts

Learning faster isn’t about shortcuts—it’s about focus.

When you apply these principles consistently, you:

  • Avoid tutorial fatigue
  • Retain knowledge longer
  • Learn like experienced engineers

Use learning principles deliberately, and your growth will compound over time.

Saturday, 3 June 2023

Useful Git Commands

Comprehensive Guide to Essential Git Commands

Introduction

Welcome to this comprehensive guide on essential Git commands. This blog covers everything from basic configuration to advanced branching strategies. Whether you're new to Git or looking to sharpen your version control skills, you'll find practical examples and best practices throughout.

---

1. Initial Setup & Configuration

**Purpose:** Configure your Git identity for commits and version tracking.

**Commands:**

```
git config --global user.email "phani@soft.com"
git config --global user.name "phani"
```

**Description:** These commands configure your user information globally across all repositories on your system. This information will appear in all your commits.

---

2. Creating & Initializing a Project

**Purpose:** Initialize a new Git repository for version control.

**Command:**

```
```

**Description:** Creates a `.git` folder in your project directory, initializing it as a Git repository. This is the first step when starting a new project.

---

## 3. Checking Project Status

**Purpose:** View the current state of your working directory and staging area.

**Command:**

```

git status

```

**Description:** Displays modified files, staged files, and untracked files in your repository. Use this frequently to understand what changes exist.

---

## 4. Staging Files

**Purpose:** Prepare files for commit to the repository.

**Commands:**

```
git add FILE_NAME       # Stage a specific file
git add .              # Stage all changes
```

**Description:** Staging marks files as ready to be committed. Think of it as the intermediate step between making changes and recording them.

---

**Purpose:** Save staged changes to your repository with a descriptive message.

3. Checking Project Status

4. Staging Filesgit commit -m "Your commit message"

```

**Example:**

```
git commit -m "Add user authentication feature"
```

**Description:** Commits record snapshots of your project. Use clear, descriptive messages to document what changed and why.

---

## 6. Viewing Commit History

**Purpose:** Review all commits and changes in your repository.

**Command:**

```
```

**Description:** Displays a chronological list of all commits with author, date, timestamp, and message. Essential for tracking project evolution.

---

## 7. Working with Branches

**Purpose:** Create isolated workspaces for different features without affecting the main codebase.

**Commands:**

```
git branch                  # List local branches
git branch --all           # List all branches (local + remote)
```

**Description:** Branches allow you to work on different features simultaneously. Each branch is an independent line of development.

---

## 8. Switching Branches

**Purpose:** Move your working directory to a different branch.

**Modern Approach (Git 2.23+):**

```
```

**Legacy Approach:**

```
```

**Description:** Switching branches updates your working files to match the selected branch. Use the modern `git switch` for clarity.

---

## 9. Merging Branches

**Purpose:** Integrate changes from one branch into another.

**Command:**

```
```

**Description:** Merging creates a merge commit that combines changes from both branches. This preserves complete history but can create complex commit graphs.

---

## 10. Handling Detached HEAD State

**Purpose:** Recover from accidental checkout of specific commits.

**Problem Scenario:**

```
git checkout HASH-CODE-COMMIT    # Enters detached HEAD state
```

**Solution - Create a Branch:**

```
```

**Description:** When checking out a specific commit instead of a branch, you enter a detached HEAD state. Creating a branch preserves your changes and prevents data loss.

---

## 11. Git Rebase

**Purpose:** Rewrite commit history by applying commits on a new base.

**Command:**

```
```

**Key Differences from Merge:**

**git merge:**
- Creates a new merge commit
- Preserves complete history of both branches
- Results in a non-linear commit graph

**git rebase:**
- Rewrites commit history
- Moves commits onto a new base
- Creates a linear, cleaner history

**Example Workflow:**

```
git checkout feature
git rebase master
```

**Description:** Git replays commits from the feature branch on top of the latest commit in master, creating a linear history.

---


**Purpose:** Create named labels for important commits (releases, milestones).

**Commands:**

```
git tag TAG-NAME                # Create a tag
git checkout TAG-NAME           # Checkout a specific tag
```

**Description:** Tags mark specific points in repository history. Commonly used for release versions (v1.0.0, v2.1.3, etc.).

---


**Purpose:** Synchronize local repository with remote repositories.

**Commands:**

```
git pull        # Fetch and merge remote changes
git push        # Upload local commits to remote
git fetch       # Download remote changes without merging
git merge       # Integrate fetched changes
```

**Description:** 
- **pull** = fetch + merge (combines downloading and integrating)
- **fetch** = download only (safe, doesn't modify working directory)
- **push** = upload your commits to the remote repository

---

## 14. Best Practices Summary

### Branching Strategy
- Use feature branches for new development
- Keep main/master branch stable
- Delete merged branches regularly

### Commit Practices
- Write clear, descriptive commit messages
- Make commits logically grouped
- Avoid "I fixed stuff" commits

### Merging vs. Rebasing
- Use **merge** for shared/main branches (preserves history)
- Use **rebase** for feature branches (cleaner history)
- Never rebase public/shared branches

### Tagging & Releases
- Tag every release with semantic versioning
- Use annotated tags for releases
- Document what changed in each tag

---

## Conclusion

Mastering these Git commands will transform you from a basic user into a proficient developer capable of managing complex projects efficiently. Start with the basics, practice regularly, and gradually incorporate advanced techniques like rebasing and strategic branching. Your future self will thank you for maintaining clear, organized version control!

**Key Takeaway:** Git is not just for backup—it's a powerful collaboration and project management tool. Use it wisely.

Friday, 17 July 2020

IntelliJ IDEA Short Cuts

IntelliJ IDEA Short Cuts


sout =>system.out.println
get+Space => to generate getters 
cmd+o =>search files
presentation assistant plugin
Shift+ctlr +F =>find file content
Ctrl+F12 =>  File Structure

Call Hierarchy => Ctrl+Alt+H
Find usage: Alt+f7

Open windows: each window have one number ex: project explorer has one number then command => Alt + 1
Shift+f12 =>reset layout

without Using Mouse:

Alt + 1 => project window
up arrow and down arrow => for navigating files 
left arrow and right arrow => expanding folders
enter specific file => to open file
Esc => for switching cursor to open file
Ctrl + y => delete line

AceJump plugin => use to move specific line in a file

Ctrl + ; and UpArrow => to activate and enter specific letter to move cursor to specific line
Esc => exit from AceJump

Alt + Insert => for creating new package or class etc...
Alt + Insert => setters and getters when cursor in file
Ctrl + Alt + - => expand/collapse function in file
Shift + Alt + - => expand/collapse all function in file

sout => system.out.println()
Alt + Ctrl + v => extract content to a variable
Alt + Ctrl + n => opposite to above
Ctrl + w => to select text
Alt + Ctrl + M => Extract to Method
Alt + Ctrl + p => extract data to a variable
Ctrl + D => duplicate line
Ctrl + Backspace => delete word start

Ctrl + shift + t => create tests 

Ctrl + F8 => toggle break point

Debugging:
Step over / into F8 / F7
Smart step into/Step out Shift + F7 / Shift + F8
Run to cursor Alt + F9
Evaluate expression Alt + F8
Resume program F9

Git:
Annotate => see the person comitted to that file
Ctrl + K => commit window opens
Alt + ` => for GIT operations

Reference:

Sunday, 20 May 2018

Useful Unix Commands for Developers -3

man bash
man bash | wc -l
info bash | wc -l
http://goo.gl/S2Jj2n

execute the script
Make scripts executable with chmod u+x
chmod +x file.sh

The bash time command
Bash has a builtin time command to report how much time a process consumed
time find / -name File_name

Variables in Bash
for a shell script to get a copy of a shell variable, it needs to be "exported":
export mynewvar or declare -x mynewvar

you can export and assign in the same statement:
export var2="var2 value".

export -f myFunc will export the function myFunc.

a=1
(a=2)
echo $a
prints 1

a=1
{a=2}
echo $a
prints 2

see built in commands
$enable

see bash keywords
$compgen -k

Bash startup
.bash_profile is read when bash is invoked as alogin shell.
.bashrc is executed when a new shell is started.
if you extend an exported variable, like PATH, in .bashrc, it will grow with each nested shell invocation.

PATH=$PATH:/usr/local/bin
This would keep adding /usr/local/bin to the end of PATH within nested shells.

Aliases and functions should normally be defined in .bashrc.

Sourcing Scripts:
source example.sh, or . example.sh
it is "dot space" as a short way to source a script.
The shell executed the script in the shell's own process instead of in a new process.

Sourcig is common way to import variable assignments or functions
The sourced script is executed within the calling shell's process.

Wroking with Aliases
The alias command allows for short command alternatives:  alias ll="ls -l"
some people use an alias to give an alternative, more familiar name or to fix a common typo to a Linux command.

$source ./script_file.sh
Note: what this script contains we can use variable in further.


Working with Aliases
alias copy=cp
alias rn=mv
alias mroe=more
mroe myfile
  ls -l | mroe
List defined aliases by simply typing alias
Unset an alias with the unalias command


Using the Echo Command
Built into bash and doesn't start a new process
-n ->don't print a trailing newline
-e ->enable backslash escaped characters like \n and \t
-E ->disable backslash escaped characters in case they were enabled by default

Ex:e
echo Hello World
echo -n Good to see you "\n\n"
echo Thanks
echo -e Hi "\t\t\t" There "\n\n"
echo -E Bye "\t\t\t" For now "\n\n"

ls * => would list contents of directories
echo * =>would show file and directory names

Use file redirection techniques to send the output to other files, such as stderr:
echo 'Warning will Robinson!' >&2

Local Variables and Typeset
Variables can be created in function that will not be available outside of it.
The typeset command makes variables local, can provide a type, or can provide formatting.
typeset -i x
#x must be an integer

Arithmetic is faster for variables defined to be integers.
Let allows for convenient arithmetic:
let x++; let y=x**2; let x=x*3; let x*=5, ....

The Declare Command
declare -l uppercase values in the variable are converted to lowercase.
declare -u lowercase values in the variable are converted to uppdercase.
declare -r variable is made read-only.

declare -a MyArray will make MyArray an indexed array.
declare -A MyArray2 will make MyArray2 an associative array.

function f1 {
   typeset x
   x=7
   y=8
}
x=1
y=2
echo x is $x
echo y is $y
f1
echo x is $x
echo y is $y
output:
x is 1
y is 2
x is 1
y is 8
-------------------
declare -l lstring="ABCdef"
declare -u ustring="ABCdef"
declare -r readonly="A Value"
declare -a Myarray
declare -A Myarray2

echo lstring = $lstring
echo ustring = $ustring
echo readonly = $readonly
readonly="New Value"
Myarray[2]="Second Value"
echo 'Myarray[2]= ' ${Myarray[2]}
Myarray2["hotdog"]="baseball"
echo 'Myarray2[hotdog]= ' ${Myarray2["hotdog"]}

Output:
lstring = abcdef
ustring = ABCDEF
readonly = A Value
declare.sh: line 11: readonly: readonly variable
Myarray[2]=  Second Value
Myarray2[hotdog]=  baseball


The Read Command
Read a line into a variable or multiple variables
read a b---reads first word into a and the rest into b
convenient for a while loop

While Loops

While
command list1
do
command list
done
#loops while command list 1 succeeds

Ex:
while ((x<10))
do
echo loop $x;
date >date.$x
((x++))
done

-------------
while
   read a b
do
echo a is $a b is $b
done <data_file
-------------
ls -l | while
read a b c d
do
   echo owner is $c
done

--------------
For Loops
for <var> in <list>
do
command list
done

for i dog cat elephant
do
echo i is $i
done
----------------
seq 1 5
#prints 12345

for num in `seq 1 5`
#loops over 1 2 3 4 5

generate sequence with {A..Z}, {1..10}

for d in $(<data_file)
#loops over space/newline
#separated data in data_files

-----
for j in *.c
#making a list with file globbing

----
for f in $(find . -name *.c)
#using a command to generate a list

-----------
export a=first
export b=second
export c=third
echo a is '['$a']' b is '['$b']' c is '['$c']'
read a b <data_file
echo a is '['$a']' b is '['$b']' c is '['$c']'

data_file:
dog cat rooster
elephant hen rabbit snake
carrot lettuce pineapple banana pizza


output:
a is [first] b is [second] c is [third]
a is [dog] b is [cat rooster] c is [third]

--------
ls -l /etc | while
     read a b c d
do
     echo owner is $c
done
----------------
nl for.sh  => nl number line

     1 #!/bin/bash
     2 for i in dog cat hotdog
     3 do
     4    echo i is $i
     5 done
     6 for i in `seq 3 5`
     7 do
     8     echo i in seq is $i
     9 done
    10 for i in {N..P}
    11 do
    12     echo i in letter list is $i
    13 done
    14 for d in $(<data_file)
    15 do
    16      echo d in data_file is $d
    17 done
    18 for f in $(find /etc 2>/dev/null | grep grub)
    19 do
    20      echo grub named things are $f
    21 done


Functions:
Give a name to a sequence of statements that will execute within the shell, not in a new process
function NAME {
  function body........
}

commonly used to organize code in a shell program
function printhello{
echo Hello
}

printhello
#shell memorizes the function like it's a new command

Return Command:
fucntions return when there are no more statements or when a return statement is executed.
function myFunc{
echo starting
return
echo this will not be executed
}

The return command:
functions produce results by writing output like commands do.
hvar=$(printhello)

The Exit command:
exit <value> sets the exit status, represented by $? to <value>
exit terminates the shell process
exit in a function terminates the whole shell program, not just the function.

Ex:
function myfunc {
   echo starting myfunc
   return
   echo this will not be executed
}

myfunc
n=$(myfunc)
echo n is $n

Output:
starting myfunc
n is starting myfunc

---------------------
Redirection and pipes:

Processes normally have three files open:
 0 =>stdin, 1 =>stdout, 2 =>stderr
Command > stdout-here 2> stderr-here < stdin-from-here
Command &> file
 #file gets stdout and stderr from command, file is created or overwritten

Redirection and Pipes:
command | command2
 #command2's stdin comes from command's stdout
command 2>&1 | command2
 #gets stdout and stderr from command

command |& command2
 #alternative way for command2 to get command's stdout and stderr as its stdin

command >> file
 #appends stdout of command to end of file

command &>> file
 #appends stdout and stderr of command to end of file

Here Documents: <<
Here documents are a way to embed input for standard input inside of a script.
They avoid having to create a new file just to hold some input values.

sort <<END
cherry
banana
apple
orange
END

Open and Close file descriptiors:
exec N< myfile
 #opens file descriptor N for reading from file myfile
exec N> myfile
 #opens file descriptor N for writing to myfile

exec N<> myfile
 #opens file descriptor N for reading & writing with myfile
exec N>&- or exec N<&-
 #closes file descriptor N

Use lsof to see what file descriptors for a process are open
exec 7>/tmp/myfile7
lsof -p $$
 # $$ is shell's PID



echo Just '>' ---------------------------------------
find /etc -name grub >grub.out
echo Doing '2>' ---------------------------------------
find /etc -name grub 2>errs.out
echo Doing '&>' ---------------------------------------
find /etc -name grub &>both.out

> only standard output
2> only standard error
&> both standard output and errors


find /etc -name grub |& grep grub
|& => first command success ouput without errors passes to input to right side of pipe command


echo hi >myfile
replace content myfile with hi
echo cheese >>myfile
append data at bottom of myfile.

Sorting:

sort <<END
<content>
END

Ex:
sort <<END
cherry
banana
apple
orange
END

while
read a b c
do
    echo a: $a b:$b c:$c
done <<EOF
   one two three four
   five six seven eight nine ten
   eleven twelve
EOF

The Case Statements:

case expression in
pattern 1)
command list;;
pattern 2)
command list;;
...
esac
ex:
case $ans in
yes|YES|y|Y|y.x ) echo "will do!";;
n*|N*) echo "will not do!";;
*) echo "Oops!";;
esac

The If-Then-Else Statement
if
command list # last result is used
then
command list
[else
command list]
fi

EX:
if
grep -q important myfile
then
echo myfile has important stuff
else
echo myfile does not have important stuff
fi

Tests in Bash
The builtin test is used to check various conditions and set the return code with the result.
Loops and conditionals often use the result of test
An alternative to tet is [[]] or (())

ex: Test Example
if
test -f afile
if [[ -f bfile ]] #instead of 'test' [[]]
if
test $x -gt 5

Test Operator

Numeric Comparision:
[[ ex1 -eq ex2 ]]  [[ ex1 -nq ex2 ]]
[[ ex1 -lt ex2 ]]  [[ ex1 -gt ex2 ]]
[[ ex1 -le ex2 ]]  [[ ex1 -ge ex2 ]]

or

(( ex1 == ex2 ))  (( ex1 != ex2 ))
(( ex1 < ex2 ))   (( ex1 > ex2 ))
(( ex1 <= ex2 ))  (( ex1 >= ex2 ))
(( ex1 && ex2 ))  (( ex1 || ex2 ))

(( expr?expr:expr))

test -d X   => success if X is a directory
test -f X   => success if X is a regular non-empty file
test -s X   => success if X exists non-empty

test -x X   => success if you have x permission on X
test -w X   => success if you have w permission on X
test -r X   => success if you have r permission on X

Ex:
x=01
y=1
echo comparing $x and $y
if
[ $x == $y ]
then
   echo ==
else
   echo not ==
fi
if
[ $x -eq $y ]
then
   echo eq
else
   echo not eq
fi

if
((x==y))
then
   echo '(())' ==
else
   echo not '(())' ==
fi

Ex: 2

if
   test -x /bin/ls
then
   if
   [ ! -w /etc/hosts ]
   then
      if
      echo about to look for foobar
      grep -q foobar /etc/passwd
      then
         echo foobar found in /etc/passwd
      else
         echo foobar not found
      fi
   fi
else
   echo Oh no, /bin/ls not executable
fi


Filters:
In Linux, a program is a filter if it reads from stdin and writes to stdout.
Filters can be used in pipes.
Filters provide the powerful means of combining the input and output of a sequence of commands to get the kind of report that you want.

The Head and Tail Commands
head prints the first n lines of a file or stdin.
tail prints the last n lines of a file or stdin
ls -l |head -5 #first 5 lines of ls -l
ls -l |tail -7 #last 7 lines of ls -l
ls -l |head -10 |tail -5 #lines 6-10

The Head and Tail Commands
wc(word count) prints line, word, and char counts
wc -l prints the number of lines
ls |wc -l prints number of entries in directory.

$./makeoutput.sh >output &  => end & uses to run process background
$tail -n2 -f output

#!/bin/bash
for i in {1..100}
do
    read a b c d e <<END
    $(date)
END
    echo $d
    sleep 1
done

The Command sed:

is a stream editor, which means it is not interactive
works great as a filter
is ideal for batch editing tasks
Usually applies its editing to all lines in the input
with the -i option, change a file instead of echoing the modified file to stdout

Using sed Substitute:
sed 's/old/new/' myfile

Substitute the first occurrence of old on each line for new in the file myfile and display the result on stdout
old is a pattern and can be regular expression
The / is the usual character to separate the old from the new.
the file myfile will not be changed; the new version is echoed to stdout
No options are required for simple substitutions.

Ex:
sed 's/@home/@domicile/; s/truck/lorrie/'
sed -e 's/[xX]/Y/' -e 's/b.*n/blue/'
sed -f sedscript -n sed4
date | sed 's/J/j/'
sed '1,5p'
sed '/alpha/s/beta/gamma/'
sed '/apple/,/orange/d'
sed '/important/!s/print/throw_away/'

The awk Language:
A pattern matching language
An interpreted programming language that orks as a filter
Good for report writing
Handy for short algorithmic kinds of processing
processes a line at a time like sed
breaks each line into fields, $1, $2, etc.,
Fields are delimited by the values in the variable FS, normally white space.
$0 is the entire line(record)

EX:
$ps -el | \
awk '/pts/||$8~/35/{printf("%5d %5d %s\n", $4, $5, $14)}'

Ex:
sed2:
s/a/A/
s/B/BBB/

$ cat sometext:
now we have
some words and
fruit like apple cherry orange peach
and BIG things like
cruise ship, skyscraper, Bigfoot

$sed -f sed2 sometext
now we hAve
some words And
fruit like Apple cherry orange peach
And BBBIG things like
cruise ship, skyscrAper, BBBigfoot

Script Parameters and {}:
Parameters to a shell program: $1, $2
called "positional parameters"
to reference multidigit use {},e.g., ${10}
$0 is the path to the program itself:
For ex, echo Usage: $0 arg1 .....
Shift moves $2 into $1,$3 into $2, etc.
It is sometimes handy or required to use{} with named variable,. e.g., echo ${abc}DEF.
x=abc
abc=def
echo ${!x} prints def. indirection!

Unset or Null Variables:
${variable <OPR> value}
x=${var: -Hotdog}

:- if var unset/null , return value; otherwise, return value of var
:= if var unset/null var is assigned value & returned
:? Display an error and exit script if var unset/null
:+ if var unset/null return nothing; otherwise, return value

String Operations:
${var:offset}-value of var string at offset
${var:offset:len}-value of vr starting at offset upto length len
${#var}-length of var
${var#pre}-remove matching prefix
${var%post}-remove suffix-
Prefix and postfix -handy for processing filenames/paths

ex:#!/bin/bash
echo arg1 is $1 arg 11 is ${11}
shift
echo now arg1 is $1 arg 11 is ${11}
echo program is $0

$bash pos.sh {A..Z}
arg1 is A arg 11 is K
now arg1 is B arg 11 is L
program is pos.sh


ex:
x=abc
abc="Start Of Alphabet"
echo x is $x
echo abc is $abc
echo '${!x}' is ${!x}

output
x is abc
abc is Start Of Alphabet
${!x} is Start Of Alphabet


Ex:
#!/bin/bash

unset x
a=${x:-Hotdog}
echo a is $a
echo x is $x

a=${x:=Hotdog}
echo a is $a
echo x is $x

unset x
${x:?}
echo Will not get here

output:
a is Hotdog
x is
a is Hotdog
x is Hotdog
./unsetnull.sh: line 13: x: parameter null or not set

Ex:
#!/bin/bash
s="a string with words"
sub=${s:4}
echo sub is $sub
sub=${s:4:3}
echo sub is $sub
echo length of s is ${#s}

output:
sub is ring with words
sub is rin
length of s is 19

Ex:
p="/usr/local/bin/hotdog.sh"
echo whole path is $p
echo Remove prefix ${p#/*local/}
echo Remove suffix ${p%.sh}
cmd=${p#*/bin/}
cmd2=${cmd%.sh}
echo the command without .sh is $cmd2

output:
whole path is /usr/local/bin/hotdog.sh
Remove prefix bin/hotdog.sh
Remove suffix /usr/local/bin/hotdog
the command without .sh is hotdog


Advanced Bash:
Using Coprocesses:
a coprocess is a background process where your shell gets file descriptors for the process's stdin and stdout. Implemented with pipe

We need a script that is a filter.

#!/bin/bash
while
  read line
do
  echo $line | tr "ABC" "abc"
done

Using Coprocesses:
coproc ./mycoproc.sh
echo BANANA >&"${coproc[1]}"
cat <&"${COPROC[0]}"

or

coproc my { ./mycoproc.sh; }
echo BANANA >&"${my[1]}"
cat <&"${my[0]}"


Debugging script:
tee
 ex: cmd | tee log.file | ...


Sunday, 13 May 2018

Useful Unix Commands for Developers -2

rmdir
more book.txt page by page
head book.txt head
tail book.txt head
~ users home directory
~- previous directory
touch {apple,banana,cherry,durian} =>creates these files
touch file_{1..100} =>create 100 files
echo {1..10..2} => 2 represents intervel
echo {1..10..3}
echo {A..Z}
echo {A..z}
echo {w..d..2}
ls | more
input from commandline environment and output to it and error arises something goes wrong.
cp -v ../otherfolder 1>../success.txt 2>../error.txt
success will go to the success.txt and error goes to error.txt
here we are coping results to file instead of console.

----------
Redirecting output (bother success and error  to log file
cp -v * ../otherfoler &> ../log.txt

ignoring output of a command  "/dev/null"
ls > /dev/null

grep -i break-in auth.log | awk {'print $12'}
man awk
man sed =>used for string manipulation
ping example.com
ping -c 1 example.com | grep 'bytes from'
ping -c 1 example.com | grep 'bytes from' | cut -d = -f 4
f =>field 4
d => delimiter

#!/bin/bash
Hashbang or shebang, path to the bash executable

nana my.sh

#!/bin/bash
#this is a basic bash script

ls

ctrl o
ctrl s


bash my.sh
chmod +x my.sh
./my.sh

If you put your script file to /usr/bin then the file will run directly by just typing $my.sh instead of $./my.sh
sudo cp my.sh /usr/bin

echo no-quoates   => If you write a statement with no quotes, Bash goes along and interprets things as it finds them.
echo single-quoates =>There's also single quotes or strong quotes, where nothing inside of the quotes gets interpreted. So everything comes out literally, even if I put a variable inside
echo double-quoates =>Bash display something literally inside of double quotes, you could escape it with backslash

Adding attributes to variables
declare -i d=420 # d is an integer
declare -i e=555 #e is read-only
declare -l f="LOLCats" # f is lolcats
declare -u g="LOLCats" #g is LOLCATS

Built-in variables
echo $HOME => returns user home directory[Linux: /home/phani]
echo $PWD =>returns current directory [Linux: /home/phani]
echo $MACHTYPE =>returns machine type [Linux: x86_4-pc-linux-gnu]
echo $HOSTNAME =>returns system name [Linux: orion]
echo $BASH_VERSION => returns version of Bash[Linux:4.2.25(1)-release]
echo $SECONDS=> returns the number of seconds the Bash session has run
echo $0 =>returns the name of the script
d=$(pwd)
echo $d
a=$(ping -c l example.com | grep 'bytes from' | cut -d = -f 4)
echo "the ping was $a"


Arithmetic operations
val=$(( expression ))
exponentiation - $a ** $b
multiplication - $a * $b
division - $a / $b
modulo - $a % $b
addition - $a + $b
subtraction $a - $b
increment  => ((e++))), ((e--)) , ((e+=5)) , ((e/=3)) , ((e-=5))
echo $e
if we use
echo $e+=5 => returns 49 concatenation if e=4
echo $((e+=5)) => reruns 9 if e=4
Note: Bash math only works with integers, not floating point numbers.
If you need to get a floating point value you can use the BC program with the predefined math routines to return that.

g=$(echo 1/3 | bc -l )
echo $g => 0.333333333333

Comparison Operations
[[ expression ]]
1: FALSE
0: TRUE
It is only for string comparison :
less than => [[ $a < $b ]]
greater than [[ $a > $b ]]
less than or equal [[ $a <= $b ]]
greater than or equal [[ $a >= $b ]]
equal  [[ $a == $b ]]
not equal [[ $a != $b ]]
Ex:
[[ "cat" == "cat" ]] echo $?
[[ "cat" = "cat" ]] echo $?

For Number comparison:
less than => [[ $a -lt $b ]]
greater than [[ $a -gt $b ]]
less than or equal [[ $a -le $b ]]
greater than or equal [[ $a -ge $b ]]
equal  [[ $a -eq $b ]]
not equal [[ $a -ne $b ]]

Logic operations
Logical AND => [[ $a && $b ]]
Logical OR => [[ $a || $b ]]
Logic NOT  => [[ !$a ]]

String null Value
is null? => [[ -z $a ]]
is not null? => [[ -n $a ]]
Ex: a="" , b="abc"
 [[ -z $a && -n $b ]] =>echo $?

Working with Strings
a="hello"
b="Unix"
c=$a$b
echo $c => hellowunix
get the length of each of those strings, a and c
echo ${#a} => 5
echo ${#c} => 9
certain piece or a substring from an existing string
use a colon followed by an index number to start from. Keep in mind that the first character is at index 0
d=${c:3} , echo $d => lowunix
e=${c:3:4} => lowu
getting four characters at the end of the string
d=${c: -4} , echo $d => unix
e=${c: -4:3} => uni
replace text in the string with some other text.
In this case the string fruit, followed by one slash, the term I want to replace, in this case banana, another slash, and the string I want to replace
the first instance of the search term with. In this case, durian.
Ex:
fruit="apple banana banana cherry"
echo ${fruit/banana/durian} => apple durian banana cherry =>replace only one instance

echo ${fruit//banana/durian} => apple durian banana cherry =>replace all instance
echo ${fruit/#apple/durian} => durian banana banana cherry =>replace begining of string if it exist

echo ${fruit/%cherry/durian} => apple banana banana durian =>replace ending  of string if it exist

Exploring some handy helpers: date and printf
date => Mon Jun 10 10:00:10 UTC 2018
date +"%d-%m-%Y" => 10-06-2018
date +"%H-%M-%S"

printf "Name:\t%s\nID:\t%04d\n" "Phani" "11"
Name: Phani
ID: 0011

today=$(date +"%d-%m-%Y")
time=$(date +"%H-%M-%S")
printf -v d "Current User:\t%s\nDate:\t\t%s @ %s\n" $USER $today $time
echo $d
Current user: phani
Date:         10-06-2018 @ 10:11:12
Note: -v used to ouput the value to a variable as "d"

Working with arrays
a=()
b=("java" "c language" "dot net")
echo ${b[2]} => dot net Note: arras are index based start with 0.
setting array value
b[5]="hadoop"
But if you want to add something to the end of an array, you can use the plus equals operator. Make sure you put the value in parentheses.
b+=("hive")
echo ${b[@]} => represents whole array
echo ${b[@]: -1} => hadoop

Associative arrays[Available only bash4 and above versions]
declare -A myarray
myarray[color]=blue
myarray["office building"]="HQ West" [Note: if we have space use quote]
echo ${myarray["office building"]} is ${myarray[color]}
=> HQ West is blue

Reading and writing text files
echo "xome text" > file.txt
$>file.txt =>remove everything in file
echo "some more text" >> file.txt =>append text in file

reading files
i=1
while read f; do
    echo "Line $i: $f"
((i++))
done < file.txt

freespace = $(df -h | grep -E "\/$" | awk '{print $4}')


Conditional statement

if [ expression ]
if [[ expression ]] =>if you are using regular expression
if (( expression )) =>integer comparison
if expression; then   => no brackets


if expression
then
  echo "true"
elif expression2; then
   echo "true"
else
 echo "false"
fi


i=0
while [$i -le 10 ]; do
echo i:$i
 ((i++))
done

j=0
until [$j -ge 10 ]; do
echo j:$j
 ((j+=i))
done

For Loop:

for i in 1 2 3
do
echo $i
done

for i in {1..100..2}  note : bash4 and above
do
echo $i
done

for (( i=1; i<=10; i++ ))
do
 echo $i
done

arr = ("c" "java" "dotnet")
for i in ${arr[@]}
do
 echo $i
done

Associate array
declare -A arr
arr["name"]="phani"
arr["id"]="1478"
for i in "${!arr[@]}"   =>! represents key
do
 echo "$i: ${arr[$i]}"
done

Command substitution
for i in $(ls)
do
echo "$i: ${arr[$i]}"
done

 case Statement

 a="java"
 case $a in
   dotnet) echo "feline";;
   java|hadoop) echo "canine"
    *)echo "No match!";;
esac

functions
 function numberthings{
i=1
for f n $@;  do
echo $i: $f
((i+=1))
done
}

numberthings $(ls)
numberthings java c dontnet hadoop

Sunday, 15 April 2018

Use Full Git Details


Use Full Git Details
Installing Git Linux: Apt-get install git-core
Windows: http://msysgit.github.com
Git –version
Configuring Git system level configuration git config --global user.name "phani git"
it config --global user.email "venkataphanikumar.r@gmail.com"
git config --global –list
git config --global core.editor vim
git config --global help.autocorrect 1
git config --global color.ui auto
git config --global color.ui auto
cat ~/.gitconfig
git config --global --unset core.editor
git config –sytem
stored in /etc/gitconfig or c:\programFiles\Git\etc\gitconfig
User-level configuration
git config –global
Stored in ~/.gitconfig or c:\Users\name\.gitconfig
Repository-level configuration
git config –global
stored in .git/config in each repo
Creating a local repository
Adding files
Comimting changes
Viewing history
Viewing a diff
Working copy, staging, and repository
Deleting files
Cleaning the working copy
Ignoring files with .gitignore
Initiate link to git
git init
Initialized empty Git repository in /home/phani/code-git/.git/

echo "Hello, Git" > README.txt
git status
Untracked files: README.txt
Add new file to git repo
git add README.txt
git status
Changes to be committed : README.txt
Changes to be committed
git commit
Added README.txt
git log
nano README.txt
Modify the file in second time
git status
Changes not staged for commit ; README.txt
Update file in git repo
git add -u
git status
Changes to be committed: README.txt
git log
git commit -m "updated README.txt"
git log
Changes between 2 commits
git diff a5b807..9a3dd7
(or)
Git diff HEAD~1..HEAD [HEAD= latest version, HEAD~1 = latest -1 version]
(or)
Git diff HEAD~1..
Add new files to stating
git add file1.txt file2.txt
(or)
Git add -A
Git commit -m “adding new 2 files” [-m = comments]
Remove file
rm file2.txt
git status
Changes not staged for commit:
git add -u
git status
git add file3.txt
Mv file1.txt new_file_name.txt
git add -A (-A=add all)
git commit -m "Reorganized" [commit for stating]
Revert our local changes to latest version in git
git checkout README.txt
HARD, SOFT RESET
git reset –hard
git reset --soft HEAD~1
Cleaning the working copy
git clean -f
Ignoring files with .gitignore
creating a local repository
Adding, updating , and deleting files
Commiting changes
Viewing history and diffs
Working copy, staging, and repository
Cleaning the working copy
Ignoring files with .gitignore
Cloning a remote repository
Listing remote repositories
Feching changes from a remote
Merging changes
Pulling from a remote
Pushing changes remotely
Working with tags
git clone https://github.com/jquery/jquery.git
git log –onelineoption [comments for all commits will display]
git log --oneline |wc -l
git log --oneline –graph
git shortlog
git shortlog -sne
git show HEAD [Last commit made]
git log --oneline
git show 2b5f5d5
git remote
git remove -v
Git protocols
http(s) 80/443 – read-write pwd for auth firewall friendly
Git 9418 –read-only anonymous only
Ssh 22 – read-write SSH key for auth
File n/a – read write local only

git branch
git branch -r
git tag

git remote add origin https://github.com/venkataphanikumarr/GitFundamentals.git
Git fetch
git fetch original
git log original/master
git merge origin/master
git branch -r
git fetch; git merge original/master (or) get pull
git push



Tags are 3types
git tag v1.0
git tag -a v1.0_with_message
git tag -s v1.0_signed

git push –tags
Cloning a remote repository
Fetching and pulling from a remote repository
Pushing changes
Working with tags
Working with local branches
Stashing changes
Merging branches
Rebasing commits
Cherry-picking commits
Working with remote branches
git log --graph –oneline
git log --graph --oneline --all –decorate

git config --global alias.lga "log --graph --oneline --all –decorate"
git lga
cat ~/.gitconfig

Git branch feature1
git checkout feature1
echo "Feature1" >> README.txt
git commit -am "added feature1"
git lga
Note: branches follow commits as you add aditional commits to that branch
Tags: stay on same commit (friendly name for perticular commit)

Git checkout master
git branch fix1 561e0ef
git checkout fix1
echo "Fixing bug #1234" >> README.txt
git commit -am "fix bug 1234"

git branch -m fix1 bug1234 (rename the branch)
git branch -D bug1234 (delete the branch)

Bring back deleted branch (only available 30 days)
git reflog
git branch bug1234 8335b38
git checkout bug1234

Git stash
Git stash pop

GIT MERGE BRANCH
git merge bug1234
git mergetoo
Local and remote branches
Stashing changes
Merging, rebashing, and cherry-picking