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

No comments:

Post a Comment