Lab 7 – Bash Script CIE 302 - Operating systems
What is bash scripting?
Bash script: a plain text that contains a series of commands, which can be used to
write Linux commands in the terminal.
Anything you can run normally on the command line can be put into a script and it
will do exactly the same thing.
Bash scripts an extension of .sh (myscript.sh). However, Linux is an extensionless
system so a script doesn't necessarily have to have this characteristic in order to
work.
How to execute a bash script?
./script it will execute the script file, as a new child
process within the bash/terminal.
to grant permissions, to execute file:
chmod 755 script1 (rwxr-xr-x) The file's owner may read, write, and execute the file. All
others may read and execute the file.
Script format
#!/bin/bash The first line in code, must start with “#!”.
# A sample Bash script comment line, not executed.
echo Hello World! actual command that could be directly written in the terminal.
Variables
To declare a variable:
$a, $A, $a1A.
To initialize a variable
a=4
To display the variable and its value
echo $a
Variable substitution
var='Hello Student'
text=" $var Ahmed"
echo $text
If statements
If statements
For loop
While loop
Functions
To declare a variable:
$a, $A, $a1A.
To initialize a variable
a=4
To display the variable and its value
echo $a
Variable substitution
var='Hello Student'
text=" $var Ahmed"
echo $text