1. touch {apple,banana,grapes}_{01..100}{w..d..2}
2. cp -v == Verbose of what is copying
cp -v 1>../success.txt 2>../error.txt
3 A script contains:
Variable, Arguments, Flow control login
Variables:
user=sherin
b="good morning"
c=2345
d=($pwd)
Builtin variables:
$pwd
$machtype
$hostname
$bash_version
$seconds - Display number of seconds the bash session has run
$0 - Returns the name of script
$1 - Retun the first argument after the script
$? - Return the result of above command
Arithemetic expression:
Exponentiation **
Multiplication *
Division /
Modulation %
Addition +
Substraction -
Comparison Operations = All must be in double squra brasis [[ ]]
<, >, ==, <=, >=, != OR lt, gt, eq, le, ge ne AND &&, ||, !
String null value: -z = null , -n = not null
a=hello
b=world
$c=$a$b
echo ${#a} = It returens the word count
d=${c:3} It displays $c after first three characters (Eg: loworld)
d=${c:3:4} (Eg: lowo)
d=${c: -4} - Displays last 4 characters
echo fruits={apple,banana,banana,cherry}
echo ${fruits/banana/durian} - It replace first Banana with durian
echo ${fuits//banana/durian} - It replaces all banana with durian
echo ${fuits/#apple/durian} - It replaces if apple is first on the line
echo ${fuits/%cherry/durian} - It replaces if cherry is last on the line
COLOR setup
-------------
Forground Background
Black 30 40
Red 31 41
Green 32 42
Yello 33 43
Blue 34 44
Magenta 35 45
cyan 36 46
White 37 47
Eg:
echo -e '\033[34;42mColor Text\033[0m'
echo -e '\033[37;40mWhiteTextonBlack\033[0m'
echo -e '\033[30;47mBlackTextonWhite\033[0m'
Style:
No Style = 0
Bold = 1
Low Intensity = 2
Underline = 4
Blink= 5
Reverse = 7
Invisible == 8
Eg:
echo -e "\033[5;33;44mERROR: \033[0m\033[33;44m Somethoing Went wrong \033[0m"
Declare color:
flashred="\033[5;33;44m"
red="\033[31;40m"
none="\033[0m"
Styled Text:
Forground : tput setaf [0-7]
Background : tput setab [0-7]
No Style : tput sgv0
Bold : tput bold
Low intensity: tput dim
Underline: tput smul
Blinking: tput blink
Reverse: tput rev
color code:
Forground Background
Black 0 0
Red 1 1
Green 2 2
Yello 3 3
Blue 4 4
Magenta 5 5
cyan 6 6
White 7 7
Declare color:
flashred=$(tput setab 0; setaf 1; tput blink)
red=$(tput setab 0; setaf 1)
none=$(tput setsgr0)
----------
printf "Name:\t%s\nID:\t%04d\n" "Scott" "13"
//\t for Tab %s=for string, %04d=must have 4digits, ifnot 00 will be added
eg:
today=$(date +"%d-%m-%y")
time=$(date +"%H)
printf -v -d "Current User:\t%s \n Date:\t\t%s @%s\n" Scott $today $time
echo $d
ARRAY
=======
#!/bin/bash
a=()
b=("apple" "grape" "mange")
echo ${b[2]}
b[5]="kiwi"
b+=("mango")
echo ${b[6]}
Declare:
#!/bin/bash
declare -A myarray
myarray[color]=blue
myarray["Office Building"]=Losangels
echo ${myarray["Office Building"]} is ${myarray[color]}
Result:
Losangels is blue
-----------------------------
vi ftp.txt
open mirror.xmission.com
user anonymouse nothinghere
ascii
cd gutenberg
get gutindex.00
#ftp -n < ftp.txt //Seems this reads the text file and input values from it.
-----------------------------
cat << EndOfText
write
multiple lines
to print on the sccreen
EndOfText
-----------------------------
#!/bin/bash
ftp -n <<- DoneWithTheUpdate
open mirror.xmission.com
user anonymouse nothinghere
ascii
cd gutenberg
get gutindex.01
bye
DoneWithTheUpdate
IF
=====
if [ ]; then
while
=======
#!/bin/bash
i=0
while [ $i -le 10 ]; do
echo i:$1
((i+=1))
done
FOR
=====
for i in {1..100}
do
echo $i
done
for (( 1=1; 1<100; 1++ ))
do
echo $i
done
CASE
=======
#!/bin/bash
echo "Enter the aninal name that you like:"
read a
case $a in
cat) echo " Cat is a small Animal";;
dog|puppy) echo " DOg barks";;
hen) echo " Hen is a bird";;
*) echo "Other Animals";;
esac
FUNCTION
==========
#!/bin/bash
function greet
{
echo "Hi $1, Good Morning!"
}
greet scott
Result: Hi scott, Good Morning!
-----------
#!/bin/bash
function greet
{
echo "Hi $1, Good Morning! $2"
}
greet scott
greet Everyone How are you
-----------
Result:
Hi scott, Good Morning!
Hi Everyone, Good Morning! How
#!/bin/bash
function count
{
i=1
for j in $@; do
echo $i:$j
((i+=1));
done
}
count $(ls)
Result:
1:a2dnscheck.sh
2:a2mig
3:a2migg
4:a2migg.pem
5:a2mig.ppk
Read
======
#!/bin/bash
echo "What is your name:"
read name
echo "What is the password:"
read -s passwd //-s tag for silent
read -p "What is your aninmal:" animal //-p tag toshow a prompt before input area
echo "Name: $name \n Password: $passwd \n Animal: $animal"
SELECT
========
#!/bin/bash
select animal in "Cat" "Hourse" "Tiger" "Hen"
do
echo "You Selected: $animal"
break
done
Result:
[sshamsu@wopr ~]$ sh my.sh
1) Cat
2) Hourse
3) Tiger
4) Hen
#? 4
You Selected: Hen