100% found this document useful (1 vote)
28 views

Module2 Session4

This document provides an introduction to a Linux course on bioinformatics. It covers key topics like file permissions, environment variables, and shell scripting. The learning objectives are to understand permissions, writing scripts, important environment variables, and connecting to remote machines via SSH. The course will teach how to change permissions, write simple scripts, know common environment variables, and connect to remote servers.

Uploaded by

Papillon Blanc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
28 views

Module2 Session4

This document provides an introduction to a Linux course on bioinformatics. It covers key topics like file permissions, environment variables, and shell scripting. The learning objectives are to understand permissions, writing scripts, important environment variables, and connecting to remote machines via SSH. The course will teach how to change permissions, write simple scripts, know common environment variables, and connect to remote servers.

Uploaded by

Papillon Blanc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Introduction

to Bioinformatics online course: IBT

Linux
Linux: Permissions, groups, and process
control

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Learning Objectives

① Understand file and directory permissions


and how to change them
②Understand loops, variables and script
generation to automate tasks
③ Understand environment variables and why
they are important
④Learn how to ssh onto a remote machine

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Learning Outcomes

① Be able to change file permissions


②Be able to write a simple script
③Know some of the environment variables
④Be able to connect via ssh onto a remote
machine

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Part 1

Files and directories permissions

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Linux is a multi-users OS

• On a Linux system, each file and directory is


assigned access rights for the owner of the
file, the members of a group of related users,
and everybody else

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Remember the ls -l example

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Permissions are broken into 4 sections

Source: www.pluralsight.com

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Access permissions on files

• r indicates read permission: the permission to


read and and copy the file

• w indicates write permission: the permission


to change a file

• x indicates execution permission: the


permission to execute a file, where
appropriate

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Access permissions on directories

• r indicates the permissions to list files in the


directory

• w indicates that users may delete files from the


directory or move files into it

• x indicates means the right to access files in the


directory. This implies that you may read files in
the directory provided you have read permission
on the individual files
Introduction to Bioinformatics online course: IBT
Linux | Amel Ghouila
chmod command
• Used to change the permissions of a file or a
directory.
• Syntax: chmod options permissions filename
• Only the owner of the file can use chmod to
change the permissions
• Permissions define permissions for the owner,
the group of users and anyone else (others)
• There are two ways to specify the permissions:
üSymbols: alphanumeric characters
üOctals: digits (0 to 7)

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
chmod options
Symbol Meaning
u user
g group
o other
a all
r read
w write (and delete)
x execute (and access directory)
+ add permission
- take away permission

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Octal permissions
• 4 stands for "read”
• 2 stands for "write”
• 1 stands for "execute”
• 0 stands for "no permission"

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
chmod examples
• chmod u=rwx,g=rx,o=r filename
same as
• chmod 754 filename

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
More examples
• 777: (rwxrwxrwx) No restrictions on permissions. Anybody
may do anything
• 755: (rwxrxrx) The file's owner may read, write, and
execute the file. All others may read and execute the file
(common for programs that are used by all users)
• 700: (rwx) The file's owner has all the rights. Nobody else
has any rights (private for the owner)
• 666: (rwrwrw) All users may read and write the file.
• 644: (rwrr) The owner may read and write a file, while all
others may only read the file (everybody may read, but
only the owner may change)
• 600: (rw) The owner may read and write a file. All others
have no rights

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Part 2

Environment variable

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Variables
• Variables are areas of memory that can be used to
store information and are referred to by a name
• How to create a variable: a line that contains the name
of the variable followed immediately by an equal sign
("=").
• 2 types of variables: shell variables and environment
variables
• Some variables are already set in your shell session
• printenv: prints the values of all your environment
variables

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
What is an environment variable
• An environment variable is a dynamic "object" on
a computer that stores a value, which in turn can
be referenced by one or more programs.
• Environment variables help programs know what
directory to install files in, where to store
temporary files, where to find user profile
settings, and other things.
• Environment variables help to create and shape
the environment of where a program runs.
Introduction to Bioinformatics online course: IBT
Linux | Amel Ghouila
Examples of environment variables
• HOME: the environmental value that shows the
current user's home directory,
• PATH: the environmental variable, which contains
a colon-separated list of the directories that the
system searches to find the executable program
corresponding to a command issued by the user
• PWD: always stores the value of your current
working directory

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Part 3

Shell scripting

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
echo command
• Syntax: echo options arguments
• Writes arguments to the standard output
• echo: just prints its command-line parameters
to standard output
• If you redirect the result your arguments will
be written into the file you are redirecting to
• Commonly used by the shell scripts to display
results or ask the user to enter parameters

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Let’s echo some stuff
ü echo Bioinformatics is great starting writing scripts
ü If you want to jump to another line add \n and use the
option –e
ü echo –e “Bioinformatics is great \n starting writing
scripts”
ü Setting a variable: X=firstvariable
ü echo X: prints X
ü echo $X prints firstvariable (the value of the variable)
ü echo ‘$X’ è $X
ü echo “$X” è firstvariable

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Print the result of a command
• Asking the shell to substitute the results of a
given command
• `command` or $(command)

• echo `pwd` or echo $(pwd)

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
What is a shell script
• Short programs written in shell programming
language useful to automate tasks under Linux OS
• A shell script is a file containing a series of
commands
• Could be helpful to perform the same actions on
many different files
• Shell script= scripting interpreter+ command line
interface to the system
• echo is also commonly used to have a shell script
display a message or instructions, such as Please
enter Y or N in an interactive session with users.
Introduction to Bioinformatics online course: IBT
Linux | Amel Ghouila
Let’s start using the power of scripting
1. nano myfirstscript
2. Write the content of your script for example:
#!/bin/bash The shebang
clear
echo “Hey, I am starting writing shell scripts”
echo “Let the fun begin!!! ”

