0% found this document useful (0 votes)
22 views

Section 6

A shell is a program that provides an interface between the user and the operating system. It executes commands like ls, cd, cat using low-level OS functions. Common shells include sh, bash, csh. Bash shell offers features like command history, tab autocompletion, shortcuts. Shell scripts allow running multiple commands by storing them in a file with .sh extension. Variables and arithmetic operations can be used in shell scripts. The .bashrc file stores variables and functions permanently to customize the shell environment.

Uploaded by

pspc139
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Section 6

A shell is a program that provides an interface between the user and the operating system. It executes commands like ls, cd, cat using low-level OS functions. Common shells include sh, bash, csh. Bash shell offers features like command history, tab autocompletion, shortcuts. Shell scripts allow running multiple commands by storing them in a file with .sh extension. Variables and arithmetic operations can be used in shell scripts. The .bashrc file stores variables and functions permanently to customize the shell environment.

Uploaded by

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

Section #6

1​ ​Shells
Shell​: A program responsible for executing commands like (cd, ls, cat) using
some “ ​low-level​ ” functions of the operating system.

So ​Instead of ​writing many many lines to do something simple like “listing


a directory contents” .. you just write one command → “ ls “

The shell then takes this “ ls “ command and uses some low-level functions
that the OS provides to execute the command.

Different shells exist


1. sh ⇒ the first shell ever created.
2. bash ⇒ (​B​ourne ​a​gain ​sh​ell) has some improvements
over the traditional shell.
3. csh ⇒ (C shell) a shell with a ​syntax​ close to the C
language syntax.
4. ksh​, ​tcsh​, and many other shells..
The main differences among these shells are the ​syntax ​and the ​features
each shell provide, for example, the bash shell offers you a ​history of the
commands you used before, auto-completion of your commands and file
names by hitting the ​tab key​, as well as ​multiple useful shortcuts​.

Some examples of the shortcuts provided:


1. ctrl + L ⇒ clears the command screen.
2. ctrl + U ⇒ clears the line.
3. ctrl + C ⇒ cancels currently running
process.
4. ctrl + R ⇒ allows you to search the
previously used commands

==> To find all of the available shells on your system, print the contents of
the file “​/etc/shells​” using the “​cat​” command.

[ahmad@localhost /home]$ ​cat /etc/shells


Returns something like
/bin/sh
/bin/bash
/bin/tcsh
/bin/csh
==> To use a specific shell ​temporarily​, write the full path of the shell.

[ahmad@localhost /home]$ ​/bin/sh

The previous command changes the current shell to ​sh​ ​temporarily​.

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).

[ahmad@localhost /home]$ ​chsh


Changing shell for ahmad.
New shell [/bin/bash]: ​/bin/sh
Password:
Shell changed.

It will then ask you to ​write the path to the new shell​, and afterwards it will
ask you for the ​user password​.

==> To find the name of the current shell

[ahmad@localhost /home]$ ​echo​ $SHELL

Notice the ​$​ and that the word “​SHELL​” is all in CAPS.
2​ ​Bash Shell ​programming
2.1 Variables

Local​ variables

declared ONLY during your current


session.
Define a variable by writing the ​variable name followed by an
equals sign​ followed by the ​value ​(integers, strings).

[ahmad@localhost /home]$ a=hello-world


[ahmad@localhost /home]$ b=”Hello World”
[ahmad@localhost /home]$ c=19

In the 3 previous commands we declared 3 variables


1) a​ ==> type (string). Notice we ​don’t need to add double
quotes​ as long as the value consists of ​one
word​ ​with n
​ o spaces​.
2) b​ ==> type (string). ​Double quotes​ were added because the
value consists of ​two words​ separated by
a ​space​.

3) c​ ==> type (integer)

Print the value of a variable using the ​echo command followed by a


dollar sign​ followed by the ​variable name​.

[ahmad@localhost /home]$ echo $a


hello-world
[ahmad@localhost /home]$ echo $b
Hello World
[ahmad@localhost /home]$ echo $c
19
ENVIRONMENT​ variables
Variables declared by the Operating system that may
be edited later by the user.

PATH a colon-separated list of directories in echo $PATH


which the shell looks for commands.

SHELL current Shell echo $SHELL

PS1 prompt settings echo $PS1

to print all the environment variables

[ahmad@localhost /home]$ printenv

2.2 Arithmetic Operations


Arithmetic operations should be wrapped inside of double
parentheses

(( arithmetic expression ))
Example ==> (( a + b ))

To ​assign the result to a variable a dollar sign should be added before the
parentheses

Example ==> c=$(( a + b ))


[ahmad@localhost /home]$ a=3
[ahmad@localhost /home]$ b=6
[ahmad@localhost /home]$ c=$(( a + b ))
[ahmad@localhost /home]$ echo $c
9
Allowed operations
Operation Operator
Exponentiation a ​**​ b
Multiplication a ​*​ b
Division a​ /​ b
Modulo a ​%​ b
Addition a ​+​ b
Subtraction a ​-​ b

==> Notice the ​increment ​and ​decrement ​operators are also available.

[ahmad@localhost /home]$ a=3


[ahmad@localhost /home]$ echo $a
3
[ahmad@localhost /home]$ (( a++ ))
[ahmad@localhost /home]$ echo $a
4
[ahmad@localhost /home]$ (( a-- ))
[ahmad@localhost /home]$ echo $a
3

==> Notice that floating point math is ​not available


[ahmad@localhost /home]$ d=$(( 1/3 ))
[ahmad@localhost /home]$ echo $d
0

3​ ​Bash Shell ​Scripting


Using the bash shell, variables, and commands can be only written ​one at a
time​.In order to get over this limitation, we use a ​script​.

Script​: A file containing multiple commands, variables,... etc.

Before we start we need to install a Text


Editor called ( ​nano​ )
log in as a “ ​root​ ” THEN
[root@localhost /home]# yum install nano

Steps of creating and running a bash shell script


1. Create a new file ending with the extension (.sh)

[ahmad@localhost /home]$ ​touch​ first_script.sh

2. Open your file using an editor of your choice ( here we’re using ​nano​ )

[ahmad@localhost /home]$ ​nano​ first_script.sh

3. Add ( ​#!/bin/bash​ ) to the first line of your file.

4. Move to the next line and add some commands.

#!/bin/bash

file_name=”Ahmad”

touch ​$​file_name

find /usr/share/doc -iname “*.pdf” -exec cp { } /home/ahmad \;


Save →​ ctrl + o
Exit → ​ctrl + x

5. Run ( ​bash first_script.sh​ ), assuming the script name is


“first_script.sh”

6. To make sure everything worked just fine .. run

[ahmad@localhost /home]$ ​ls

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​ )

[ahmad@localhost /home]$ ​chmod​ ​u+x​ first_script.sh

[ahmad@localhost /home]$ ​./​first_script.sh

bashrc​ file
a bash script that is used to hold your variables,
and functions ​permanently

System wide version → ​/etc/bashrc

Runs each time you login with any username

User specific version → ​/home/ahmad/.bashrc


{{ replace ​ahmad​ with your username }}

Runs each time you create a new session

==> Adding a welcome message to the user Ahmad

[ahmad@localhost /]$ ​cd ~


[ahmad@localhost /home]$ ​nano ​.bashrc
Move to the last line of the file

# .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

# Add the following Line


echo “ Hello ”$USER

save ( ctrl + O ) THEN exit ( ctrl + x )

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.

2. ​edit​ the “/etc/bashrc” file to ​add a welcoming


message​ and try multiple users to login and make
sure the message is displayed to everyone

You might also like