How can I use better method for xferlog - bash

I have a bash script for get date from xferlog like this(this is sample code):
#!/bin/bash
xferlog="/var/log/xferlog"
xfer1="test"
xfer2="test2"
xfer1last=$(tac "$xferlog" | awk -v pattern="$xfer1" '$9 ~ pattern {print; exit}')
xfer2last=$(tac "$xferlog" | awk -v pattern="$xfer2" '$9 ~ pattern {print; exit}')
day1=${xfer1last:8:2}
month1=${xfer1last:4:3}
year1=${xfer1last:20:4}
hour1=${xfer1last:11:2}
minute1=${xfer1last:14:2}
if [ "$month1" == "Jan" ];then
monthlast1=01
fi
if [ "$month1" == "Feb" ];then
monthlast1=02
fi
if [ "$month1" == "Mar" ];then
monthlast1=03
fi
if [ "$month1" == "Apr" ];then
monthlast1=04
fi
if [ "$month1" == "May" ];then
monthlast1=05
fi
if [ "$month1" == "Jun" ];then
monthlast1=06
fi
if [ "$month1" == "Jul" ];then
monthlast1=07
fi
if [ "$month1" == "Aug" ];then
monthlast1=08
fi
if [ "$month1" == "Sep" ];then
monthlast1=09
fi
if [ "$month1" == "Oct" ];then
monthlast1=10
fi
if [ "$month1" == "Nov" ];then
monthlast1=11
fi
if [ "$month1" == "Dec" ];then
monthlast1=12
fi
daylast1=$day1
if [ "$day1" == " 1" ];then
daylast1=01
fi
if [ "$day1" == " 2" ];then
daylast1=02
fi
if [ "$day1" == " 3" ];then
daylast1=03
fi
if [ "$day1" == " 4" ];then
daylast1=04
fi
if [ "$day1" == " 5" ];then
daylast1=05
fi
if [ "$day1" == " 6" ];then
daylast1=06
fi
if [ "$day1" == " 7" ];then
daylast1=07
fi
if [ "$day1" == " 8" ];then
daylast1=08
fi
if [ "$day1" == " 9" ];then
daylast1=09
fi
echo "$daylast1.$monthlast1.$year1 $hour1 $minute1"
Xferlog sample like this:
Thu Sep 2 09:52:00 2004 50 192.168.20.10 896242 /home/test/file1.tgz b _ o r suporte ftp 0 * c
My script working and show me what I want, like this:
02.09.2004 09:52
So I need to change month format( for ex. Jan to 01 ) and write 0 before day if day value is single character.
Any better suggestion for this? Maybe I can do that with smaller code.
Thank you.

If you have access to GNU date:
$ date -d"Thu Sep 2 09:52:00 2004" +"%d.%m.%Y %H:%M"
02.09.2004 09:52

Related

String to string replacement in file while taking those string from a file

