Pages

Wednesday, November 26, 2014

Bash multiple cd using alias

We need to use cd .. to come back one directory or have to use cd ../../ to get back twice. So this can be resolved by a new command using the alias.
Create a new shell file and copy the following.

b.sh
=======
#!/bin/sh
_backfunc(){
if [ "$1" -eq 1 ]; then
cd ..
elif [ "$1" -eq 2 ]; then
cd ../..
elif [ "$1" -eq 3 ]; then
cd ../../..
elif [ "$1" -eq 4 ]; then
cd ../../../..
fi
}
alias back='_backfunc'

Now run this script in the current terminal as source b.sh
From now on you can go back upto 4 directory by using following statements:

back 1 # to go back 1 directory
back 2 # to go back 2 directory
back 3 # to go back 3 directory
back 4 # to go back 4 directory

You can add more statements like to go for a particular directory with a number or string by adding more elif statements on the above code. You can also change the "back" to whatever command you want by changing the "back"  in the code.

We can also implement multiple back from current directory by using while loop like shown below :
bloop.sh
==============
#!/bin/sh
_backfunc(){
count=$1
while [ "$count" -ne 0 ]
do
cd ..
count=$(( count - 1 ))
done
}

alias back='_backfunc'

To permanently make this alias you can add "source /home/USER/dir/bloop.sh" in the ~/.bashrc and from then on every new terminal will automatically detect the back statement. You can run the following script to add the file to the ~/.bashrc.

installbloop.sh
===============
 #!/bin/sh
#Put the bloop.sh and this file in the same directory to install at start up.
pw=$(pwd)
pw="source $pw/bloop.sh"
b="$HOME/.bashrc"
echo "$pw">>$b
echo "Added $pw to $b file\n"

This little bloop code can save your time a lot as it is for me!

Monday, November 24, 2014

source command in UNIX

Source command can be used to run the shell code in the current terminal. If we are running a bash script otherwise it will be running in a separate new terminal and after the end of the shell script it will come back to the current shell. So if we are writing the alias and all in the script it will not work on the current shell after the execution of the script since it is in separate context.

Syntax:
=======
source current.sh

current.sh - use back to go back once from the current directory
=========
#!/bin/sh
alias back="cd .."

# you can add the required shorts over this file and run by using the source to make it applicable to the current shell.

The other use of the source is to add separate bash script file which have some functions, alias or some other initialization and we need to use these in a new script. Here just we can use source command. For example :

new.sh - to go back twice from the current directory
=======
#!/bin/sh
source current.sh
back
back

Now call source new.sh to go back twice from the current directory..

Monday, November 17, 2014

Useful Electronics Links



Useful Git statements

Procedure for new repository:
=======================
Create new repository with suitable name on your http://github.com/user profile (Check + sign).
Go to the terminal directory where you have the files to be uploaded to the repository.
For initializing use:
#git init

For adding all files use:
#git add
.
For linking to the git repository use(without " "):
#git remote add origin "repository link"

For committing current files to the repository:
#git commit -a -m "First Commit"

For updating it with the repository:
#git push origin master


Check your files at the repository. So simple!

Create a fork :
===============
go to the repository on website click on fork
# git clone "link"

# git remote -v


To create new branch
===========

# git push origin "new-branch-name"


Show  current branches along with other branches
======================================
# git branch


git checkout to other branches
========================

# git checkout -b "branch-name"



commit to that branch:
======================

# git commit -a -m '"Message"'

Install GOLANG on UNIX

Steps:
=======

  • Get the current archive from the list at - https://golang.org/dl/
  • Untar it . For example 
    • tar xvf  go1.3.3.src.tar.gz
  • A new directory of "go" will be generated.
  • cd to go/src
  • run all.bash
    • ./all.bash
  • Now the go binary will be generated at the go/bin directory.( check whether go and gofmt binaries are generated or not)
  • Open the file ~/.bashrc
  • Add the PATH variable with the bin directory to your .bashrc file at the end like
    • #PATH=$PATH:/home/arun/Documents/GITHUB/golan/go/bin
  • Save the file.
  • Now we can run a go file directly. 
  • Write a hello.go using any editor. For example
    • vim hello.go
    • press i for insert
    • copy the code below from here.
package main
import "fmt"
func main(){
fmt.Printf("Hi How r u\n")
}
    • press the ctrl+shift+v to copy the file to termival vim.
    • press esc and :wq to write and quit.
    • to run the code try 
      • go run hello.go
    • If everything went right you will see the "Hi How r u" statement on terminal.
  • Thats it!