Unix Command Guide Planner Wise Part 1
Unix Command Guide Planner Wise Part 1
```bash
#!/bin/bash
```bash
#!/bin/bash
# Program Definition: This script performs basic arithmetic operations on two numbers.
sum=$((num1 + num2))
difference=$((num1 - num2))
product=$((num1 * num2))
division=$((num1 / num2))
3. An employee's basic salary is input through the keyboard, where DA is 40% of the basic
salary and HRA is 20% of the basic salary. Write a program to calculate the gross salary:
```bash
#!/bin/bash
4. Write a program to calculate the area and perimeter of the rectangle, and the area and
circumference of the circle:
```bash
#!/bin/bash
# Program Definition: This script calculates the area and perimeter of a rectangle,
# and the area and circumference of a circle.
# Rectangle
read -p "Enter the length of the rectangle: " length
read -p "Enter the width of the rectangle: " width
rectangle_area=$((length * width))
rectangle_perimeter=$((2 * (length + width)))
# Circle
read -p "Enter the radius of the circle: " radius
```bash
#!/bin/bash
# Program Definition: This script swaps two integer values.
6. Write a shell program to calculate the simple interest for which the principal amount,
the number of years, and the rate of interest are passed as arguments on the command line:
```bash
#!/bin/bash
principal=$1
years=$2
rate_of_interest=$3
7. Write a shell program that prints "Invalid no. of arguments" if more than 5 command-
line arguments are given, else print "Valid no. of arguments":
```bash
#!/bin/bash
8. Write a shell program to find the average of 3 numbers which are passed on command-
line:
```bash
#!/bin/bash
# Program Definition: This script calculates the average of three numbers passed as command-
line arguments.
num1=$1
num2=$2
num3=$3
9. Write a shell program that receives 5 words as command-line arguments and prints
them in reverse order:
```bash
#!/bin/bash
10. Write a script to determine whether a given year is a leap year or not:
```bash
#!/bin/bash
# Program Definition: This script checks whether a given year is a leap year or not.
11. A script that uses a conditional statement to check if a number is positive, negative, or
zero:
```bash
#!/bin/bash
# Program Definition: This script checks whether a number is positive, negative, or zero.
```bash
#!/bin/bash
13. A script that uses a conditional statement to check the greater number from two
numbers:
```bash
#!/bin/bash
# Program Definition: This script finds the greater number from two numbers.
14. The marks obtained by a student in 5 different subjects are input through the
keyboard. The student gets a division as per the following rules:
- Percentage above or equal to 70 à Distinction
- Percentage between 60 and 70 à First
- Percentage between 50 and 60 à Second
- Percentage between 40 and 50 à Third
```bash
#!/bin/bash
# Program Definition: This script calculates the division based on percentage marks.
: " s1
read -p "Enter marks in Subject 2: " s2
read -p "Enter marks in Subject 3: " s3
read -p "Enter marks in Subject 4: " s4
read -p "Enter marks in Subject 5: " s5
total_marks=$((s1 + s2 + s3 + s4 + s5))
percentage=$((total_marks / 5))
Example: To copy a file named file.txt to a directory /path/to/destination, you would use:
cp file.txt /path/to/destination
Example: To move a file named file.txt to a directory /path/to/destination, you would use:
mv file.txt /path/to/destination
To rename a file from oldname.txt to newname.txt, you would use:
mv oldname.txt newname.txt
rm file.txt
CMP (Compare): The cmp command is used to compare two files byte by byte. It helps identify
the first differing byte and the byte offset at which the difference occurs.
Example: To compare two files named file1.txt and file2.txt, you would use:
DIFF (Difference): The diff command is used to compare two files line by line. It displays the
lines that differ between the two files.
Example: To check the difference between two files named file1.txt and file2.txt, you would use:
Copy code
diff file1.txt file2.txt
#!/bin/bash
# Program Definition:
# This shell script takes two file names as input and uses the cmp command
# to compare the files and display the result.
file1="$1"
file2="$2"
if [ ! -e "$file2" ]; then
echo "Error: File '$file2' does not exist."
exit 1
fi
O/P
./compare_files.sh file1.txt file2.txt
#!/bin/bash
# Program Definition:
# This shell script takes two file names as input and uses the diff command
# to compare the files and display the differences.
file1="$1"
file2="$2"
if [ ! -e "$file2" ]; then
echo "Error: File '$file2' does not exist."
exit 1
fi
o/p
./compare_diff.sh file1.txt file2.txt