3.3 - Linux : Shell Scripting - Basics
Programming languages:
- There are various types of programming languages, compared on various parameters
- From Embedded system engineer’s view it should be seen how close or how much away from the hardware the language is, Based on that view programming languages can be categorized into three areas:
• Assembly language (ex: 8051)
• Middle level language (ex: C)
• High level / Scripting language (ex: Shell)
• Middle level language (ex: C)
• High level / Scripting language (ex: Shell)
- Each programming language offers some benefits with some shortcomings:
1. Depending on the need of the situation appropriate language needs to be chosen
2. This make language selection is a key criteria when it comes to building real time products!
A Comparison of Assembly, C, & Shell Languages:
Language Parameter | Assembly | C | Shell |
---|---|---|---|
Speed | High | Medium | Medium |
Portability | Low | Medium | High |
Maintainability | Low | Medium | High |
Size | High | Medium | Low |
Easy to Learn | Low | Medium | High |
- Shell or any scripting language is also called as ‘interpreted’ language as it doesn’t go through compilation phase. This is to keep the language simple as the purpose is different than other languages.
Shell Script:
- Any collection of shell commands can be stored in a file, which is then called as shell script
- Programming the scripts is called shell scripting
- Scripts have variables and flow control statements like other programming languages
- Shell script are interpreted, not compiled
- The shell reads commands from the script line by line and searches for those commands on the system
Applications of use?
- System Administration
• Automate tasks
• Repeated tasks
• Repeated tasks
- Development
- Allows testing a limited sub-set
- Testing tools
- Automated Data Back Up's
- Simple scripts (Such as Restart, Software Updates)
- Reminders, e-mails etc.
Invoking scripts
Example:
$ vi hello.sh
- Next, type the following inside it:
#!/bin/bash
echo “ Hello World”
- Then, make the script executable:
$ chmod 700 hello.sh
$ ./hello.sh
- First line tells Linux to use BASH interpreter
- Second line prints the “Hello world” into standard I/O
Variables
- Variables are a way of storing information temporarily.
NAME=”CodeGig”
X=10
- A couple of conversions we need to follow
• Variables usually appear in uppercase
• There is no white space between the variable name and the equal sign
• There is no white space between the variable name and the equal sign
- Variable substitution
- Variable assignment
- Bash variables & special variables
Whitespace & Line-breaks
- Bash shell scripts are very sensitive to whitespace & line-breaks
- Because the “keywords” of this programming language are actually commands evaluated by the shell
- Need to separate arguments with whitespaces
- Likewise a line-break in the middle of a command will mislead the shell into thinking the command is incomplete.
Example: x=10; x = 10; x = “ok”; x=”ok”
Special characters
Character | Meaning |
---|---|
- | The current user's home directory |
$ | used to access a variable (e.g. : $HOME) |
& | used to put a command in the background |
* | wildcard, matching zero or more characters (e.g. : ls doc_*) |
? | wildcard, matching exactly one character (e.g.: ls doc_?) |
${#} | No of arguments passed to shell script |
${@} | Value of all arguments passed |
$0 | contains the name of the script user executed |
Quotes
- Using single quotes causes the variable name to be used literally, and no substitution will take place.
$var=’test string’
$newvar=’Value of var is $var’
echo $newvar
- Using double quotes to show a string of characters will allow any variables in the quotes to be resolved
$var=“test string”
$newvar=“Value of var is $var”
echo $newvar
Expressions :
- expr: Evaluates simple math on the command line calculator.
- bc: An arbitrary precision calculator language
- Available operators: +, , /, *, %
Example :
#!/bin/bash #Sample Shell Program for Math Operations. echo "Enter the First Number : " read x echo "Enter the Second Number : " read y add=$(($x+$y)) sub=$(($x-$y)) mul=$(($x*$y)) div=$(($x/$y)) mod=$(($x%$y)) echo "Addition of X & Y = $add" echo "Subtraction of X & Y = $sub" echo "Multiplication of X & Y = $mul" echo "Division of X & Y = $div" echo "Modulus of X & Y = $mod"
If-then condition
- The if statement chooses between alternatives each of which may have a complex test.
- The simplest form is the if-then statement.
#!/bin/bash #Demonstration of if Condition a=4 b=6 if [ $a -gt $b ] then echo "A is greater than B" else echo "B is greater than A" fi
If-then-elif
- Multiple if blocks can be strung together to make an elaborate set of conditional responses
-Syntax
if [ condition_A ]
then
#condition_A is true
elif [ condition_B ]
then
#condition_A false & condition_B true
else
#both conditions false
fi
....
Post a Comment