Linux Important Questions Answer
Linux Important Questions Answer
```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"
```
- Example:
```bash
#!/bin/bash
echo $((RANDOM))
```
9) Finding Files
- Example:
```bash
sha256sum file.txt
```
- 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)"
```
```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"
```
```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))"
```
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"
```
7) **Biodata Script:**
```bash
#!/bin/bash
echo "Enter your name:"
read name
echo "Enter your age:"
read age
echo "
These answers provide both detailed explanations and examples for each command and script
requested.