You are on page 1of 4

LAB 3 – Shell Programing

Shell programming refers to writing scripts or programs that interact with the shell of an
operating system. The shell is a command-line interface that allows users to interact with the
operating system by typing commands.
In a shell program, you can automate tasks, execute commands, manipulate files and
directories, and perform various system operations. These scripts are written using a scripting
language that is interpreted by the shell.
To understand shell programming better, you need to learn the syntax and features of the
shell scripting language used in your lab content. Commonly used shells include:
1. Bash (Bourne Again Shell): This is the default shell on most Linux distributions and
macOS. It's a powerful shell with extensive scripting capabilities.
2. Shell scripting on Windows: On Windows systems, you can use the Command
Prompt (cmd.exe) or PowerShell for shell scripting. Each has its own syntax and
features.
Here are some basic concepts and tasks you might encounter in shell programming:
1. Variables: You can define variables to store values like file paths, user input, or
command output.
2. Commands: You can execute system commands directly from the script.
3. Control structures: Like any programming language, shell scripting supports control
structures such as loops and conditional statements.
4. Functions: You can define functions to encapsulate reusable code blocks.
5. File manipulation: You can create, read, write, and delete files and directories.
6. Error handling: Shell scripts can handle errors gracefully by checking return codes
and reacting accordingly.
Here are some lab exercises related to shell programming that you can perform in the Cygwin
environment
1. Basic Script Creation:
 Create a shell script called hello.sh that prints "Hello, World!" when executed.
echo "Hello, World!"
 Save this code into a file named hello.sh. Then, make the file executable using the chmod
command: chmod +x hello.sh
 Finally, execute the script: ./hello.sh
 You should see the output Hello, World! printed to the terminal.

1|P ag e
2. Variables and User Input:
 Write a script that asks the user for their name and then prints a greeting using that name.
echo "What is your name?"
read name
echo "Hello, $name!"
 This script prompts the user to enter their name, stores it in the variable name, and then
prints a greeting message using that name.
3. File Operations:
 Write a script that checks if a file named example.txt exists in the current directory.
if [ -e "example.txt" ]; then
echo "File exists"
else
echo "File does not exist"
fi
 Convert the file into unix line ending first using dos2unix
 This script uses the -e option with the test command to check if the file exists. If it does, it
prints "File exists"; otherwise, it prints "File does not exist".
4. Conditional Statements:
 Write a script that takes a number as input from the user and checks if it is even or odd.
echo "Enter a number:"
read number
if ((number % 2 == 0)); then
echo "Even"
else
echo "Odd"
fi
 This script uses the % operator to calculate the remainder when dividing by 2. If the
remainder is 0, the number is even; otherwise, it's odd.
5. Looping Constructs:
 Write a script that prints numbers from 1 to 10 using a loop.
for ((i = 1; i <= 10; i++)); do
echo $i
done

2|P ag e
 This script uses a for loop to iterate from 1 to 10 and prints each number on a separate
line.
6. Command Line Arguments:
 Write a script called greet.sh that takes a name as a command-line argument and prints a
personalized greeting.
name=$1
echo "Hello, $name!"
 Save this code into a file named greet.sh. Make it executable and run it with a name as an
argument:
chmod +x greet.sh
./greet.sh John
 This will print "Hello, John!" to the terminal.
7. String Manipulation:
 Write a script that takes a string as input from the user and prints its length.
echo "Enter a string:"
read string
length=${#string}
echo "Length of the string: $length"
 This script uses the ${#variable} syntax to get the length of the string stored in the
variable string.
8. Function Definitions:
 Write a script that defines a function called multiply which takes two numbers as
arguments and returns their product.
multiply() {
echo $(($1 * $2))
}
result=$(multiply 5 3)
echo "Result: $result"
 This script defines a function multiply that takes two arguments and returns their product.
It then calls the function with arguments 5 and 3 and stores the result in the variable
result.
9. Error Handling:
 Write a script that attempts to remove a file named nonexistent.txt.
file="nonexistent.txt"

3|P ag e
if [ -e "$file" ]; then
rm "$file"
echo "File removed"
else
echo "File does not exist"
fi
 This script first checks if the file exists using the -e option with the test command. If it
exists, it removes the file using the rm command; otherwise, it prints "File does not exist".
10. Advanced File Operations:
 Write a script that lists all files in the current directory and their sizes (in kilobytes),
sorted by size in descending order.
# List files and sizes in kilobytes, sorted by size in descending order
ls -l | awk '{printf "%.0fK %s\n", $5/1024, $9}' | sort –nr
 This script uses ls -l to list files along with their sizes, awk to extract the size and file
name, and sort -nr to sort by size in descending order.

Check File Encoding:


 Ensure that the script file (file.sh) is saved with Unix line endings (LF) rather than
Windows line endings (CRLF). Incorrect line endings can sometimes cause syntax errors.
You can use the file command to check the file type and encoding:
 file file.sh
 If the file is identified as a text file with DOS line endings, you can convert it to Unix line
endings using tools like dos2unix.
 If not installed, install it from the package.
Conversion:
 Once dos2unix is installed, you can use it to convert a file from DOS/Windows format to
Unix/Linux format. Navigate to the directory containing your script file (file.sh) in the
Cygwin terminal and run:
 dos2unix file.sh (file.sh is your file)

4|P ag e

You might also like