What's new

Closed Bash scripting 101

Status
Not open for further replies.

PUTANKINNAMALL

Forum Veteran
Elite
Joined
May 12, 2016
Posts
1,647
Reaction
790
Points
695
Ano ang bash?
Bash (Bourne Again Shell) is a unix shell written by Brian Fox for the GNU project to replace sh (Bourne Shell).
It is a command processor that typically runs in a text window, where the user types commands that cause actions. Bash can also read commands from a file, called a script. Like all Unix shells, it supports filename wildcarding, piping, here documents, command substitution, variables and control structures for condition-testing and iteration.
-golgol

My First Script

magsimula tayo sa simple na si "hello world"
Code:
#!/bin/bash
echo "Hello World"
1st line isinasaad nito kung anong application sa script. so its bash.
2nd line the 'echo' inuutusan niya ang terminal na iprint mo nga ito which is 'hello world' w/out qoutes.

Running the bash script
ayan nakagawa nako ng my firstever.sh which .sh ang extension niya.
para runnable yong script mo need mo change yong excute permission sa tamang owners tawag chmod o change mode +"flags" "filename. sh" check mo sa command ls - la kung my nkita kang nagbago

Code:
chmod +x filename.sh

ngayon na bago na ang permission.
run this code
Code:
filename. sh
ito yung linaka common way to execute a script. It is preferred to execute the script like this in a subshell.

Variables
eto yung meron sa kahit anong programming languages. Ang variables sa bash pwedeng may value ng string, integers, characters. di na kailangang i declare yung variable. just assign a value to its reference then will auto create it.

Code:
#!/bin/bash     
BH="Hello World!"
echo $BH

2nd Line creates a variable called BH and assigns the string "Hello World!" to it. at yung VALUE of this variable is retrieved by putting the '$' in at the beginning. Please notice (try it!) that if you don't use the '$' sign, ang output magiging iba the way it is.

Conditionals

ito naman ay pinapipili whether to perform action or not.. nagagamit to sa mga statement tulad if, elseif and multiple other statements.. fashion statements joke haha.

If
Code:
#!/bin/bash

[B]      if [ "foo" = "foo" ]; then
      echo expression evaluated as true
      fi

Programming conditional statement that, if proved true magdidisplay siya ng info. Below is a general example of an if statement, not specific to any particular programming language. The code to be executed if the expression within braces is true can be found after the 'then' word and before 'fi' which indicates the end of the conditionally executed code.


If then else

Code:
#!/bin/bash
      if [ "420" = "420" ]; then
      echo expression evaluated as true
      else
      echo expression evaluated as false
      fi
nakasaad naman dito if ang 420 is equal to 420 then it excute value is true and
and if not it will return false.. at kung walang feedback then might have some glitch in your code or error.

User Interfaces(UIs)
seems interesting. Graphical user interfaces (GUI) accept input via devices such as a computer keyboard and mouse and provide articulated graphical output on the computer monitor.
Code:
#!/bin/bash
      OPTIONS="Hello Quit"
      select opt in $OPTIONS; do
      if [ "$opt" = "Quit" ]; then
      echo done
      exit
      elif [ "$opt" = "Hello" ]; then
      echo Hello World
      else
      clear
      echo bad option
      fi
      done
same lang to sa if statements. So options are either "HELLO" or "QUIT".This code says if the opt(option) in our variable OPTIONS is "quit" then we will execute the echo "done" and then "exit" which would exit our if statement so the code doesn't carry on to our elif statement. Now, in our elif statement, if it is "HELLO" then it will execute Hello world. If it isn't either of them, the screen will clear.

User Input
Code:
#!/bin/bash
      echo Please, enter your name
      read NAME
      echo "Hi $NAME!"
So this will as for a user input by saying "Please enter your name". Upon a users input, the input will go into the $NAME variable and then once it is read it will echo out the variable after the word "Hi". So it would look something like "Hi im ******!"

Select
Code:
echo "Do you wish to install this program?"
select yn in "Yes" "No"; do
    case $yn in
      Yes ) make install; break;;
      No ) exit;;
    esac
done
this one prompts you with number na pipiliin mo. Select also loops automatically... there's no need for a 'while true' loop to retry if they give invalid input. sa aking kakaramlot na kaalaman sa code, it asks you if you wish to install a program. We then set an option for either "y/n == "Yes", "No". If the case is "y" or "Yes", it will make install; break;;. If the case is "No" it will exit the script(Not install the program that you wish to do. Once it runs through those options, if neither is enter Y/N(Yes, No) it will execute "done" which will close the script and "not install the program."

ok that all folks have a nice evening.
feel free to bash[/B]
 
Status
Not open for further replies.
Back
Top