Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Monday, July 6, 2015

Bash - Few shortcuts to make our life easier


Navigate the cursor inside the current command statement
Ctrl + a - Start of the command
Ctrl + e - End of the command
Ctrl + f - Move to the next character
Ctrl + b - Move to the previous character
Alt + right_arrow - End of the current word or the next word
Alt + left_arrow - Start of the current word or the previous word
                      
Delete
Ctrl + k - Delete from current cursor position to the end of the command
Ctrl + u - Delete from current cursor position to the start of the command
Ctrl + w - Delete one word before the cursor
Ctrl + h - Delete one character before the cursor (Backspace)
Ctrl + d - Delete the current character under the cursor
                      
Search History
Ctrl + r - Find the command with the phrase you type.It searches the
history as you type. For each consecutive hit "Ctrl + r", it finds the
next match in the history and moves to the start of the history.
!! - Repeat the last command
!xyz - Find the latest command starting with xyz from the history and run it
!xyz:p - Print the latest command starting with xyz from the history
!$ - Last argument of previous command
!* - All arguments of previous command
^abc^def - Replace abc with def in the previous command and run it
                      
Process
$$ - Gets the pid of the current bash/script
$! - Pid of the last background process
$? - Exit status of the last command
                      

Tuesday, December 17, 2013

[BASH SCRIPTING] Get exit status on the piped process

#!/bin/bash

function logging() {
  while read line; do
    echo "{$(date +'%Y-%m-%d %H:%M:%S')} $line"
  done
}

function stdintoexitstatus() {
  read exitstatus
  return $exitstatus
}

cmds=(
    "cal"
    "cal lkjhsd"
)
for i in $(seq 0 $((${#cmds[@]}-1)))
do
    echo "--------------------------------------------------------"
    echo "[COMMAND] ${cmds[$i]}"
    echo "--------------------------------------------------------"
   ((((eval ${cmds[$i]} 2>&1; echo $? >&3) | logging >&4) 3>&1) | stdintoexitstatus) 4>&1
    echo "Exit Status[$?]"
    echo
done

COMMAND:  eval ${cmds[$i]} 2>&1
The output and the error messages of the eval are redirected to stdout

COMMAND:   echo $? >&3
The status of the eval is stored in the file descriptor 3

COMMAND:   | logging >&4
The (output/error)stdout from eval is piped to logging function where we do logging
and the output of the logging function is stored in file descriptor 4

COMMAND:   3>&1 | stdintoexitstatus
Get the status of eval from file descriptor 3 and update $?

COMMAND:   4>&1
Get the output of the logging function from file descriptor 4 and redirect them to stdout

Reference: http://unix.stackexchange.com/questions/14270/get-exit-status-of-process-thats-piped-to-another

Wednesday, August 14, 2013

Associative Arrays in Bash

#!/bin/bash

# -A declares v to be an associative array
# [key]=vlaue
declare -A v
v=(
    ["var1"]="variable 01"
    ["var2"]="variable 02"
    ["var3"]="variable 03"
    ["var4"]="variable 01"
)

# ${!v[@]} gives us the keys
# to access the value use ${v[key]}
for k in ${!v[@]}
do
    echo "v: [$k] => [${v[$k]}]"
done


# key can have spaces
declare -A vws
vws=(
    ["var 1"]="variable 01"
    ["var 2"]="variable 02"
)

echo "vsw: [var 1] => [${vws['var 1']}]"
echo "vsw: [var 2] => [${vws['var 2']}]"

Output:
v: [var1] => [variable 01]
v: [var3] => [variable 03]
v: [var2] => [variable 02]
v: [var4] => [variable 01]
vsw: [var 1] => [variable 01]
vsw: [var 2] => [variable 02]
Reference: http://www.linuxjournal.com/content/bash-associative-arrays

Saturday, March 26, 2011

Command-line dictionary

It just parse the website wordnetweb and extract the meaning of the word/phrase given. You need lynx to run this script successfully.


References:
http://ubuntuforums.org/showthread.php?t=371482
http://wordnetweb.princeton.edu/perl/webwn

Wednesday, September 15, 2010

awk - One of the best tools in linux

I was searching for a tool/command in linux to remove all the duplicate lines from a file without sort the lines. And i came across this link (see the link below)...Its really amazing. We can do lots and lots of things using awk. Its one of the mostpowerful tools for file/string processing. I hope it will be useful for most of the software developers and testers. One more good thing is that awk is available for windows too.

Link: http://kaneda.bohater.net/faq/awk1line.txt.
The latest version of this file is here.

Processing input arguments in bash shell scripting

 
# Function to print the usage of this script
Usage ()
{
    cat <<EOF
Usage: `basename $0` options [-kld]
          -k <val_k>   Value for the the option -k
          -l <val_l>   Value for the the option -l
          -d           Does not have any value
          -e           Does not have any value
          -h           Print this message
EOF
exit $E_OPTERROR
}
 
# Check for input arguments. If no arguments given, print the usage.
if [ $# -eq 0 ]; then
    Usage
fi
 
while getopts ":k:l:de" Option
do
    case $Option in
        k ) echo "value for the option [k] is \"$OPTARG\""
            ;;
        l ) echo "value for the option [l] is \"$OPTARG\""
            ;;
        d ) echo "option [d] is given."
            ;;
        e ) echo "option [e] is given."
            ;;
        h ) Usage
            ;;
        * ) echo "Invalid input option"
            Usage
            ;;
    esac