I have following requirement to be written in bash, which is as follows
I have file consist of filename,stra,strb,colname
filename: Name of the file
stra: string to be find out
strb: string to be replaced with
colname: is name of the column in file
stringlist.csv:
filename,stra,strb,colname
x.txt,aaa,xxx,col1
y.txt,bbb,yyy,col1
x.txt:
col1,col2,col3
aaa,10,20
bbb,20,30
aaa,21,34
ccc,43,98
y.txt:
aaa,10,20
bbb,20,30
aaa,21,34
ccc,43,98
From the stringlist.csv as input file, In x.txt column col1 aaa should be replaced with xxx and respectively in y.txt col1 containing bbb should be replaced with yyy and other content in the file should remain same
I did it by myself. I was expecting some hint but not the code
echo -n "Enter 1 for directory
Enter 2 for list of files
Enter 3 for one file as input: "
read mode
if [ $mode -eq 1 ]; then
echo -n "Enter direcotry path: "
read dir
DIRS=`ls -l $dir | awk '{print $9}'`
echo -n "Do you want to override the file or want to create a new file(y/n)? "
read choice
if [ $choice == "n" ]; then
echo -n "Enter the prefix of the output file. Output file will be prefix followed by filename: "
read prefix
elif [ $choice == "y" ]; then
:
else
echo $'You have chosen invalid option.... Bye!!\n'
exit
fi
elif [ $mode -eq 2 ]; then
echo -n "Enter List of files separated by space: "
read DIRS
echo -n "Do you want to override the file or want to create a new file(y/n)? "
read choice
if [ $choice == "n" ]; then
echo -n "Enter the prefix of the output file. Output file will be prefix followed by filename: "
read prefix
elif [ $choice == "y" ]; then
:
else
echo $'You have chosen invalid option.... Bye!!\n'
exit
fi
elif [ $mode -eq 3 ]; then
echo -n "Enter file name: "
read DIRS
echo -n "Do you want to override the file or want to create a new file(y/n)? "
read choice
if [ $choice == "n" ]; then
echo -n "Enter the prefix of the output file. Output file will be prefix followed by filename: "
read prefix
elif [ $choice == "y" ]; then
:
else
echo $'You have chosen invalid option.... Bye!!\n'
exit
fi
else
echo $'You have chosen invalid option.... Bye!!\n'
exit
fi
echo -n "Enter file delimiter: "
read del
echo -n "enter string replacement file: "
read strrepfile
echo -n "Enter Delimiter for string replacement input file: "
read indel
if [ $indel == "tab" ]; then
indel='\t'
sed "s/$indel/|/g" $strrepfile > $strrepfile.1
else
sed "s/$indel/|/g" $strrepfile > $strrepfile.1
fi
#filenames=`awk 'BEGIN{FS=OFS='$indel1'}{print $1}' $strrepfile`
if [ $del == "tab" ]; then
del1='"'"\t"'"'
else
del1='"'$del'"'
fi
for var in $DIRS; do
if [ $mode -eq 1 ]; then
colheadfile=$dir/$var
else
colheadfile=$var
fi
break
done
awvar="-v col"
while read -r line
do
echo "$line" > temp
stra=`awk -F '|' '{print $1}' temp`
strb=`awk -F '|' '{print $2}' temp`
col=`awk -F '|' '{print $3}' temp`
if [ $del == "tab" ]; then
del='\t'
fi
filedel="'"$del"'"
colpos=`head -1 $colheadfile | tr -s $filedel '\n' | nl -nln | grep $col | cut -f1`
colpos=$(echo $colpos | tr -d ' ')
awkvar1=$awkvar1$awvar$colpos'='$colpos' '
gsubvar=$gsubvar'gsub("'$stra'","'$strb'",$'$colpos');'
done < "$strrepfile.1"
echo $awkvar1
echo $gsubvar
for dir1 in $DIRS; do
echo $dir1
if [ $mode -eq 1 ] && [ $choice == "y" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir/$dir1 > $dir1
elif [ $mode -eq 1 ] && [ $choice == "n" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir/$dir1 > $prefix'_'$dir1
elif [ $mode -eq 2 ] && [ $choice == "y" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $dir1
elif [ $mode -eq 2 ] && [ $choice == "n" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $prefix'_'$dir1
elif [ $mode -eq 3 ] && [ $choice == "y" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $dir1
elif [ $mode -eq 3 ] && [ $choice == "n" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $prefix'_'$dir1
fi
done
rm $strrepfile.1
rm temp
Thanks

How do I find the middle of three numbers in shell script

#!/bin/bash
echo "Enter three numbers and this program will give you the middle number : " ; read num1 ; read num2 ; read num3
if [ "$num1" -gt "$num2" ] && [ "$num1" -lt "$num3" ] || [ "$num1" -lt "$num2" ] && [ "$num1" -gt "$num3" ]; then
{
echo "The middle number is $num1"
}
elif [ "$num2" -gt "$num1" ] && [ "$num2" -lt "$num3" ] || [ "$num2" -lt "$num1" ] && [ "$num2" -gt "$num3" ]; then
{
echo "The middle number is $num2"
}
elif [ "$num3" -gt "$num1" ] && [ "$num3" -lt "$num2" ] || [ "$num3 -lt "$num1" ] && [ "$num3" -gt "$num2" ]; then
{ echo "The middle number is $num3" }
fi
The problem I have is with the or condition. I input the numbers 1, 2, and 3, but I get the middle number as 1 all the time.
How about this:
getmid() {
if (( $1 <= $2 )); then
(( $1 >= $3 )) && { echo $1; return; }
(( $2 <= $3 )) && { echo $2; return; }
fi;
if (( $1 >= $2 )); then
(( $1 <= $3 )) && { echo $1; return; }
(( $2 >= $3 )) && { echo $2; return; }
fi;
echo $3;
}
# All permutations of 1, 2 and 3 print 2.
getmid 1 2 3
getmid 2 1 3
getmid 1 3 2
getmid 3 1 2
getmid 2 3 1
getmid 3 2 1
This one should work:
#!/bin/bash
echo "Enter three numbers and this program will give you the middle number : " ; read num1 ; read num2 ; read num3;
if [ "$num1" -gt "$num2" ] && [ "$num1" -lt "$num3" ]; then
{
echo "The middle number is" $num1 ;
}
elif [ "$num1" -lt "$num2" ] && [ "$num1" -gt "$num3" ]; then
{
echo "The middle number is" $num1 ;
}
elif [ "$num2" -gt "$num1" ] && [ "$num2" -lt "$num3" ]; then
{
echo "The middle number is" $num2 ;
}
elif [ "$num2" -lt "$num1" ] && [ "$num2" -gt "$num3" ]; then
{
echo "The middle number is" $num2 ;
}
elif [ "$num3" -gt "$num1" ] && [ "$num3" -lt "$num2" ]; then
{
echo "The middle number is" $num3 ;
}
elif [ "$num3" -lt "$num1" ] && [ "$num3" -gt "$num2" ]; then
{
echo "The middle number is" $num3 ;
}
fi

How to use IF loop in unix for multiple conditions

I need a logic to implement the following logic in unix
if ( $a !="xyz" || $d !="abc" ) && ( $b= $c))
then
echo "YES WORKING"
fi
I tried below code not working
if [ [ [ $a != "xyz" ] -o [ $d != "abc" ] ] -a [ "$b" = "$c" ] ]
then
echo "YES WORKING"
fi
getting error as
:[ :] unexpected operator/operand
You can do something like this:
[ $a != "xyz" -o $d != "abc" ] && [ "$b" = "$c" ] && echo "YES WORKING"
Your logic should work easy in shells supporting [[ ]]:
if [[ ($a != "xyz" || $d != "abc") && $b = "$c" ]]; then
echo "YES WORKING"
fi
Although there's a way for those that doesn't:
if ([ ! "$a" = "xyz" ] || [ ! "$d" = "abc" ]) && [ "$b" = "$c" ]; then
echo "YES WORKING"
fi
But that's still inefficient since you'd be summoning subshells, so use { } but the syntax is a little ugly:
if { [ ! "$a" = "xyz" ] || [ ! "$d" = "abc" ]; } && [ "$b" = "$c" ]; then
echo "YES WORKING"
fi

BASH function: syntax error near unexpected token `'2''

I am completely lost as to why this error keeps happening, I am fairly certain that I am using the correct syntax, however, every time I execute this BASH script, I get the following error:
/home/clucky/MCServerBackup/MCServerBackup.sh: line 200: syntax error near unexpected token `'2''
/home/clucky/MCServerBackup/MCServerBackup.sh: line 200: ` logInfo() '2' "none" "Minecraft: $minecraftsize.$minecraftsized $minecraftunit"'
The line in particular that it appears to be refrencing is line 200, which is where the code attempts to execute the function logInfo() as follows:
logInfo() '2' "none" "Minecraft: $minecraftsize.$minecraftsized $minecraftunit"
In the above line of code, 2 is the indent level (2 x 5 spaces). none tells the function to ignore the logLevel that is specified in the configuration and log it anyway. Minecraft: $minecraftsize...$minecraftunit is what is being added into the log file, which the words preceeded by a $ are of course variables. The entire code can be viewed below.
#! /usr/bin/env bash
#Declare Functions
logInfo() {
if [ "$logInFile" == "true" ] || [ "$logInConsole" == "true" ] ; then
let tabAmount=5*$1
indent=""
for (( indentrepeat=0; indentrepeat < $tabAmount; indentrepeat++ ))
do
indent="$indent "
done
if [ "$2" == "none" ] ; then
infoItem="$3"
else
if [ "$logLevel" == "high" ] ; then
if [ "$2" == "subtask" ] ; then
#High + Subtask
infoItem="`date '+%H:%M:%S'` $3"
elif [ "$2" == "task" ] ; then
#High + Task
infoItem="`date '+%H:%M:%S'` $3"
fi
elif [ "$logLevel" == "medium" ] ; then
if [ "$2" == "subtask" ] ; then
#Medium + Subtask
infoItem="$3"
elif [ "$2" == "task" ] ; then
#Medium + Task
infoItem="`date '+%H:%M:%S'` $3"
fi
else
if [ "$2" == "subtask" ] ; then
#Low + Subtask
infoItem=''
elif [ "$2" == "task" ] ; then
#Low + Task
infoItem="$3"
fi
fi
fi
if [ "$logInFile" == "true" ] ; then
echo -e "$indent$infoItem" >> backup.log
fi
if [ "$logInConsole" == "true" ] ; then
echo -e "$indent$infoItem"
fi
fi
}
logError() {
if [ "$logInFile" == "true" ] || [ "$logInConsole" == "true" ] ; then
let tabAmount=5*$1
indent=""
for (( indentrepeat=0; indentrepeat < $tabAmount; indentrepeat++ ))
do
indent="$indent "
done
if [ "$logInFile" == "true" ] && [ "$logErrors" == "true" ] ; then
echo -e "$indent[ERROR] $2" >> backup.log
fi
if [ "$logInConsole" == "true" ] && [ "$logErrors" == "true" ] ; then
echo -e "$indent[ERROR] $2"
fi
fi
}
#Set Variables
source config.sh
minecraftsize=0
sqlsize=0
websitesize=0
timedate=`date '+%m.%d.%Y-%H:%M'`
#Update
if [ $version != "1.0.0" ] ; then
source update.sh
fi
#Trigger General Settings
echo -ne "\033]0;$serverName - Backup\007"
if [ "$serverDirectory" == "default" ] ; then
BINDIR="$(dirname "$(readlink -fn "$0")")"
cd "$BINDIR"
else
if [ -d "$serverDirectory" ] ; then
cd "$serverDirectory"
else
directoryExists=false
echo '$serverDirectory defined as '"$serverDirectory"' does not exist. Please double check your configuration and make sure this folder exists.'
if [ "$logInFile" == "true" ] ; then
echo "-------------- `date '+%d-%B-%Y %H:%M'` --------------" >> backup.log
echo -e ' [ERROR] $serverDirectory defined as '"$serverDirectory"', does not exist. Please double check your configuration and make sure this folder exists.'"\n" >> backup.log
fi
fi
fi
if [ "$purgeFiles" == "true" ] || [ "$backupMinecraft" == "true" ] || [ "$backupMySQL" == "true" ] || [ "$backupWebsite" == "true" ] && [ "$directoryExists" != "false" ] ; then
echo "-------------- `date '+%d-%B-%Y %H:%M'` --------------" >> backup.log
#Create Directories
echo "[`date '+%H:%M:%S'`] Directory Creation Started" >> backup.log
if [ ! -d ".backups" ] ; then
mkdir -p .backups
fi
mkdir -p .backups/$timedate
if [ "$backupMySQL" == "true" ] ; then
mkdir -p .backups/$timedate/MySQL
fi
if [ "$backupWebsite" == "true" ] ; then
mkdir -p .backups/$timedate/Website
fi
if [ "$backupMinecraft" == "true" ] ; then
mkdir -p .backups/$timedate/MinecraftServers
fi
echo "[`date '+%H:%M:%S'`] Directory Creation Completed" >> backup.log
#Backup Minecraft Servers
if [ "$backupMinecraft" == "true" ] ; then
echo "[`date '+%H:%M:%S'`] Minecraft Backup Started" >> backup.log
for (( servercount = 0; servercount < ${#mcservers[#]}; servercount++ ))
do
echo " ${mcservers[servercount]}" >> backup.log
tar -zcpf .backups/$timedate/MinecraftServers/${mcservers[servercount]}.tar.gz --directory ${mcservers[servercount]} ${fileexcludes[servercount]} .
minecraftsize=`expr $minecraftsize + $(ls -la .backups/$timedate/MinecraftServers/${mcservers[servercount]}.tar.gz | awk '{ print $5}')`
done
echo "[`date '+%H:%M:%S'`] Minecraft Backup Completed" >> backup.log
fi
#Copy MySQL Databases
if [ "$backupMySQL" == "true" ] ; then
echo "[`date '+%H:%M:%S'`] MySQL Backup Started" >> backup.log
for (( sqlcount = 0; sqlcount < ${#sqldatabases[#]}; sqlcount++ ))
do
if [[ "${sqldatabases[sqlcount]}" =~ # ]] ; then
sqldatabases[sqlcount]=${sqldatabases[sqlcount]//[#]/}
mysqldump -u"$sqluser" -p"$sqlpass" -h"$sqlhost" ${sqldatabases[sqlcount]} > .backups/$timedate/MySQL/${sqldatabases[sqlcount]}.sql;
sqlsize=`expr $sqlsize + $(ls -la .backups/$timedate/MySQL/${sqldatabases[sqlcount]}.sql | awk '{ print $5}')`
echo " ${sqldatabases[sqlcount]}*" >> backup.log
else
mysqldump -u"$sqluser" -p"$sqlpass" -h"$sqlhost" --skip-lock-tables ${sqldatabases[sqlcount]} > .backups/$timedate/MySQL/${sqldatabases[sqlcount]}.sql;
sqlsize=`expr $sqlsize + $(ls -la .backups/$timedate/MySQL/${sqldatabases[sqlcount]}.sql | awk '{ print $5}')`
echo " ${sqldatabases[sqlcount]}" >> backup.log;
fi
done
fi
#Backup Website
if [ "$backupWebsite" == "true" ] ; then
echo "[`date '+%H:%M:%S'`] Website Backup Started" >> backup.log
if [ "$websiteDirectory" == "default" ] ; then
websiteDirectory=var/www/
fi
if [ -d "$websiteDirectory" ] ; then
tar -zcpf .backups/$timedate/Website/website.tar.gz --directory $websiteDirectory $websiteExcludes .
echo "[`date '+%H:%M:%S'`] Website Backup Completed" >> backup.log
else
logError "5" "\0044websiteDirectory defined as $websiteDirectory does not exist. Please double check your configuration and make sure this folder exists."
echo "[`date '+%H:%M:%S'`] Website Backup Cancelled" >> backup.log
fi
fi
#Purge files 3 days old
if [ "$purgeFiles" == "true" ] ; then
echo "[`date '+%H:%M:%S'`] Purging Old Backups" >> backup.log
find .backups* -mmin +$purgeTime -exec rm --recursive {} \;
echo "[`date '+%H:%M:%S'`] Purging Complete" >> backup.log
fi
#Read back file size
let filesize=$minecraftsize+$sqlsize+$websitesize
if [ "$filesize" -ge 1024 ] ; then
if [ "$filesize" -ge 1048576 ] ; then
if [ "$filesize" -ge 1073741824 ] ; then
filedivider=1073741824
fileunit="GB"
else
filedivider=1048576
fileunit="MB"
fi
else
filedivider=1024
fileunit="KB"
fi
else
filedivider=1
fileunit="Bytes"
fi
let filesized='(''('$filesize%$filedivider')'*100')'/$filedivider
let filesize=$filesize/$filedivider
echo " Total Compression Size: $filesize.$filesized $fileunit" >> backup.log
if [ "$backupMinecraft" == "true" ] ; then
if [ "$minecraftsize" -ge 1024 ] ; then
if [ "$minecraftsize" -ge 1048576 ] ; then
if [ "$minecraftsize" -ge 1073741824 ] ; then
minecraftdivider=1073741824
minecraftunit="GB"
else
minecraftdivider=1048576
minecraftunit="MB"
fi
else
minecraftdivider=1024
minecraftunit="KB"
fi
else
minecraftdivider=1
minecraftunit="Bytes"
fi
let minecraftsized='(''('$minecraftsize%$minecraftdivider')'*100')'/$minecraftdivider
let minecraftsize=$minecraftsize/$minecraftdivider
logInfo() '2' "none" "Minecraft: $minecraftsize.$minecraftsized $minecraftunit"
fi
if [ "$backupMySQL" == "true" ] ; then
if [ "$sqlsize" -ge 1024 ] ; then
if [ "$sqlsize" -ge 1048576 ] ; then
if [ "$sqlsize" -ge 1073741824 ] ; then
sqldivider=1073741824
sqlunit="GB"
else
sqldivider=1048576
sqlunit="MB"
fi
else
sqldivider=1024
sqlunit="KB"
fi
else
sqldivider=1
sqlunit="Bytes"
fi
let sqlsized='(''('$sqlsize%$sqldivider')'*100')'/$sqldivider
let sqlsize=$sqlsize/$sqldivider
echo " MySQL: $sqlsize.$sqlsized $sqlunit" >> backup.log
fi
if [ "$backupWebsite" == "true" ] ; then
if [ "$websitesize" -ge 1024 ] ; then
if [ "$websitesize" -ge 1048576 ] ; then
if [ "$websitesize" -ge 1073741824 ] ; then
websitedivider=1073741824
websiteunit="GB"
else
websitedivider=1048576
websiteunit="MB"
fi
else
websitedivider=1024
websiteunit="KB"
fi
else
websitedivider=1
websiteunit="Bytes"
fi
let websitesized='(''('$websitesize%$websitedivider')'*100')'/$websitedivider
let websitesize=$websitesize/$websitedivider
echo " Website: $websitesize.$websitesized" >> backup.log
fi
echo "" >> backup.log
fi
As I stated earlier, I am completely lost, so any assistance would be greatly appreciated. Thank you!
You don't invoke a function with parentheses. Change:
logInfo() '2' "none" "Minecraft: $minecraftsize.$minecraftsized $minecraftunit"
to
logInfo '2' "none" "Minecraft: $minecraftsize.$minecraftsized $minecraftunit"

How to write an if-statement within a if-statement

How can I write something like:
if $1 = a then check second statement if $2 is b then echo a and b
else $1 = 1 then check second statement if $2 = 2 then echo 1 and 2
...where all of the variables are strings?
This is what I have:
fun() {
if [ "$1" == "a" ]; # when $1 is a then
then
if [ "$2" == "" ]; # $1 is a and $2 is empty string
echo a
elif [ "$2" == "b" ]; # $1 is a and $2 is b
then
echo "a and b"
fi
fi
else
if [ "$1" == "1" ]; # when $1 is 1 then
then
if [ "$2" == "" ]; # $1 is 1 and $2 is empty string
echo a
elif [ "$2" == "2" ]; #$1 is 1 and $2 is 2
then
echo "1 and 2"
fi
fi
}
Using a nested case statement could help you: Nested case in bash script
Your function would look like this:
fun(){
case "$1" in
"a") # $1 is 'a'
case "$2" in
"") echo "$1";; # only $1 present
"b") echo "a and b";; # $1 is 'a' and $2 is 'b'
esac;;
"1") # $1 is '1'
case "$2" in
"") echo "$1";; # only $1 present
"2") echo "1 and 2";; # $1 is '1' and $2 is '2'
esac;;
esac
}
fun() {
if [ "$1" == "a" ]; # when $1 is a then
then
if [ "$2" == "" ]; # $1 is a and $2 is empty string
then # was missing
echo a
elif [ "$2" == "b" ]; # $1 is a and $2 is b
then
echo "a and b"
fi
# fi # shouldn't be here if you want to have else
else
if [ "$1" == "1" ]; # when $1 is 1 then
then
if [ "$2" == "" ]; # $1 is 1 and $2 is empty string
then
echo a
elif [ "$2" == "2" ]; #$1 is 1 and $2 is 2
then
echo "1 and 2"
fi
fi
fi
}
"then" should be after each "if"
fun() {
if [ "$1" == "a" ]; # when $1 is a then
then
if [ "$2" == "" ]; # $1 is a and $2 is empty string
then #### 1st omitted "then"
echo a
elif [ "$2" == "b" ]; # $1 is a and $2 is b
then
echo "a and b"
fi
# fi #### this fi should be in the end
else
if [ "$1" == "1" ]; # when $1 is 1 then
then
if [ "$2" == "" ]; # $1 is 1 and $2 is empty string
then #### 2nd omitted "then"
echo a
elif [ "$2" == "2" ]; #$1 is 1 and $2 is 2
then
echo "1 and 2"
fi
fi
fi #### here
}

Resources