Sunday, 24 January 2016
Friday, 22 January 2016
Sunday, 17 January 2016
Mount ISO and Install FTP Client & Server
If ftp server not running in your Linux Box.
If you have ISO image. mount to local machine.
[root@localhost ~]# mount -o loop /home/ssankar/rhel-server-6.0-i386-dvd.iso /mnt/
[root@localhost ~]# cd /mnt/
[root@localhost mnt]#
[root@localhost Packages]# pwd
/mnt/Packages
Install FTP Client
----------------
[root@localhost Packages]# rpm -ivh ftp-0.17-51.1.el6.i686.rpm
warning: ftp-0.17-51.1.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:ftp ########################################### [100%]
[root@localhost Packages]# ftp
ftp> quit
[root@localhost Packages]# ftp localhost
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220 (vsFTPd 2.2.2)
If you have ISO image. mount to local machine.
[root@localhost ~]# mount -o loop /home/ssankar/rhel-server-6.0-i386-dvd.iso /mnt/
[root@localhost ~]# cd /mnt/
[root@localhost mnt]#
[root@localhost Packages]# pwd
/mnt/Packages
Install FTP Client
----------------
[root@localhost Packages]# rpm -ivh ftp-0.17-51.1.el6.i686.rpm
warning: ftp-0.17-51.1.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:ftp ########################################### [100%]
[root@localhost Packages]# ftp
ftp> quit
[root@localhost Packages]# ftp localhost
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220 (vsFTPd 2.2.2)
Install FTP Server
----------------
[root@localhost Packages]# rpm -ivh vsftpd-2.2.2-6.el6.i686.rpm
warning: vsftpd-2.2.2-6.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:vsftpd ########################################### [100%]
[root@localhost Packages]#
[root@localhost Packages]# service vsftpd status
vsftpd is stopped
[root@localhost Packages]# service vsftpd start
Starting vsftpd for vsftpd: [ OK ]
AWK Script Basic
[ssankar@localhost AWK]$ cat empdata.txt
1|100|sankar|dell|chennai
2|101|rahul|tcs|chennai
3|102|pal|cts|bangalore
4|103|kali|cts|bangalore
5|104|john|cts|bangalore
6|105|pal2|cts|bangalore
[ssankar@localhost AWK]$ cat sum.awk
BEGIN{
FS="|"
SNO=1 ;
EMPID=2 ;
NAME=3 ;
COMPANY=4 ;
LOCATION=5 ;
}
{
list[$4] += 1;
sum += $1 ;
}
END{
printf ("%d\n", sum) ;
for (comp in list)
{
printf("%s %d \n", comp, list[comp]);
}
}
[ssankar@localhost AWK]$ awk -f sum.awk empdata.txt
21
tcs 1
dell 1
cts 4
[ssankar@localhost AWK]$
1|100|sankar|dell|chennai
2|101|rahul|tcs|chennai
3|102|pal|cts|bangalore
4|103|kali|cts|bangalore
5|104|john|cts|bangalore
6|105|pal2|cts|bangalore
[ssankar@localhost AWK]$ cat sum.awk
BEGIN{
FS="|"
SNO=1 ;
EMPID=2 ;
NAME=3 ;
COMPANY=4 ;
LOCATION=5 ;
}
{
list[$4] += 1;
sum += $1 ;
}
END{
printf ("%d\n", sum) ;
for (comp in list)
{
printf("%s %d \n", comp, list[comp]);
}
}
[ssankar@localhost AWK]$ awk -f sum.awk empdata.txt
21
tcs 1
dell 1
cts 4
[ssankar@localhost AWK]$
Bash shell script trap command example
#/bin/bash
#trace ctrl+c for kill a process
#trap handles seperately before terminating the process
function trap_handler()
{
echo "trap_handler called $$ $@"
rm -f /tmp/input0.$$
exit 1
}
trap trap_handler 2
echo "provide emp name"
read name
#trace ctrl+c for kill a process
#trap handles seperately before terminating the process
function trap_handler()
{
echo "trap_handler called $$ $@"
rm -f /tmp/input0.$$
exit 1
}
trap trap_handler 2
echo "provide emp name"
read name
Bash shell script shift operation
#!/bin/bash
echo "passed arguments: $1 $2 $3.. array $@"
shift
echo "passed arguments: $1 $2 $3.. array $@"
declare -a arr=("sankar" "suriya" "pal" )
echo "length of array arr: ${#arr[@]}"
echo -e "$@"
shift 2
echo -e "$@"
echo "array ${arr[@]}"
echo "passed arguments: $1 $2 $3.. array $@"
shift
echo "passed arguments: $1 $2 $3.. array $@"
declare -a arr=("sankar" "suriya" "pal" )
echo "length of array arr: ${#arr[@]}"
echo -e "$@"
shift 2
echo -e "$@"
echo "array ${arr[@]}"
Bash shell script shopt command sample
#!/bin/bash
## Before enabling xpg_echo
echo "welcome to\n"
echo "shell script training\n"
shopt -s xpg_echo
## After enabling xpg_echo
echo "welcome to\n"
echo "shell script training\n"
# Before disabling aliases
alias l.='ls -l .'
l.
# After disabling aliases
shopt -u expand_aliases
l.
## Before enabling xpg_echo
echo "welcome to\n"
echo "shell script training\n"
shopt -s xpg_echo
## After enabling xpg_echo
echo "welcome to\n"
echo "shell script training\n"
# Before disabling aliases
alias l.='ls -l .'
l.
# After disabling aliases
shopt -u expand_aliases
l.
Bash shell script let command example
#!/bin/bash
#let command alter for expr command
let a=1
let b=2
let add=$a+$b
let sub=$a-$b
let mul=$a*$b
let div=$a/$b
echo $add $sub $mul $div
#let command alter for expr command
let a=1
let b=2
let add=$a+$b
let sub=$a-$b
let mul=$a*$b
let div=$a/$b
echo $add $sub $mul $div
Bash shell script Set Unset Command Examples
#!/bin/bash
var="Learning Bash Shell Scripting"
set -- $var
echo "\$1=" $1
echo "\$2=" $2
echo "\$3=" $3
echo "\$4=" $4
echo "Var $var"
unset var
echo "After unset $var"
var="Learning Bash Shell Scripting"
set -- $var
echo "\$1=" $1
echo "\$2=" $2
echo "\$3=" $3
echo "\$4=" $4
echo "Var $var"
unset var
echo "After unset $var"
Bash shell script getops example
#! /bin/bash
#process the command line argument using getopts
#take action based on input
while getopts :h:r:l: OPTION
do
case $OPTION in
h)
echo "help of $OPTARG"
help "$OPTARG"
;;
r)
echo "Going to remove a file $OPTARG"
rm -f "$OPTARG"
;;
*)
echo "Unknown option"
echo "Usage: sh getopt.sh [-hrl] [value]"
;;
esac
done
#process the command line argument using getopts
#take action based on input
while getopts :h:r:l: OPTION
do
case $OPTION in
h)
echo "help of $OPTARG"
help "$OPTARG"
;;
r)
echo "Going to remove a file $OPTARG"
rm -f "$OPTARG"
;;
*)
echo "Unknown option"
echo "Usage: sh getopt.sh [-hrl] [value]"
;;
esac
done
Bash shell script eval command
#!/bin/bash
#eval command - evaluate the expression and execute it
#provide input as username
if [ ! -z $1 ]
then
proccomm="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | grep $1"
else
proccomm="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu"
fi
eval $proccomm
#eval command - evaluate the expression and execute it
#provide input as username
if [ ! -z $1 ]
then
proccomm="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | grep $1"
else
proccomm="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu"
fi
eval $proccomm
Bash shell script expressions
#!/bin/bash
#command line arg processing
#find last command line value [
echo "-1 value ${@: -1}"
echo "0th value ${BASH_ARGV[0]}"
echo "last index value ${@: $#}"
echo "final process value ${!#}"
for value in $@; do :; done
echo "after loop $value"
#]
#wildcards
#read the input, search that file from the directory
echo "enter the user name"
read user
echo "user $user"
#list files for user
for file in `ls /home/$user`
do
echo "$file"
done
#list file extension .sh
for file in `ls /home/$user/Training/Shell*/*.sh`
do
echo "$file"
done
#list file that has 3 char file name size
for file in `ls /home/$user/Training/Shell*/fo?`
do
echo "$file"
done
#list files those start with [lfh]
for file in `ls /home/$user/Training/Shell*/[lfh]*`
do
echo "$file"
done
#list files that matches the list
echo -e "\n a-z \r\n"
for file in `ls /home/$user/Training/Shell*/[a-z]*`
do
echo "$file"
done
#arguments
echo "arguments $*"
#command line arg processing
#find last command line value [
echo "-1 value ${@: -1}"
echo "0th value ${BASH_ARGV[0]}"
echo "last index value ${@: $#}"
echo "final process value ${!#}"
for value in $@; do :; done
echo "after loop $value"
#]
#wildcards
#read the input, search that file from the directory
echo "enter the user name"
read user
echo "user $user"
#list files for user
for file in `ls /home/$user`
do
echo "$file"
done
#list file extension .sh
for file in `ls /home/$user/Training/Shell*/*.sh`
do
echo "$file"
done
#list file that has 3 char file name size
for file in `ls /home/$user/Training/Shell*/fo?`
do
echo "$file"
done
#list files those start with [lfh]
for file in `ls /home/$user/Training/Shell*/[lfh]*`
do
echo "$file"
done
#list files that matches the list
echo -e "\n a-z \r\n"
for file in `ls /home/$user/Training/Shell*/[a-z]*`
do
echo "$file"
done
#arguments
echo "arguments $*"
Bash shell script functions
#!/bin/bash
#definition of function
#passing arg to function
#command line args
#exit status of function
#define your function here
Hello() {
echo "Hello World"
}
#Invoke the function
Hello
#Passing the values
Hello() {
echo "Hello World $1 $2"
}
#Invoke the function
Hello Sankar S
#return values
Hello () {
echo "Hello $1 $2"
echo $@
return 0
}
Hello Sankar Suriya
echo "Exit status of Hello Function : $?"
#command line arguments
numofarg=$#
echo "Total Number Of Arguments Are : $numofarg"
if [ $numofarg -gt 0 ]
then
for i in $@
do
echo "print $i"
done
fi
#definition of function
#passing arg to function
#command line args
#exit status of function
#define your function here
Hello() {
echo "Hello World"
}
#Invoke the function
Hello
#Passing the values
Hello() {
echo "Hello World $1 $2"
}
#Invoke the function
Hello Sankar S
#return values
Hello () {
echo "Hello $1 $2"
echo $@
return 0
}
Hello Sankar Suriya
echo "Exit status of Hello Function : $?"
#command line arguments
numofarg=$#
echo "Total Number Of Arguments Are : $numofarg"
if [ $numofarg -gt 0 ]
then
for i in $@
do
echo "print $i"
done
fi
Bash shell script case statement
#!/bin/bash
n=11
case "$n" in
1) echo "value 1 is received"
;;
2) echo "value 2 is received"
;;
3) echo "value is 3 received"
;;
10) echo "value is 10 received"
;;
*) echo "number $n is not processed"
;;
esac
n="abc"
case "$n" in
"abc") echo "value abc is received"
;;
2) echo "value 2 is received"
;;
3) echo "value is 3 received"
;;
10) echo "value is 10 received"
;;
*) echo "number $n is not processed"
;;
esac
for filename in $(ls)
do
# Take extension available in a filename
ext=${filename#*\.}
case "$ext" in
c) echo "$filename : C source file"
;;
o) echo "$filename : Object file"
;;
sh) echo "$filename : Shell script"
;;
txt) echo "$filename : Text file"
;;
*) echo " $filename : Not processed"
;;
esac
done
n=11
case "$n" in
1) echo "value 1 is received"
;;
2) echo "value 2 is received"
;;
3) echo "value is 3 received"
;;
10) echo "value is 10 received"
;;
*) echo "number $n is not processed"
;;
esac
n="abc"
case "$n" in
"abc") echo "value abc is received"
;;
2) echo "value 2 is received"
;;
3) echo "value is 3 received"
;;
10) echo "value is 10 received"
;;
*) echo "number $n is not processed"
;;
esac
for filename in $(ls)
do
# Take extension available in a filename
ext=${filename#*\.}
case "$ext" in
c) echo "$filename : C source file"
;;
o) echo "$filename : Object file"
;;
sh) echo "$filename : Shell script"
;;
txt) echo "$filename : Text file"
;;
*) echo " $filename : Not processed"
;;
esac
done
Bash shell script looping statements
#!/bin/bash
#for loop , while loop
declare -a arr=( 1 2 3 4 5 )
echo ${arr[@]}
#< len of array
for (( i=0; i < ${#arr[@]} ; i++ ))
do
echo "$i -> ${arr[$i]}"
done
#while loop
n=${#arr[@]}
i=0
while [ $i -lt $n ]
do
echo "$i -- > ${arr[$i]}"
let i++
done
#until
i=0
until [ ! $i -lt $n ]
do
echo "$i -- > ${arr[$i]}"
i=`expr $i + 1`
done
#for loop , while loop
declare -a arr=( 1 2 3 4 5 )
echo ${arr[@]}
#< len of array
for (( i=0; i < ${#arr[@]} ; i++ ))
do
echo "$i -> ${arr[$i]}"
done
#while loop
n=${#arr[@]}
i=0
while [ $i -lt $n ]
do
echo "$i -- > ${arr[$i]}"
let i++
done
#until
i=0
until [ ! $i -lt $n ]
do
echo "$i -- > ${arr[$i]}"
i=`expr $i + 1`
done
Bash shell script assignment operations
#!/bin/bash
#assignments
x=10
y=$x
echo "$x $y"
a=1.2
b=$a
echo "$a $b"
p="abc"
q=$p
echo "$p $q"
readonly n=100
s=$n
echo "$n $s"
#array to scalar
a=( 1 2 3 4 5 )
echo "${a[@]}"
m=${a[0]}
echo "a[0] $m"
arr=( "aa" "bb" "cc")
echo "${arr[@]}"
m=${arr[0]}
echo "arr[0] $m"
p=( "${arr[@]}" )
echo "p -> ${p[@]} "
v=100
a[2]=$v
echo "${a[@]}"
u="dd"
arr[1]=$u
echo "${arr[@]}"
#assignments
x=10
y=$x
echo "$x $y"
a=1.2
b=$a
echo "$a $b"
p="abc"
q=$p
echo "$p $q"
readonly n=100
s=$n
echo "$n $s"
#array to scalar
a=( 1 2 3 4 5 )
echo "${a[@]}"
m=${a[0]}
echo "a[0] $m"
arr=( "aa" "bb" "cc")
echo "${arr[@]}"
m=${arr[0]}
echo "arr[0] $m"
p=( "${arr[@]}" )
echo "p -> ${p[@]} "
v=100
a[2]=$v
echo "${a[@]}"
u="dd"
arr[1]=$u
echo "${arr[@]}"
Bash shell script if-else statements
#!/bin/bash
#if-else conditional statements
n=100
if [ $n -eq 100 ]
then
echo "n is 100"
fi
n=99
if [ $n -eq 100 ]
then
echo "n is 100"
else
echo "n is not 100"
fi
n=99
if [ $n -eq 100 ]
then
echo "n is 100"
elif [ $n -gt 100 ]
then
echo "n is greater than 100"
else
echo "n is less than 100"
fi
n=99
if [ $n -eq 100 ]
then
echo "n is 100"
else
if [ $n -gt 100 ]
then
echo "n is greater than 100"
else
echo "n is less than 100"
fi
fi
#if-else conditional statements
n=100
if [ $n -eq 100 ]
then
echo "n is 100"
fi
n=99
if [ $n -eq 100 ]
then
echo "n is 100"
else
echo "n is not 100"
fi
n=99
if [ $n -eq 100 ]
then
echo "n is 100"
elif [ $n -gt 100 ]
then
echo "n is greater than 100"
else
echo "n is less than 100"
fi
n=99
if [ $n -eq 100 ]
then
echo "n is 100"
else
if [ $n -gt 100 ]
then
echo "n is greater than 100"
else
echo "n is less than 100"
fi
fi
Bash shell scripting Array Operations
#!/bin/bash
#array declarations and its operations
arr[0]='apple'
arr[1]='orange'
arr[2]='lemon'
arr[3]='kiwi'
a[0]=1
a[1]=2
a[2]=3
a[3]=4
echo "${arr[@]}"
echo "${arr[0]}"
echo "${arr[1]}"
echo "${arr[2]}"
echo "${arr[3]}"
echo "Total length of array : ${#arr[@]}"
echo "size of arr[0] : ${#arr[0]}"
#using declare
declare -a city=('Chennai' 'Trichy' 'Madurai' 'Kovai')
city=("${city[@]}" 'Villupuram' 'Vellore')
echo "${city[@]}"
#offset
echo "${city[0]:0:3}"
echo "${city[@]}"
#search and replace
echo "${city[@]/Chennai/Madras}"
#remove
unset city[0]
echo "${city[@]}"
#remove with pattern
declare -a newcity=( ${city[@]/*ai/} )
echo "${newcity[@]}"
#copy entire array to newone
mycitylist=("${newcity[@]}")
echo -e "my final city list: \n ${mycitylist[@]} "
#join arrays
total=("${newcity[@]}" "${arr[@]}" "${a[@]}" )
echo "${total[@]}"
#delete entire array
unset total
echo "${total[@]}"
#array declarations and its operations
arr[0]='apple'
arr[1]='orange'
arr[2]='lemon'
arr[3]='kiwi'
a[0]=1
a[1]=2
a[2]=3
a[3]=4
echo "${arr[@]}"
echo "${arr[0]}"
echo "${arr[1]}"
echo "${arr[2]}"
echo "${arr[3]}"
echo "Total length of array : ${#arr[@]}"
echo "size of arr[0] : ${#arr[0]}"
#using declare
declare -a city=('Chennai' 'Trichy' 'Madurai' 'Kovai')
city=("${city[@]}" 'Villupuram' 'Vellore')
echo "${city[@]}"
#offset
echo "${city[0]:0:3}"
echo "${city[@]}"
#search and replace
echo "${city[@]/Chennai/Madras}"
#remove
unset city[0]
echo "${city[@]}"
#remove with pattern
declare -a newcity=( ${city[@]/*ai/} )
echo "${newcity[@]}"
#copy entire array to newone
mycitylist=("${newcity[@]}")
echo -e "my final city list: \n ${mycitylist[@]} "
#join arrays
total=("${newcity[@]}" "${arr[@]}" "${a[@]}" )
echo "${total[@]}"
#delete entire array
unset total
echo "${total[@]}"
Bash shell scripting different variables
#!/bin/bash
#understanding different system and user varibles
echo "whoami $USER"
#global variables
# use ` for getting output of shell commands within echo output
#echo -e "Global Current Shell Values Are:\n `printenv`"
#user defined variables
i=10
j=20.5
k=30.254
echo "$i $j $k"
#strings
name="jataayu"
name1='jataayu'
echo $name $name1
echo '$name'
#constant
readonly n=100
echo $n
#understanding different system and user varibles
echo "whoami $USER"
#global variables
# use ` for getting output of shell commands within echo output
#echo -e "Global Current Shell Values Are:\n `printenv`"
#user defined variables
i=10
j=20.5
k=30.254
echo "$i $j $k"
#strings
name="jataayu"
name1='jataayu'
echo $name $name1
echo '$name'
#constant
readonly n=100
echo $n
Shell Scripting Echo Examples
#!/bin/bash
echo "Hello World..!!"
printf "My First Shell Program\nAm after new line\n"
#-e enable interpretation of backslash escapes.
echo -e "Have Fun\nI love to Code\n"
#-n supresses the new line which is implicit when use echo command
echo -e -n "I dont introduce extra new lines\n"
echo "Hello World..!!"
printf "My First Shell Program\nAm after new line\n"
#-e enable interpretation of backslash escapes.
echo -e "Have Fun\nI love to Code\n"
#-n supresses the new line which is implicit when use echo command
echo -e -n "I dont introduce extra new lines\n"
Shell Scripting Arithmetic Operations
#!/bin/bash
#arithmetic operations
a=10
b=20
echo "a = $a b = $b"
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
readonly n=100
total=`expr $val + $n`
echo $total
#n=$total
#echo $n
#floating point arithmetic
x=10.1
y=20.2
echo "$x + $y" | bc
echo "$x - $y" | bc
echo "$x * $y" | bc
echo "$x / $y" | bc
#arithmetic operations
a=10
b=20
echo "a = $a b = $b"
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
readonly n=100
total=`expr $val + $n`
echo $total
#n=$total
#echo $n
#floating point arithmetic
x=10.1
y=20.2
echo "$x + $y" | bc
echo "$x - $y" | bc
echo "$x * $y" | bc
echo "$x / $y" | bc
Subscribe to:
Posts (Atom)