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

Shell_Scripting_Lab_06

Uploaded by

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

Shell_Scripting_Lab_06

Uploaded by

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

OPERATING SYSTEMS

CS F22- LAB 06

Pre-Requisite
chmod command:

The chmod command in Unix-like operating systems (Linux, macOS, etc.) is used to change the permissions
of a file or directory. Permissions determine who can read, write, or execute a file. Here's a breakdown of
how chmod works, including different types of commands:

Basic Structure:
chmod [options] mode file

 mode: Specifies the new permissions.


 file: The name of the file or directory whose permissions are being changed.

Permission Types

There are three basic types of permissions:

1. r (read): Permission to read the file.


2. w (write): Permission to modify or write to the file.
3. x (execute): Permission to execute the file (for scripts or programs).

User Classes

Permissions are set for three classes of users:

1. u (user): The file owner (or creator).


2. g (group): Users who are in the same group as the file.
3. o (others): All other users (not the owner or group members).
4. a (all): A shorthand for all three classes (user, group, and others).

Numeric Mode (Octal Notation)

Each permission is assigned a number:


 r=4
 w=2
 x=1

Permissions are represented as a three-digit number, with each digit specifying the permissions for user,
group, and others.

 Read (r) + Write (w) = 4 + 2 = 6


 Read (r) + Execute (x) = 4 + 1 = 5
 Write (w) + Execute (x) = 2 + 1 = 3
 Read (r) = 4
 Write (w) = 2
 Execute (x) = 1
 No permission = 0

For example:

 chmod 755 myscript.sh gives:


o User (owner) permissions: rwx (7 = 4 + 2 + 1)
o Group permissions: rx (5 = 4 + 1)
o Others permissions: rx (5 = 4 + 1)

Symbolic Mode

In symbolic mode, you use letters to specify permissions.

 +: Adds permissions.
 -: Removes permissions.
 =: Sets exact permissions, overwriting existing ones.

Common Commands:

1. Add execute permission to the owner:

chmod u+x myscript.sh

This gives the user (owner) execute permission.

2. Remove write permission from group:

chmod g-w myscript.sh

This removes write permission for the group.

3. Give read, write, and execute permissions to all users:

chmod a+rwx myscript.sh


This adds read, write, and execute permissions to all users (user, group, and others).

4. Remove execute permission from others:

chmod o-x myscript.sh

This removes execute permission for others.

5. Set permissions exactly (overwrites any previous permissions):

chmod u+x,g-w,o=r myscript.sh

This command:

o Adds execute permission for the owner.


o Removes write permission for the group.
o Sets read-only permission for others.

Example:
1. Granting Execute Permission for Everyone

Command:

chmod +x myscript.sh

Explanation: This command adds execute (x) permission for everyone (user, group, and others).

2. Granting Read, Write, and Execute Permissions to the Owner, and Read-Only to Group and Others

Command:

chmod 755 myscript.sh

Explanation: This command sets the file permissions to:

 Owner: read, write, and execute (rwx = 7)


 Group: read and execute (rx = 5)
 Others: read and execute (rx = 5)

3. Setting Exact Permissions for User, Group, and Others

Command:

chmod u+x,g-w,o=r myscript.sh


Explanation: This command:

 Owner (user): Adds execute permission (+x).


 Group: Removes write permission (-w).
 Others: Sets only read permission (=r).

4. Granting Read, Write Permissions for Owner and Group, and No Permission for Others

Command:

chmod 660 myscript.sh

Explanation: This command sets the file permissions to:

 Owner: read and write (rw- = 6)


 Group: read and write (rw- = 6)
 Others: no permissions (--- = 0)

5. Removing Write Permission from the Group

Command:

chmod g-w myscript.sh

Explanation: This command removes the write (w) permission for the group.

6. Adding Execute Permission Only for the Owner

Command:

chmod u+x myscript.sh

Explanation: This command adds execute (x) permission only for the owner.

7. Using Symbolic Mode to Set Permissions Explicitly

Command:

chmod u=rwx,g=rx,o=r myscript.sh

Explanation: This command sets:

 Owner: rwx (read, write, execute).


 Group: rx (read, execute).
 Others: r (read).
Introduction to Shell and Shell Scripting
What is a Shell? A shell is a program that acts as an interface between the user and the operating system. It
accepts user commands in a readable format and translates them into machine code for execution.

