Version 12 (modified by 2 years ago) ( diff ) | ,
---|
Hands-On
Linux programming (scripting)
Shell Scripting is the language of the linux terminal. Shell scripts are sometimes referred to as “shebang” which is derived from the “#!” notation. Shell scripts are executed by interpreters present in the linux kernel. Interpreters include: bash, csh, zsh e.t.c. Most popular of which is bash.
Typical operations performed by shell scripts include file manipulation, program execution, and printing text.
How to Write Shell Script in Linux/Unix
- Create a file using a vi editor(or any other editor). Name script file with extension . sh.
- Start the script with #! /bin/sh.
- Write some code.
- Save the script file as filename.sh.
- For executing the script type bash filename.sh.
File Color : Executable scripts appear in a different colour from rest of the files and folders.
Let's start to have some experience on scripting
Simple Script for newbies
First we have to create a text file
$cat > hello.sh
And create the content in it
#!/bin/sh echo 'Hello World'
#!/bin/sh
is "how to run" the program.
echo 'Hello World'
is "what to do" in the script
Then make the file executable
$ chmod +x hello.sh
And run the executable file
$./hello.sh
Output:
Hello world
Make yourself familiar with man man
and man apropos
on the shell. It will need for your self-tutoring.
Let's make this little advanced
How to define variables
Variables can be defined using the syntax variable_name=value
. To get the value of the variable, add $
before the variable
#!/bin/bash # A simple variable example greeting=Hello name=world echo $greeting $name
Arithmetic Expressions
These are the operstors supported by bash for mathematical calculations.
+ addition - subtraction * multiplication / division ** exponentiation % modulus
Numerical expressions can also be calculated and stored in a variable using the syntax below:
value=$((expression))
#!/bin/bash var=$((3+9)) echo $var
How to read user input
Sometimes user input are needed to perform relevant operations.
In bash, user inputs are taken using the read
command.
read variable_name
To prompt the user with a custom message, use the -p
flag.
read -p "Enter your age" variable_name
Numeric Comparison logical operators
Comparison is used to check if statements evaluate to true or false.
Equality num1 -eq num2 is num1 equal to num2 Greater than equal to num1 -ge num2 is num1 greater than equal to num2 Greater than num1 -gt num2 is num1 greater than num2 Less than equal to num1 -le num2 is num1 less than equal to num2 Less than num1 -lt num2 is num1 less than num2 Not Equal to num1 -ne num2 is num1 not equal to num2
Syntax:
if [ conditions ] then commands fi
Conditional Statements (Decision Making)
Conditions are expressions that evaluate to a boolean expression (true or false). To check conditions, we can use if
, if-else
, if-elif-else
and nested conditionals.
Looping and skipping
For loops allow you to execute statements a specific number of times.
Looping with numbers:
#!/bin/bash for i in {1..5} do echo $i done
Looping with strings:
#!/bin/bash for X in cyan magenta yellow do echo $X done
While loop
While loops check for a condition and loop until the condition remains true. We need to provide a counter statement that increments the counter to control loop execution.
#!/bin/bash i=1 while [[ $i -le 10 ]] ; do echo "$i" (( i += 1 )) done
Reading files:
Suppose we have a file sample_file.txt as shown below:
#!/bin/bash LINE=1 while read -r CURRENT_LINE do echo "$LINE: $CURRENT_LINE" ((LINE++)) done < "sample_file.txt"
How to execute commands with back ticks
If you need to include the output of a complex command in your script, you can write the statement inside back ticks.
syntax:
var= ` commands `
Example: Suppose we want to get the output of a list of mountpoints with tmpfs in their name. We can craft a statement like this: df -h | grep tmpfs.
#!/bin/bash var=`df -h | grep tmpfs` echo $var
How to Automate Scripts by Scheduling via cron Jobs
Cron is a job scheduling utility present in Unix like systems. Jobs can be scheduled to execute daily, weekly, monthly or in a specific time of the day. Automation in Linux heavily relies on cron jobs.
Below is the syntax to schedule crons:
# Cron job example * * * * * sh /path/to/script.sh
Here, * represent represents minute(s) hour(s) day(s) month(s) weekday(s), respectively.
Github administration
Install Git and create a Github account
To see if you already have Git installed, open up your terminal application.
Once you've opened your terminal application, type git version. The output will either tell you which version of Git is installed, or it will alert you that git is an unknown command. If it's an unknown command, read further and find out how to install Git.
Install git:
Git packages are available using apt
.
It's a good idea to make sure you're running the latest version. To do so, Navigate to your command prompt shell and run the following command to make sure everything is up-to-date: sudo apt-get update
.
To install Git, run the following command: sudo apt-get install git-all
.
Once the command output has completed, you can verify the installation by typing: git version
.
To create the github account:
Navigate https://github.com and signup with your details
Install source tree on your computer
Create a local Git repo
Every git repository has three trees. A working directory, Index and Head. ====
- Working directory: It contains the actual project files.
- Index: It is the staging area where you add the project files that needs to be committed.
- Head: Head is where the reference to you previous commit exists.
git init is one way to start a new project with Git. To start a repository, use either git init
or git clone
- not both.
To initialize a repository, Git creates a hidden directory called
.git
. That directory stores all of the objects and refs that Git uses and creates as a part of the project's history. This hidden.git
directory is what separates a regular directory from a Git repository.
git init: One Person Starting a New Repository Locally git clone: The Remote Already Exists
Add a new file to the repo
Add a file to the staging environment
Create a commit
Create a branch
Create a new repo on Github
Push a branch to GitHub
Create a pull request
Merge a pull request
Get changes on your Github back your computer
Committing new changes to the repository (commit):
Once you have added all the files to the staging area, you can commit the changes with a reference message using the “-m” flag as shown below.
git commit -m “my first commit”
Checking out a repository
You can create a copy of your git repository using the clone command. Execute the following command to clone your project directory.
git clone /path/to/project-repository
Adding file to the staging area: (add)
git add <filename>
Let’s say you want to add all the files in your project directory to the staging area. Execute the following command to do the same
git add --all