3. Run your script (using ./)


4. Change the rights to make sure you have the right to
execute
chmod u+x myfirstscript
or
chmod 744 myfirstscript

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
The shebang
#!interpreter
#!/bin/bash

Indicates the beginning


of a script The program interpreter:
absolute path to an executable
• A perl script could begin by #!/usr/bin/perl
program

(absolute path)
• You can use the which command to locate the
executable file associated with a given command
• which perl è /usr/bin/perl
• which bash è /bin/bash
Introduction to Bioinformatics online course: IBT
Linux | Amel Ghouila
Use variables in your scripts
• Makes your script easier to maintain
• Reduces the amount of typing !

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
If statements in shell scripting
Example:
Syntax:
#!/bin/bash
if [ conditional echo Let’s try some conditional tests
expression ] x=`find *.fasta| wc -l`
echo “The current working directory
then contains $x fasta files”
if [ $x -gt $y ] # if (($x > $y))
commands then
else echo there are many existing fasta files in
this directory
commands else
echo There are very few fasta files in this
fi directory here is the listing: `ls *.fasta`
fi

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Loops in shell scripting (for)
Syntax: Example:
#!/bin/bash
for variable in values for x in file1 file2
do do
commands head -n 3 $x
done echo operation
completed on file:$x
done

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Loops in shell scripting (while)
Syntax: Example:
#!/bin/bash
n=1
while [ condition ]
while [ $n <= 5 ] #n should
do have an initial value
command1 do
command2 echo Welcome $n times
done n=$(( n+1 )) #increment
$n
done
Introduction to Bioinformatics online course: IBT
Linux | Amel Ghouila
Operators supported by shell (1)
Examples: consider 2 variables a= 10 and b=20
Operator Description Example
Addition - Adds values on either side of the
+ `expr $a + $b` will give 30
operator
Subtraction - Subtracts right hand operand from
- `expr $a - $b` will give -10
left hand operand
Multiplication - Multiplies values on either side
* `expr $a \* $b` will give 200
of the operator
Division - Divides left hand operand by right hand
/ `expr $b / $a` will give 2
operand
a=$b would assign value of b
= Assignment - Assign right operand in left operand
into a
Equality - Compares two numbers, if both are
== [ $a == $b ] would return false.
same then returns true.
Not Equality - Compares two numbers, if both
!= [ $a != $b ] would return true.
are different then returns true.
Source: http://www.tutorialspoint.com/unix/unix-basic-operators.htm
Introduction to Bioinformatics online course: IBT
Linux | Amel Ghouila
Operators supported by shell (2)
Examples: consider 2 variables a= 10 and b=20
Operator Description Example
Checks if the value of two operands are equal or not, if yes [ $a -eq $b ] is not
-eq
then condition becomes true. true.
Checks if the value of two operands are equal or not, if
-ne [ $a -ne $b ] is true.
values are not equal then condition becomes true.
Checks if the value of left operand is greater than the value [ $a -gt $b ] is not
-gt
of right operand, if yes then condition becomes true. true.
Checks if the value of left operand is less than the value of
-lt [ $a -lt $b ] is true.
right operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal to
[ $a -ge $b ] is not
-ge the value of right operand, if yes then condition becomes
true.
true.
Checks if the value of left operand is less than or equal to the
-le [ $a -le $b ] is true.
value of right operand, if yes then condition becomes true.
Checks if the value of two operands are equal or not, if yes [ $a -eq $b ] is not
-eq
then condition becomes true. true.
Source: http://www.tutorialspoint.com/unix/unix-basic-operators.htm
Introduction to Bioinformatics online course: IBT
Linux | Amel Ghouila
Part 4

Controlling tasks

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Commands to control
processes
• ps: list the processes running on the system

• kill: send a signal to one or more processes


(usually to "kill" a process)
• jobs: an alternate way of listing your own
processes
• bg: put a process in the background

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Launching a background job

• Programs that takes time or open a new Graphical User


Interface

è The prompt doesn’t reappear after the program launched.


The shell is waiting for the program to
finish before control returns to you
• ctlr+Z: interrupts a program
Or
• You can put it in the background so that the prompt will
return immediately

è Use the command name followed by & to do so

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Part 5

SSH into remote machine

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
What is SSH
• SSH (secure Shell) is a protocol used to securely log
onto remote systems (remote Linux machine and Unix-
like servers)
• ssh command is the tool used in Linux to connect via
SSH protocol
• Syntax: ssh remoteusername@remotehost
• Remote host could be an IP address or domain name
• You will be asked to provide your password
• To exit and go back to your into your local session, use
exit

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Multi-users in a Linux machine
• While your computer only has one keyboard and
monitor, it can still be used by more than on user.
• For example, if your computer is attached to a
network, or the Internet, remote users can log in
via ssh (secure shell) and operate the computer.

• Remote users can execute applications and have


the output displayed on a remote computer

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Copy files from or to a remote
machine
• scp: secure copy
• Syntax: scp pathfrom pathto
• The difference: in scp, at least the source or
the destination is in a remote machine
• Example: uploading all the .txt files from your
current working directory to a remotehost
scp ./*.txt [email protected]:/home/username/folder

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila
Thanks
Shaun Aron & Sumir Panji

Introduction to Bioinformatics online course: IBT


Linux | Amel Ghouila

You might also like