0% found this document useful (0 votes)
27 views12 pages

Unix Command Guide Planner Wise Part 1

Uploaded by

doxik44036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

Unix Command Guide Planner Wise Part 1

Uploaded by

doxik44036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Command Explanation Sample Example

Who Displays a list of who $ who


users currently user1 pts/0 2023-07-24
logged into the 10:00
system. user2 pts/1 2023-07-24
11:30
PS Provides a snapshot ps $ ps
of currently running PID TTY TIME CMD
processes. 123 pts/0 00:02:30 bash
MAN Shows the manual man ls $ man ls
page of a specified
command.
PWD Displays the current pwd $ pwd
working directory. /home/user/documents
Uname Prints system uname -a $ uname –a
information, such as Linux example-PC 5.4.0-
the system name, 88-generic #99-Ubuntu
kernel version, etc. SMP
Echo Displays a message echo "Hello, World!" $ echo "Hello, World!"
or the value of a Hello, World!
variable.
Print **Note: "Print" is echo "Printing using echo" $ echo "Printing using
not a standard echo"
terminal command Printing using echo
in Unix/Linux. It
might refer to
"echo" or custom
script.
Tput Clear Clears the terminal tput clear $ tput clear
screen.
Exit Closes the current exit $ exit
terminal session or
script.
Make Shell Creates a shell vi my_script.sh<br>(Inside VI $ vi my_script.sh
Script using script file using the editor)<br>#!/bin/bash<br>echo (Inside VI editor)
VI VI text editor. "Hello, this is my script!"<br>(Save Press i to enter INSERT
and exit VI) mode)
Command Explanation Sample Example
#!/bin/bash
echo "Hello, this is my
script!"
(Press Esc to exit INSERT
mode)
(Type :wq and press
Enter to save and exit VI)
CD Changes the current cd /path/to/directory $ cd
directory. /home/user/documents
MKDIR Creates a new mkdir new_directory $ mkdir new_directory
directory.
RMDIR Removes a rmdir empty_directory $ rmdir
directory (only if it's empty_directory
empty).
CAT Concatenates and cat file.txt $ cat file.txt<br>This is
displays the content the content of the file.
of a file.
LS Lists the files and ls $ ls
directories in the file1.txt file2.txt
current directory. directory1
EXPR Evaluates expr 5 + 3 $ expr 5 + 3
expressions and 8
performs
operations.
DATE Prints or sets the date $ date
system date and Sun Jul 24 12:30:00
time. UTC 2023
CAL Displays a calendar cal 07 2023 $ cal 07 2023
for a specific month
or year.
BC A calculator that bc<br>(Inside BC)<br>5 + 3<br>(Ctrl $ bc (Inside BC)
performs arithmetic + D to exit BC) 5+3
operations. (Ctrl + D to exit BC)
1. A simple program that prints "Hello, World!" to the console:

```bash
#!/bin/bash

# Program Definition: This script prints "Hello, World!" to the console.

echo "Hello, World!"


```

2. A script that performs basic arithmetic operations:

```bash
#!/bin/bash

# Program Definition: This script performs basic arithmetic operations on two numbers.

read -p "Enter the first number: " num1


read -p "Enter the second number: " num2

sum=$((num1 + num2))
difference=$((num1 - num2))
product=$((num1 * num2))
division=$((num1 / num2))

echo "Sum: $sum"


echo "Difference: $difference"
echo "Product: $product"
echo "Division: $division"
```

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

# Program Definition: This script calculates the gross salary of an employee.

read -p "Enter the basic salary: " basic_salary


DA=$((basic_salary * 40 / 100))
HRA=$((basic_salary * 20 / 100))
gross_salary=$((basic_salary + DA + HRA))

echo "Gross Salary: $gross_salary"


```

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

circle_area=$(echo "scale=2; 3.14 * $radius * $radius" | bc)


circle_circumference=$(echo "scale=2; 2 * 3.14 * $radius" | bc)

echo "Rectangle Area: $rectangle_area"


echo "Rectangle Perimeter: $rectangle_perimeter"
echo "Circle Area: $circle_area"
echo "Circle Circumference: $circle_circumference"
```

5. Write a script to swap two integer values:

```bash
#!/bin/bash
# Program Definition: This script swaps two integer values.

read -p "Enter the first number: " num1


read -p "Enter the second number: " num2

echo "Before swapping: num1 = $num1, num2 = $num2"

# Swapping using a temporary variable


temp=$num1
num1=$num2
num2=$temp

echo "After swapping: num1 = $num1, num2 = $num2"


```

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

