@Lahiru-Amarathunga bash script gana lipi pelak post karala thiyenne gihin balanna
Binura Yeshan Samarawickrama
@Binura Yeshan Samarawickrama
Posts made by Binura Yeshan Samarawickrama
-
RE: Lets talk about bash script
-
Logical Operators in bash script(And, OR, NOT)
හරි මම ඔයාලට කියල දෙන්නම් අද කොහොමද BASH Script වලදි Logical expression use කරන්නෙ කියලා..
#!/bin/bash echo "Enter marks :"#user ගෙන් input එක ඉල්ලන්න read mark #input එක ගන්න if [ $mark -ge 80 -a $mark -le 100 ] #enter කරපු mark එක 80ට වඩා වැඩිද(-ge 80) ඒ වගේම (-a) 100ට වඩා අඩුද(-la 100) then #එහෙමනම් echo "A" #A කියල print කරන්න elif [ $mark -ge 60 -a $mark -le 79 ] then echo "B" elif [ $mark -ge 40 -a $mark -le 59 ] then echo "C" elif [ $mark -ge 20 -a $mark -le 39 ] then echo "D" elif [ $mark -ge 10 -a $mark -le 19 ] then echo "E" else echo "Invalid mark" fi
-
File Test Operators(bash script use කරල file එකක් හදල permission දීල run කරමු)
හරි මම ඔයාලට කියල දෙන්නෙ මේ වෙනකන් කියල දීපු හැම පොස්ට් එකකින්ම ගත්ත දේවල් එකතු කරල කොහොමද linux වලදි එක ස්ක්රිප්ට් එකකින් ෆයිල් එකක් හදල ඒක ඇතුලෙ දෙයක් type කරල ඒකට execute permission දීලා ඒක run කරන්නෙ කියලා.
හරි දැන් ඔයාල මෙන්න මේ වගේ sh file එකක් හදාගෙන script එක ලියන්න.
#!/bin/bash cd ~ #මේ කියන්නෙ ඔයා ඉන්න directory එකෙන් ඊට කලින් තිබ්බ directory එකට යන්න කියල මම මේ ඉන්නෙ Documents Directory එකේ තියෙන folder එකක ඒ නිසා මම මගේ User Directory එකට යන්න දෙපාරක් back වෙන්න ඕන cd ~ # මේ දෙවනි එක cd Desktop# හරි මම දැන් ඔන්න Script එකේ ලියනවා Desktop Directory එකට එන්න කියල if [ -d new ] #මේ කියන්නේ new කියල Directory එකක් තියෙනවද කියල බලන්න කියන එක then echo "Directory available" #එහෙම තියෙනවනම් Directory available කියල display කරන්න cd new #තියෙනවනම් ඒක ඇතුලට යන්න else echo "Directory not available so i am creating file.."#එහෙම නැත්නම්not Directory available කියල display කරන්න mkdir new#Directory එක හදන්න cd new#දැන් ඒක ඇතුලට යන්න fi #එක decision making command set එකක් ඉවරයි if [ -f foo.sh ] #හරි දැන් බලන්න foo.sh කියල file එකක් තියෙනවාද කියල then echo "File available"#එහෙම තියෙනවානම් File available කියල print කරන්න else echo "File not available so i am creating file..." #එහෙම නැත්නම් File not available so i am creating file..." කියල print කරලා touch "foo.sh"; echo "#!/bin/bash" >> foo.sh #File එක හදන්න හදලා ඒකෙ #!/bin/bash කියල type කරන්න කියන්න fi if [ -e foo.sh ] #හරි මේ බලන්නෙ ඒ හදපු ෆයිල් එක ඇතුලෙ text මොනවා හරි තියෙනවාද කියල then echo "available"# එහෙම තියෙනවානම් available කියල print කරන්න else echo "Not available so i am writing.." #එහෙම නැත්නම් echo "#!/bin/bash" >> foo.sh##!/bin/bash කියල ෆයිල් එක ඇතුලෙ ටයිප් කරන්න fi if [ -x foo.sh ]#මේ කියන්නෙ ඒ හදපු ෆයිල් එකට execution permission දීලද බල්න්න කියල then echo "Yes executable file so i am opening file"#එහෙම දීලනම් open foo.sh#open කරන්න ./foo.sh#Execute කරන්න else echo "Not a executable file i give permission and opening file"#file එකට permission දීල නැත්නම් chmod +x foo.sh #permission දෙන්න open foo.sh ./foo.sh fi echo "enter yes if you want edit file otherwise enter no" # හරි මම දැන් කියනවා ඔයාට ඒ file එක ආයිත් edit කරන්න ඕනනම් yes කියල type කරන්න එපානම් No කියල ටයිප් කරන්න read input #user ගේ input එක ගන්න if [ "$input" = "yes" ] # input එක yes නම් then open foo.sh # ෆයිල් එක open කරන්න else echo "Work done bye!!!!" fi
-
Arithmetic operators in BASH scripting
අද කතා කරන්නේ Basic Arithmetic operators ටික ගැන. ඒ කියන්නෙ එකතු කිරීම, අඩු කිරීම , බෙදීම , ගුන කිරීම.
ඔයාල දැන් දන්නවනේ කොහොමද bash script file එකක් හදාගන්නෙ කියල.. දන්නෙ නැති අය මෙ Link එක Use කරල ඔක්කොම ටික බලන්න - https://binurayeshan.blogspot.com
හරි දැන් ඔයාලා හදා ගත්ත script file එකේ මෙන්න මේ code line ටික type කරන්න.
#!/bin/bash a=2 b=5 c=9 echo "$a,$b,$c" echo "Sum of variable are = $[a+b+c]" #එකතු කිරිම් echo "Value of c MOD b = $[c%b] " #mod එක ඒ කියන්නෙ බෙදුවම එන ඉතිරිය echo "Value of c MOD b = $[c-b] " #අඩු කිරීම echo "Value of a * c = $[a*c]" #ගුණ කිරීම echo "Value of c / b = $[c/b]" #බෙදීම
-
DECISION MAKING IN SHELL SCRIPTS Part 2(කොහොමද Arguments දෙකක් එක්ක වැඩ කරන්නෙ)
හරි මම ඔයාලට කියල දෙන්නම් කොහොමද conditions දෙකක් check කරන්නෙ කියල.
මෙන්න මේකයි Programm එක. userගෙන් input දෙකක් ගන්නව අරගෙන එයා ඒ දෙකටම inputs දුන්නද නැද්ද කියල check කරනව.#!/bin/bash echo "Enter argument 1 :" #argument 1 එක එන්ටර් කරන්න කියනවා read arg1 #User ගෙන් input එක ගන්නවා echo "Enter argument 2 :" read arg2 if [ -n "$arg1" ] #මේ කියන්නෙ arg1 එක මෙකේ value එක Zero නෙමේනම් කියන එක then #එහෙමන්ම් echo "Argument 1 has value"#Argument 1 has value කියල print කරන්න elif [ -z "$arg1" ]#මේ කියන්නෙ arg1 එක මෙකේ value එක Zero නම් කියන එක then #එහෙමන්ම් echo "Argument 1 has no value"#Argument 1 has no value කියල print කරන්න fi#මේ කියන්නෙ මෙන්න මේ පටන් ගත්ත පලවෙනි dicision එක close කරන්න කියල if [ -n "$arg2" ] then echo "Argument 2 has value" elif [ -z "$arg2" ] then echo "Argument 2 has no value" fi
මේ link වලින් ගිහින් ඔයාලට bash script ගැන කලින් කරපු post බලාගන්න පුලුවන්..
Lets talk about bash script - https://lankadevelopers.com/topic/371/lets-talk-about-bash-script
First bash script - https://lankadevelopers.com/topic/372/first-bash-script
Sum of two numbers in bash script - https://lankadevelopers.com/topic/377/sum-of-two-numbers-in-bash-script
Bash script Array introduction - https://lankadevelopers.com/topic/386/bash-script-array-introduction
-
DECISION MAKING IN SHELL SCRIPTS(Write a shell script to determine whether a given number is either positive or negative)
මම අද ඔයාලට කියල දෙන්නෙ කොහොමද shell scripting පාවිච්චි කරල DECISION MAKING simple programme එකක් ලියන්නෙ කියල.
මේ link වලින් ගිහින් ඔයාලට bash script ගැන කලින් කරපු post බලාගන්න පුලුවන්..
Lets talk about bash script - https://lankadevelopers.com/topic/371/lets-talk-about-bash-script
First bash script - https://lankadevelopers.com/topic/372/first-bash-script
Sum of two numbers in bash script - https://lankadevelopers.com/topic/377/sum-of-two-numbers-in-bash-script
Bash script Array introduction - https://lankadevelopers.com/topic/386/bash-script-array-introduction
ඔයාලට කියල දුන්න විදියට .sh File එකක් හදාගන්න. දැන් ඔයාලා මෙන්න මේ code එක ඔයාලගෙ script එකේ ලියන්න.
#!/bin/bash echo "Enter mark :" read number #ඔයාගේ mark එක read කරනව if [ $number -lt 0 ]# මේ කියන්නෙ enter කරපු mark එක less than(අඩුයිනම්) 0 ට වඩා then #එහෙනම් echo "Negetive" #negative කියල print කරන්න elif [ $number -gt 0 ] # මේ කියන්නෙ enter කරපු mark එක greater than(වැඩියිනම්) 0 ට වඩා then #එහෙනම් echo "Positive" #Positive කියල print කරන්න else #ඒ දෙකම නෙමෙයිනම් echo "Neither Positive nor negative" fi
-
unsert variables in Bash Scripting
මේ link වලින් ගිහින් ඔයාලට bash script ගැන කලින් කරපු post බලාගන්න පුලුවන්..
Lets talk about bash script - https://lankadevelopers.com/topic/371/lets-talk-about-bash-script
First bash script - https://lankadevelopers.com/topic/372/first-bash-script
Sum of two numbers in bash script - https://lankadevelopers.com/topic/377/sum-of-two-numbers-in-bash-script
Bash script Array introduction - https://lankadevelopers.com/topic/386/bash-script-array-introduction
Read only variables in Bash Scripting - https://lankadevelopers.com/topic/387/read-only-variables-in-bash-scripting
අපි දැන් බලමු මොකක්ද මේ unset variables කියල කියන්නෙ කියල.
මෙන්න මෙහෙමයි
හිතන්න අපි මුලින්ම assign කරපු variable එකක value එක පස්සෙ අයින් කරන්න ඕනි කියල අපි මේකට use කරන්නෙ unset variables#!/bin/bash y=7 #y ගේ value එක 7 යි කියල දෙනව echo "$y" #ඒක ප්රින්ට් කරනවා unset y #ඔන්න දැන් value එක අයින් කරනවා echo "$y" #දැන් ප්රින්ට් කරල බල්න්න...
-
Read only variables in Bash Scripting
මේ link වලින් ගිහින් ඔයාලට bash script ගැන කලින් කරපු post බලාගන්න පුලුවන්..
Lets talk about bash script - https://lankadevelopers.com/topic/371/lets-talk-about-bash-script
First bash script - https://lankadevelopers.com/topic/372/first-bash-script
Sum of two numbers in bash script - https://lankadevelopers.com/topic/377/sum-of-two-numbers-in-bash-script
Bash script Array introduction - https://lankadevelopers.com/topic/386/bash-script-array-introduction
මොකක්ද මේ කතාව හරි මෙන්න මෙහෙමයි.
අපි Bash Script එකක් ලියනකොට අපිට ඕනි වෙනව වෙනස් කරන්න බැරි විදියට variables හදන්න ඒ කිවුවේ මෙන්න මෙහෙම.
හිතන්නකො මම මුලින්ම x = 5 කියල value එකක් දෙනව කියල මම ඊලග code line එකේ x=8 කියල දුන්නම x ගේ value එක 8 ක් වෙනවාx=5 echo "$x" x=8 echo "$x"
මෙන්න මේ වැඩේ අපිට නතර කරගන්න පුලුවන් readonly variable use කලාම. ඔන්න බලන්න ඒක කරන විදිය
#!/bin/bash readonly x=5 #අපි x ව readonly variable එකක් කලා echo "$x"#එයාව print කරනවා x=9 #ඔන්න දැන් එයාව වෙනස් කරන්න හදනව ඒ නිසා අපිට පෙන්නනව එයාව වෙනස් කරන්න බෑ කියල echo "$x"