Types of Shells:

1. Bourne Shell (sh)


2. C Shell (csh)
3. Korn Shell (ksh)
4. Bash Shell (bash)
5. Tcsh Shell
Environment Preparation
Ensure Shell Access:
 Use a terminal or command-line interface.
 Confirm a shell (like Bash) is available by typing:

echo $SHELL
or
echo $0

This displays your current shell.


How to Check Your available Shell:

 To find available shells on your system, use the command:

$ cat /etc/shells

How to Change Your Default Shell:

 To change the default shell:

$ chsh username new_default_shell


The administrator can change your
default shell.

What is Shell Scripting?

A shell script is a series of commands stored in a plain text file. It is used to automate tasks that can be
performed manually in a shell.

Why Use Shell Scripting?

 Automate repetitive tasks.


 System monitoring and management.
 Efficient data processing.

Steps to Create and Run a Shell Script:

1. Create the Script:


o Use a text editor to write the script, e.g., nano myscript.sh.
o Save the file with a .sh extension.
2. Make the Script Executable:
o Provide executable permissions:

$ chmod +x myscript.sh
3. Run the Script:
o Execute using one of the following commands:

$ ./myscript.sh
$ bash myscript.sh

Example Script:

#!/bin/bash:

 This is called the shebang or hashbang.


 It specifies the interpreter to be used for executing the script. In this case, the script uses the Bash
shell (/bin/bash) as the interpreter.
 The presence of this line ensures the script will be executed using Bash regardless of the default shell
on your system.

#!/bin/bash
echo "Hello, World!"

Steps to Create and Run the Script Using vi


1. Open vi to Create the Script

 Open the terminal and type the following command to create a new file:

nano hello_world.sh

2. Enter Insert Mode

 Press i to switch to Insert Mode in the nano editor.

3. Write the Script

 Type the following code:

4. Save and Exit

 To save and exit the file in nano:


1. Press Esc to exit Insert Mode.
2. Type :wq and press Enter (this saves and quits the editor).

Make the Script Executable

 Provide executable permissions for the script:

chmod +x hello_world.sh

Run the Script

 Execute the script:

./hello_world.sh

Operators in Shell Scripting:


Number Comparison:
Steps to Run the Script:

 Save the script as compare_numbers.sh.

 Make the script executable:

chmod +x compare_numbers.sh

 Run the script:

./compare_numbers.sh
Variables in Shell Scripting:
 System Variables: Predefined variables by the OS (e.g., PATH, HOME, USER).
 User-Defined Variables: Created by users.

Syntax:

variable_name=value
Example:

name="John"
echo $name

Arithmetic Operations:
Syntax:

expr operand1 operator operand2

Examples:

$ expr 5 + 3
$ expr 10 \* 2

Save and Exit

 To save and exit:


1. Press Esc to exit Insert Mode.
2. Type :wq and press Enter.
Make the Script Executable

 Provide executable permissions for the script:

chmod +x variables_and_arithmetic.sh

Run the Script

 Execute the script:

./variables_and_arithmetic.sh

Control Statements
If-Else Statements:
Syntax:

if [ condition ]
then
commands
else
commands
fi

Example:
Loops:
1. For Loop: Syntax:

for var in list


do
commands
done

Example:

2. While Loop: Syntax:


while [ condition ]
do
commands
done

Example:

Case Statement:
Syntax:

case variable in
pattern1) commands ;;
pattern2) commands ;;
*) default commands ;;
esac

Example:
Practice Questions
1. Write a script to print "Hello, World!" on the screen.
2. Write a script to calculate the sum of two numbers provided by the user.
3. Write a script to check if a given number is even or odd.
4. Create a script to display the first 10 natural numbers using a loop.
5. Write a script to print a multiplication table for a given number.
6. Write a script to display all logged-in users.
7. Create a script to back up a directory.

Using Exit Codes:


 Every command returns an exit code. 0 means success, and non-zero means failure.
 To check the exit status of a command:

$ echo $?

Functions in Shell Scripting:


Syntax:

function_name() {
commands
}
Example:

Best Practices
1. Always start scripts with #!/bin/bash.
2. Use comments (#) to explain code.
3. Follow consistent indentation for readability.
4. Test scripts on different shell environments for compatibility.

You might also like