# Program Definition: This script calculates the simple interest.

principal=$1
years=$2
rate_of_interest=$3

simple_interest=$((principal * years * rate_of_interest / 100))

echo "Simple Interest: $simple_interest"


```

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

# Program Definition: This script checks the number of command-line arguments.


if [ "$#" -gt 5 ]; then
echo "Invalid no. of arguments"
else
echo "Valid no. of arguments"
fi
```

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

average=$(( (num1 + num2 + num3) / 3 ))

echo "Average: $average"


```

9. Write a shell program that receives 5 words as command-line arguments and prints
them in reverse order:

```bash
#!/bin/bash

# Program Definition: This script prints 5 words in reverse order.

if [ "$#" -ne 5 ]; then


echo "Error: Please provide exactly 5 words as command-line arguments."
exit 1
fi

echo "Reversed words:"


for ((i=$#; i>0; i--)); do
echo "${!i}"
done
```

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.

read -p "Enter a year: " year

if (( year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) )); then


echo "$year is a leap year."
else
echo "$year is not a leap year."
fi
```

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.

read -p "Enter a number: " num

if (( num > 0 )); then


echo "Positive number"
elif (( num < 0 )); then
echo "Negative number"
else
echo "Zero"
fi
```
12. A script that uses a conditional statement to check if a number is even or odd:

```bash
#!/bin/bash

# Program Definition: This script checks whether a number is even or odd.

read -p "Enter a number: " num

if (( num % 2 == 0 )); then


echo "Even number"
else
echo "Odd number"
fi
```

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.

read -p "Enter the first number: " num1


read -p "Enter the second number: " num2

if (( num1 > num2 )); then


echo "The greater number is $num1"
elif (( num1 < num2 )); then
echo "The greater number is $num2"
else
echo "Both numbers are equal"
fi
```

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.

read -p "Enter marks in Subject 1

: " 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))

echo "Percentage: $percentage"

if (( percentage >= 70 )); then


echo "Division: Distinction"
elif (( percentage >= 60 && percentage < 70 )); then
echo "Division: First"
elif (( percentage >= 50 && percentage < 60 )); then
echo "Division: Second"
elif (( percentage >= 40 && percentage < 50 )); then
echo "Division: Third"
else
echo "Division: Fail"
fi
```
CP (Copy): The cp command is used to copy files or directories from one location to another. It
creates a copy of the specified source file or directory and places it in the destination directory.

Example: To copy a file named file.txt to a directory /path/to/destination, you would use:

cp file.txt /path/to/destination

MV (Move/Rename): The mv command is used to move or rename files or directories. When


used for moving, it transfers the specified file or directory from the source location to the
destination location. When used for renaming, it changes the name of the file or directory.

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 (Remove/Delete): The rm command is used to remove or delete files or directories. Be


cautious while using this command as deleted files cannot be easily recovered.

Example: To delete a file named file.txt, you would use:

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:

cmp file1.txt file2.txt

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.

# Check if two arguments (file names) are provided


if [ $# -ne 2 ]; then
echo "Usage: $0 <file1> <file2>"
exit 1
fi

file1="$1"
file2="$2"

# Check if the files exist


if [ ! -e "$file1" ]; then
echo "Error: File '$file1' does not exist."
exit 1
fi

if [ ! -e "$file2" ]; then
echo "Error: File '$file2' does not exist."
exit 1
fi

# Use cmp command to compare the files


cmp -s "$file1" "$file2"

# Check the exit status of cmp command


if [ $? -eq 0 ]; then
echo "The files '$file1' and '$file2' are identical."
else
echo "The files '$file1' and '$file2' are different."
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.

# Check if two arguments (file names) are provided


if [ $# -ne 2 ]; then
echo "Usage: $0 <file1> <file2>"
exit 1
fi

file1="$1"
file2="$2"

# Check if the files exist


if [ ! -e "$file1" ]; then
echo "Error: File '$file1' does not exist."
exit 1
fi

if [ ! -e "$file2" ]; then
echo "Error: File '$file2' does not exist."
exit 1
fi

# Use diff command to compare the files and display differences


diff "$file1" "$file2"

o/p
./compare_diff.sh file1.txt file2.txt

You might also like