Linux Important Questions Answer:
1) Linux Commands with Syntax and Example
Commands: `echo`, `rm`, `cp`, `mv`, `wc`, `split`
- `echo`: Used to display a line of text or variable value.
- Syntax: `echo [options] [string]`
- Example: `echo "Hello, World!"`
- `rm`: Removes files or directories.
- Syntax: `rm [options] [file/directory]`
- Example: `rm file.txt`
- `cp`: Copies files or directories.
- Syntax: `cp [options] source destination`
- Example: `cp file1.txt file2.txt`
- `mv`: Moves or renames files and directories.
- Syntax: `mv [options] source destination`
- Example: `mv oldname.txt newname.txt`
- `wc`: Counts lines, words, and characters in a file.
- Syntax: `wc [options] [file]`
- Example: `wc -l file.txt`
- `split`: Splits a file into pieces.
- Syntax: `split [options] [file] [prefix]`
- Example: `split -l 10 file.txt`
2) Arithmetic Calculator Using Case Statement and Without Case Statement
- Using Case Statement:
```bash
#!/bin/bash
echo "Enter two numbers:"
read a b
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read choice
case $choice in
1) result=$((a + b));;
2) result=$((a - b));;
3) result=$((a * b));;
4) result=$((a / b));;
*) echo "Invalid Choice";;
esac
echo "Result: $result"
```
-Without Using Case Statement:
```bash
#!/bin/bash
echo "Enter two numbers:"
read a b
echo "Choose operation (+, -, *, /):"
read op
if [ "$op" == "+" ]; then
result=$((a + b))
elif [ "$op" == "-" ]; then
result=$((a - b))
elif [ "$op" == "*" ]; then
result=$((a * b))
elif [ "$op" == "/" ]; then
result=$((a / b))
else
echo "Invalid Operation"
fi
echo "Result: $result"
```
3) Swapping of Two Numbers With and Without Using Third Variable
- Using Third Variable:
```bash
#!/bin/bash
echo "Enter two numbers:"
read a b
temp=$a
a=$b
b=$temp
echo "After swapping: a = $a, b = $b"
```
- Without Using Third Variable:
```bash
#!/bin/bash
echo "Enter two numbers:"
read a b
a=$((a + b))
b=$((a - b))
a=$((a - b))
echo "After swapping: a = $a, b = $b"
```
4) `sort` and `uniq` Command
- `sort`: Sorts lines of text files.
- Syntax: `sort [options] [file]`
- Example: `sort file.txt`
- `uniq`: Removes or filters out repeated lines.
- Syntax: `uniq [options] [file]`
- Example: `sort file.txt | uniq`
5) Redirection and Pipes with Explanation
- Redirection: Used to redirect the output/input of commands.
- Example: `command > file.txt` (Redirects output to file).
- `>`, `>>`, `<`, `2>`, `2>&1` are common operators.
- **Pipes**: Used to pass the output of one command as input to another.
- Example: `ls -l | grep ".txt"`
6) Concatenate Command `cat` with All Operations
- **`cat`**: Concatenates files and prints on the standard output.
- View File: `cat file.txt`
- Combine Files: `cat file1.txt file2.txt > combined.txt`
- Line Numbers: `cat -n file.txt`
7) `tr` (Translate) Command with Detailed Explanation
- **`tr`**: Translates or deletes characters.
- Syntax: `tr [options] SET1 [SET2]`
- Example: `echo "hello" | tr 'a-z' 'A-Z'` (Transforms lowercase to uppercase).
8) Random Number Generation
- Example:
```bash
#!/bin/bash
echo $((RANDOM))
```
9) Finding Files
- Example: `find /path -name filename`
10) Basic File Finding Commands
- Example: `locate filename`, `which command`, `whereis command`
11) `xargs` Arguments
- `xargs`: Converts input into arguments for a command.
- Example: `find . -name "*.txt" | xargs rm`
12) Cryptography Tools - Checksum Generation (The `sha256sum` Command)
- Example:
```bash
sha256sum file.txt
```
13) Operations Using `dd` Command, Intersection, and Difference
- `dd`: Copies and converts files.
- Example: `dd if=/dev/zero of=output.img bs=1M count=100`
Compulsory Assignment Questions
1) Explain and Write Shell Scripts for File Commands
- Commands: `rm`, `cp`, `cat`, `mv`, `cmp`, `wc`, `split`, `diff`
- Example:
```bash
# Script to demonstrate rm
rm file.txt
```
2) Write Shell Scripts to Show System Configuration
```bash
#!/bin/bash
echo "Logged User: $USER"
echo "Log Name: $(logname)"
echo "Current Shell: $SHELL"
echo "Home Directory: $HOME"
echo "OS Type: $(uname -o)"
echo "Current Path Setting: $PATH"
echo "Current Working Directory: $(pwd)"
echo "Number of Logged Users: $(who | wc -l)"
echo "Available Shells: $(cat /etc/shells)"
echo "CPU Information: $(lscpu)"
echo "Memory Information: $(free -h)"
```
3) Write Shell Script to Demonstrate Pipes, Redirection, and `tee`
```bash
#!/bin/bash
echo "Pipes Example: ls -l | grep .sh"
echo "Redirection Example: echo Hello > file.txt"
echo "Tee Example: echo Hello | tee file.txt"
```
4) Write Shell Script to Find Sum of Digits
```bash
#!/bin/bash
echo "Enter a number:"
read num
sum=0
while [ $num -gt 0 ]; do
digit=$((num % 10))
sum=$((sum + digit))
num=$((num / 10))
done
echo "Sum of digits: $sum"
```
5) Write Shell Script to Find the Greatest Number Using Command Line Arguments
```bash
#!/bin/bash
if [ $# -lt 2 ]; then
echo "Provide at least two numbers."
exit 1
fi
greatest=$1
for number in "$@"; do
if [ $number -gt $greatest ]; then
greatest=$number
fi
done
echo "Greatest number: $greatest"
```
Script Questions
1) **Multiplication Script:**
```bash
#!/bin/bash
echo "Enter two numbers:"
read a b
echo "Product: $((a * b))"
```
2) **Positive or Negative Script:**
```bash
#!/bin/bash
echo "Enter a number:"
read num
if [ $num -ge 0 ]; then
echo "Positive"
else
echo "Negative"
fi
```
3) **Odd or Even Script:**
```bash
#!/bin/bash
echo "Enter a number:"
read num
if [ $((num % 2)) -eq 0 ]; then
echo "Even"
else
echo "Odd"
fi
```
4) **Voting Eligibility Script:**
```bash
#!/bin/bash
echo "Enter your age:"
read age
if [ $age -ge 18 ]; then
echo "Eligible to vote"
else
echo "Not eligible to vote"
fi
```
5) **Factorial Script:**
```bash
#!/bin/bash
echo "Enter a number:"
read num
fact=1
for ((i=1; i<=num; i++)); do
fact=$((fact * i))
done
echo "Factorial: $fact"
```
6) **Sum of Digits Script:**
```bash
#!/bin/bash
echo "Enter a number:"
read num
sum=0
while [ $num -gt 0 ]; do
digit=$((num % 10))
sum=$((sum + digit))
num=$((num / 10))
done
echo "Sum of digits: $sum"
```
7) **Biodata Script:**
```bash
#!/bin/bash
echo "Enter your name:"
read name
echo "Enter your age:"
read age
echo "
Enter your address:"
read address
echo "Biodata:"
echo "Name: $name"
echo "Age: $age"
echo "Address: $address"
```
These answers provide both detailed explanations and examples for each command and script
requested.