Shell_Scripting_Lab_06
Shell_Scripting_Lab_06
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
Permission Types
User Classes
Permissions are represented as a three-digit number, with each digit specifying the permissions for user,
group, and others.
For example:
Symbolic Mode
+: Adds permissions.
-: Removes permissions.
=: Sets exact permissions, overwriting existing ones.
Common Commands:
This command:
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:
Command:
4. Granting Read, Write Permissions for Owner and Group, and No Permission for Others
Command:
Command:
Explanation: This command removes the write (w) permission for the group.
Command:
Explanation: This command adds execute (x) permission only for the owner.
Command:
Types of Shells:
echo $SHELL
or
echo $0
$ cat /etc/shells
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.
$ 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:
#!/bin/bash
echo "Hello, World!"
Open the terminal and type the following command to create a new file:
nano hello_world.sh
chmod +x hello_world.sh
./hello_world.sh
chmod +x compare_numbers.sh
./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:
Examples:
$ expr 5 + 3
$ expr 10 \* 2
chmod +x variables_and_arithmetic.sh
./variables_and_arithmetic.sh
Control Statements
If-Else Statements:
Syntax:
if [ condition ]
then
commands
else
commands
fi
Example:
Loops:
1. For Loop: Syntax:
Example:
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.
$ echo $?
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.