Programmation Shell en
Programmation Shell en
Standard IO Redirection
• Shell programming
• Shell commands
• Bash variables
• Environment va r i a b l e s
• I/O redirection
Shell programming
Shell is a program that acts as a text-mode interface between the kernel and the
user. The shell is a command interpreter and programming language. The shell is a
text-mode interface with the keyboard as input and the screen as output.
What is a shell?
• the two environments available under Linux :
• console environment ;
• graphic environment.
52
Shell programming
Many Linux users prefer to work with the operating system via the command-line terminal,
but you can also use the graphical user interface (GUI) instead of the terminal.
A desktop environment is the user interface that takes the form of a desktop, with a menu
for running applications and switching between them.
One of the great things about Linux is the variety of desktop environments available
53
Shell programming
What is Shell programming?
• a mini-programming language integrated into Linux. It's not as complete a language
as C, C++ or Java, for example, but it can automate most of your tasks: backing up
data, monitoring the load on your machine, and so on.
• The great advantage of the shell language is that it is totally integrated into Linux:
there's nothing to install, nothing to compile. All the commands you use in shell
scripts are commands from the system you're about to access: ls, cd, grep, sort...
53
Shell programming
• The features offered by the command prompt may vary depending on
the shell you're using. Here are the names of some of the main shells
available.
• sh: Bourne Shell. The ancestor of all shells.
• bash: Bourne Again Shell. An improvement on the Bourne Shell, available by default
on Linux and macOS .
• ksh: Korn Shell. A powerful shell common on proprietary Unixes, but also available
in a free, bash-compatible version.
• csh: C Shell. A shell using a syntax close to that of the C language.
• tcsh: Tenex C Shell. C Shell enhancement.
• zsh: Z Shell. A fairly recent shell incorporating the best ideas from bash, ksh and
tcsh.
• Bash (Bourne Again Shell) is the default shell o n most
Linux distributions.
54
Shell programming(2)
The shell is thus an executable file responsible for interpreting commands,
transmitting them to the system and returning the result. There are several shells:
• Bash shell commands are executable files located in the "/bin" directory. The shell
"knows" that a filename entered at the command prompt is a "command"
because the file is marked as an executable (the file is a compiled program).
• Unix-type operating systems usually feature a shell. Originally, the default shell
was sh, which gave rise to numerous variants, including csh, extended to tcsh, or
ksh, or rc... But today bash, inspired by sh, ksh and csh, is the most widely used
shell, although there are other command interpreters, such as zsh and ash.
54
Shell programming
Installing a new shell
• If you want to use another shell, like ksh for example, you can use the
download as any :
chsh
• chsh stands for Change Shell. You'll need to specify /bin/ksh for ksh, /bin/sh for sh,
/bin/bash for bash, etc.
• printenv SHELL
55
Shell programming
Creating a Shell script
• To create a Shell script, you need to create a file with a text editor like vi:
vi test.sh
• The .sh file extension is given by convention to indicate that it's a shell script, but
it's not obligatory. We can call our script file 'test' for short.
• The first thing to do in a shell script is to indicate... which shell is being used.
#!/bin/bash
• The #! is called the sha-bang, (also written shebang: It tells the system
which script interpreter to use to execute the code contained in the file.
• ).
• /bin/bash can be replaced by /bin/sh if you want to code for sh, /bin/ksh
for ksh, etc.
56
Shell programming
• A Shell script must be launched via ./<filename> and you must be in the right directory or
type the absolute path to your file.
• To run the script from any directory without "./" in front, you need to move the
script to one of the PATH directories.
• The PATH is a system variable that indicates the location of executable programs on
your computer. If you type echo $PATH you will get a list of these directories
"special".
echo $PATH
• The PATH contains directories such as /bin, /usr/bin or /usr/local/bin.
57
Shell programming
Exercise 1:
• Create a 'test.sh' file with the vi editor:
vi test.sh
• Add the following content to 'test.sh'
• Pwd: to print the working directory:
#!/bin/bash
pwd
ls
• and move it to a PATH directory: (cp test.sh /bin) Add execution rights to the 'test.sh' file
Chmod+x test.sh
• Run script
./test.sh
58
Command prompt
• The shell initializes by reading its global configuration (in a file in the /etc/ directory), then by reading the user-specific
configuration (in a hidden file, whose name begins with a dot, located in the user's home directory, i.e.
/home/user_name/.configuration_file), then displays a command prompt as follows:
machine:/directory/current$
• The first part of the prompt tells you which user is currently logged in. The second part identifies the system host name
(localhost).
• The tilde ~ sign indicates that the current working directory is the current user's home directory.
• The dollar sign $ means that the current user is a standard user. A root user would be identified by a pound sign #.
• So, how do you configure, modify and customize the Linux shell prompt?
• By default, most Linux distributions display the host name and current working directory. You can easily customize
your prompt to display the information that's important to you.
• Variable PS1: display current bash shell prompt settings 59
•
Command prompt
PS1 is an environment variable that specifies the format of the command prompt displayed in the
terminal. It stands for “Prompt String 1” and it is used to customize the appearance of the prompt
• When connecting or launching a terminal emulator, the prompt appears:
• The prompt $ for a user and # for root.
username@machine ~ $
root@machine ~ #
• Modify command prompt
User_name@machine ~ $echo $PS1
User_name@machine ~ $PS1= [\u@\h \W \A] :\$
\u: user name.
\h: machine name.
\W : name of current folder.
\A: 24-hour format
59
Command prompt
Modifying the prompt is an easy task. Simply assign a new value to PS1 and press Enter:
59
Command syntax
• Bash command syntax
user@machine ~ $[ path/]cmd_name[ option ... ] [ argument ... ]
The UNIX commands are located in the /bin directory where they can
be called directly. 60
Shell commands types
• Obtaining information
• Handling files and directories
• Displaying file contents and character strings
• Network operations
• Performance control
• Batch processing
61
Commands : Obtaining information
• Here are some common shell commands for obtaining information:
• whoami: user name
• id: current user and its ID group
• uname: operating system name
• ps: running processes and their IDs
• top: running processes and resource consumption (memory, CPU, IO)
• df: information on mounted file systems
• man: shell command reference manual
• date: today's date
62
COMMANDS: File handling
• Here are some common shell commands for manipulating files:
• cp: copy a file
• mv: change the name or path of a file
• rm: delete a file
• touch: create an empty file, modify file timestamps
• chmod: change file permissions
• wc: count the number of characters, words and lines in a file
• grep: search for a string in a file
63
COMMANDS: File handling
-The touch command without any options creates a new file. If the file exists, the touch
command will update access and modification to the current time without changing its
contents.
-chmod o+r /home/user: - authorizes others (i.e. those who are neither the owner nor a
member of the owner group) to read the /home/user directory
-chmod a+rw /home/user/unfichier: - authorizes everyone to read and write the file
/home/user/unfichier
-For example, wc rose-tatoo.txt :15 109 503 rose-tatoo.txt: 15 is the number of lines, 109 is
the number of words, 503 is the number of characters
63
The cp command
• Synopsis
• cp [option] source destination
• Copy file1 to file2 in current directory
• #cp file1 file2
• Copy file1 to file2 in the /tmp directory
• #cp file1 /tmp/file2
• Copies the /tmp directory into the /opt directory
• #cp -r /tmp /opt
• Copies the file file1 to the /tmp directory
• #cp -i /tmp / file1
66
The grep command
• Searching with expressions :
Special character Meaning
. Any character(this is used to match any character)
^ Start of line
$ End of line
[] One of the characters between the square bracket
67
The grep command
• Find a string at the beginning of a line in a file
• #grep -E ^chaine file1
• Find an end-of-line string in a file
• #grep -E string$ file1
• Search for a string string or string in a file
• #grep -E [Cc]hate file1
• Display machine IP addresses !
• # ip a l | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}"
68
COMMANDS: Directory manipulation
• Here are some common shell commands for manipulating directories :
• ls: list files and directories
• find: search and find files
• pwd: returns the current directory
• mkdir: create a new directory
• cd: change directory
• rmdir: delete a directory
69
The ls command
• The ls command for listing directory contents
• Synopsis
• ls [options] [file...]
• List current directory #ls
• List all files, hidden files #ls -a
• ls --all
• Display file type, access permissions, number of physical links, owner and group
name , size in bytes, and timestamp
• #ls -l
70
Metacharacters (wildcards)
• lists files whose name begins with file
• #ls file*
• lists files with names ending in c #ls *c
• lists files containing t in their name #ls *t*
• lists files whose name begins with t with one character after #ls t?
• lists files whose name begins with t with two characters after #ls t??
Examples:
- ls t??
- ls [clst]*
71
The cd command
• The cd command to change directories :
• Synopsis
• cd [-L | -P] [directory]
• Go to the .usr/sbin directory
• cd /usr/sbin
• Go to parent directory #cd .
• Go to your personal directory
• #cd
• #cd ~
• #cd $HOME
72
commands: display contents
• Here are some common shell commands for displaying file contents and strings:
• cat: reads file contents and displays them on standard output
• more: displays a file page by page without going back.
• head: displays the first N lines of a file
• tail: displays the last N lines of a file
• echo: one of the most basic commands. The arguments passed to echo are
displayed on standard output
Head: Without any option, it displays only the first 10 lines of the file specified.
head -n 5 state.txt => 5 lines
echo -e "Geeks \nfor \nGeeks "
73
commands: network operations
• Network commands include :
• hostname: displays the system hostname
• ping: manages the state of network connectivity between a source and a device
(IP address) in a network.
• ifconfig: view and modify the configuration of your system's network interfaces.
• curl: displays the content of a page from its URL.
• wget: downloads a file from the Web.
74
BASH Variables
• As in all programming languages, bash includes what is known as variables. These allow us to temporarily
store information in memory.
• To assign a value to a variable, the syntax is -> variable=value
• Examples: # message='Hello everyone
# A=30
# variable1=5
• The scope of this variable is local to the Shell process that defined it.
• You must be very careful not to place spaces either before or after the equal sign when assigning in bash. For
example, the expression variable =value will give the error variable: not found, and variable=value will give
the error value: not found.
• Bash uses the dynamic typing mechanism, so there's no need to specify a type for a new variable.
• A variable name may contain numbers, letters and underlined symbols, but must not start
with a number
75
BASH Variables
Display a variable
• To display a variable, we'll use the echo command followed by the variable name
preceded by the dollar sign $.
• Examples:
# message='Hello everyone'
# echo $message
• If you want to insert line breaks, activate the -e parameter and use the \n symbol.
• Examples:
# echo -e "Message\nOther line"
Message
Other line
76
BASH Variables
Quotes
• You can use quotes to delimit a parameter containing spaces. There are three types of quotes:
• apostrophes ' ' (simple quotes): the variable is not analyzed and the $ is displayed as is;
# message='Hello everyone'
# echo 'The message is: $message'
The message is: $message
• double quotes: the variable is analyzed and its contents displayed;
# message='Hello everyone'
# echo "The message is: $message "
The message is: Hello everyone
• back quotes(accents graves), which are inserted with Alt Gr + 7 on a French AZERTY keyboard: the
back quotes tell bash to execute what's inside. ;
# message=`pwd`
# echo "You are in the $message folder "
You are in the /home/mateo21/bin folder 77
BASH Variables
Read a variable
• The read command is used to read a variable from the keyboard. With the - p, you can read , you can
read the value of a variable by displaying a prompt
message:
# read -p 'Enter your name: ' name
# echo "Hello $name!"
78
Environment variables
• At present, the variables you create in your bash scripts only exist in those scripts. An
environment variable is a variable accessible by all child processes of the current shell.
• You can display all those you currently have in memory with the command:
# env
• Examples of useful environment variables :
• USER: the current system user
• SHELL: indicates which type of shell is being used (sh, bash, ksh...) ;
• PATH: a list of directories containing executables you want to be able to run without specifying their
directory. We mentioned this earlier. If a program is located in one of these folders, you'll be able to
invoke it regardless of which folder you're in;
• EDITOR: the default text editor, which opens when required;
• HOME: the position of your home folder ;
79
Environment variables
Create an environment v a r i a b l e
• To create an environment variable, export the value of a variable with the export command.
# export variable
• To illustrate the difference between a shell variable and an environment variable, simply create
an environment variable, launch a child shell and display the variable (Example 1). If you try to
do the same thing without exporting the variable, the value will not be accessible (example 2).
# my_variable=toto #
$ my_variable=toto
export my_variable #
$ bash
bash
echo $my_variable
# echo $my_variable
80
Toto Example 1 Example 2
exit
#exit
Parameters variables
• Like all commands, your bash scripts can also accept parameters. For example, we could call our
script like this:
81
Parameter variables
For example:
• Create a variables.sh file containing the following Shell script:
#!/bin/bash
echo "You have run $0, there are $# parameters"
echo "Parameter 1 is $1"
• Running the script with three parameters gives the following result:
82
Environment variables
Exercise 2:
• Each user has a .bash_profile file, which can be used to define parameters specific to that user.
In this file we can modify the PATH, a welcome message,....
• Edit this file to change a few parameters, such as the PATH.
• # vi /home/dawm/.bash_profile
83
I/O redirection
• Each process has 3 standard flows that it uses to communicate with the user:
• the standard input named stdin (identifier 0): by default, this is the keyboard,
• the standard output named stdout (identifier 1): by default, this is the screen,
• the standard error output named stderr (identifier 2): by default, this is the screen.
• These flows can be redirected to that the process can interact with another instead of
interacting with the user
84
I/O redirection
Redirect standard output
• When a command is executed, the shell displays the result on the output console (the default
screen). You can redirect this output to a file using the > sign.
# ls>result_ls
• If the file already exists, it is overwritten. It is possible to add the output of a process to an
existing file by using the double >> sign (concatenation).
# ls>>result_ls
Complete syntax
• In fact, > signs can be preceded by the stream identifier to be redirected. For standard output,
the following syntax can be used:
# ls 1>result #
ls 1>>result
85
I/O redirection
Redirect standard error output
• Standard error output stream redirection uses the same signs, but preceded by the stream
identifier: 2.
# ls 2>errors_ls
# ls 2>>errors_ls
87
User and group
administration
• The root
• The su
• The sudo command
• The /etc/passwd file
• The /etc/shadow file
• The 'home' folder
• Group ID
• User management
• Group management