done

# To get the arguments other than the option given in the usage
shift $(($OPTIND - 1))
 
for i in $*
do
    echo "Extra inputs given -> \"$i\""
done

exit 0

Tuesday, May 25, 2010

Script to log stepwise execution and output of another bash script into syslog

The input arguments for this script would be another script with its input arguments. First, this script stores the stepwise execution and the output of the called bash script into a temporary file. When the called bash script finishes its execution, this script reads the temporary file and logs them into syslog. We are using a temporary file because if we pipe the output of the called script we will lose the true return value of that script.
#!/bin/bash

tmpfile=$(mktemp)
(/bin/bash -x $@ 2>&1) > $tmpfile
retval=$?
cat $tmpfile | logger -t "logger [$1]"
rm -f $$tmpfile

exit $retval
   
  

Monday, March 8, 2010

How to create self-Extracting script

It involves 3 steps,
  1. Create Payload
  2. Create Installer script
  3. Packaging
Create Payload:
This is a tar ball  in which all the installable binaries, libraries, script files, configuration files, etc will be placed in the same file hierarchy of the target board considering the base directory of the tar ball is the installation directory on the target board.

periask@ubuntu:~$ mkdir -vp packaging/Payload
mkdir: created directory `packaging
mkdir: created directory `packaging/Payload'
periask@ubuntu:~$ cd packaging/Payload
periask@ubuntu:~/packaging/Payload$ cp --preserve=mode -rl <installables> .
periask@ubuntu:~/packaging/Payload$ tar zcf ../Payload.tar.gz .
periask@ubuntu:~/packaging/Payload$ cd ..
periask@ubuntu:~/packaging$

Create Installer Script:
Copy the below content and create "installer" file in "packaging" directory.

#!/bin/bash

# Get the starting point of the tar ball
SKIP=`awk '/^__ARCHIVE__/ { print NR + 1; exit 0; }' $0`

# Do pre-installation steps here.

# Extracting the tar ball Which is appended to this file
tail -n "+$SKIP" $0 >/dev/null 2>&1
if [ $? -eq 0 ]; then
    tail -n +$SKIP $0 | gunzip -c | tar -C / -xvf -
else
    tail +$SKIP $0 | gunzip -c | tar -C / -xvf - 
fi

# Do post-installation steps here.

exit 0

__ARCHIVE__

Note: There should not be any space  after "__ARCHIVE__" and should be a new line after "__ARCHIVE__".

Packaging:
Concatenate the file "installer" and "Payload.tar.gz" and create the package file.

periask@ubuntu:~/packaging$ cat installer Payload.tar.gz > Package-Version.sh
periask@ubuntu:~/packaging$ chmod +x Package-Version.sh

Installer for your package "Package-Version.sh" is ready.

Source: Linux Journal