Section 6
Section 6
1 Shells
Shell: A program responsible for executing commands like (cd, ls, cat) using
some “ low-level ” functions of the operating system.
The shell then takes this “ ls “ command and uses some low-level functions
that the OS provides to execute the command.
==> To find all of the available shells on your system, print the contents of
the file “/etc/shells” using the “cat” command.
it opens a new SESSION with the sh shell .. Notice we did NOT exit the
previous session.
it’s like when you’re using Microsoft Word and you create a new file .. the old
file is still there and will keep being there until you close it yourself.
==> To go back to the previous shell, just write “exit” or hit ( ctrl + D ).
[ahmad@localhost /home]$ exit
The previous command returns to the previous shell (bash shell in our
case).
==> To use a specific shell permanently, write use the command “chsh”
(short for: change shell).
It will then ask you to write the path to the new shell, and afterwards it will
ask you for the user password.
Notice the $ and that the word “SHELL” is all in CAPS.
2 Bash Shell programming
2.1 Variables
Local variables
(( arithmetic expression ))
Example ==> (( a + b ))
To assign the result to a variable a dollar sign should be added before the
parentheses
==> Notice the increment and decrement operators are also available.
2. Open your file using an editor of your choice ( here we’re using nano )
#!/bin/bash
file_name=”Ahmad”
touch $file_name
7. Another way to run your script is to add an execute permission to the
script and then running it as follows ( ./first_script.sh )
bashrc file
a bash script that is used to hold your variables,
and functions permanently
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't ..
# export SYSTEMD_PAGER=
# User specific aliases and functions
logout and log in again you’ll see a welcoming message “ Hello Ahmad ”.
Assignments
1. Write a shell script that reads some input from
the user and prints it back to the user again.