Shell Programming
&
Shell Scripting
Commonly used shell commands in programming:
Shebang to specify shell
#!/bin/bash
Display output
echo "Hello, World!"
Read user input
read -p "Enter your name: " name
echo "Hello, $name!"
Conditional statements
if [ $name == "Satyam" ]; then
echo "Welcome, Satyam!"
fi
For loop
for i in {1..5}; do
echo "Number: $i"
done
While loop
count=5
while [ $count -gt 0 ]; do
echo "Countdown: $count"
count=$((count - 1))
done
Mathematical operations
num1=10
num2=5
sum=$((num1 + num2))
echo "Sum: $sum"
Command substitution
current_date=$(date)
echo "Today is: $current_date"
Function declaration
greet() {
echo "Hello, $1!"
greet "Satyam"
Redirecting output to a file
echo "This is a sample text." > output.txt
Appending output to a file
echo "Another line." >> output.txt
File existence check
if [ -f "output.txt" ]; then
echo "File exists."
fi
Execute another script
./another_script.sh
Permission change
chmod +x script.sh
Coding Part
Conditional Statement: Even or Odd Check
bash
CopyEdit
#!/bin/bash
read -p "Enter a number: " num
if ((num % 2 == 0)); then
echo "$num is even."
else
echo "$num is odd."
Fi
How it Works:
• The if statement checks if the number is divisible by 2.
• The % operator is used for modulo.
Looping Statement: Print Numbers from 1 to 5
bash
CopyEdit
#!/bin/bash
echo "Printing numbers from 1 to 5:"
for ((i=1; i<=5; i++)); do
echo "Number: $i"
done
How it Works:
• The for loop iterates from 1 to 5 using arithmetic expression syntax (( )).
Conditional + Looping: Print Multiplication Table
bash
CopyEdit
#!/bin/bash
read -p "Enter a number for the multiplication table: " num
for ((i=1; i<=10; i++)); do
result=$((num * i))
echo "$num x $i = $result"
done
4. While Loop: Countdown Timer
bash
CopyEdit
#!/bin/bash
count=5
while ((count > 0)); do
echo "Countdown: $count"
count=$((count - 1))
sleep 1 # Pause for 1 second
done
echo "Blast off!"
5. Menu-Driven Program (Conditional and Looping)
bash
CopyEdit
#!/bin/bash
while true; do
echo "Choose an option:"
echo "1. Show date"
echo "2. List files"
echo "3. Display current user"
echo "4. Exit"
read -p "Enter your choice: " choice
case $choice in
1) date ;;
2) ls ;;
3) whoami ;;
4) echo "Exiting..."; break ;;
*) echo "Invalid choice, try again." ;;
esac
done
How it Works:
• The while loop keeps displaying the menu until the user selects 4.
• The case statement handles different user choices.
Let me know if you'd like more variations or explanations!
4o
You said:
maths realted question
ChatGPT said:
ChatGPT
Here are a few basic shell scripts to perform mathematical operations and solve simple math-related
tasks:
1. Simple Calculator (Addition, Subtraction, Multiplication, Division)
bash
CopyEdit
#!/bin/bash
echo "Simple Calculator"
read -p "Enter first number: " num1
read -p "Enter second number: " num2
echo "Choose an operation:"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read -p "Enter your choice: " choice
case $choice in
1) result=$((num1 + num2))
echo "Result: $num1 + $num2 = $result" ;;
2) result=$((num1 - num2))
echo "Result: $num1 - $num2 = $result" ;;
3) result=$((num1 * num2))
echo "Result: $num1 * $num2 = $result" ;;
4) if ((num2 == 0)); then
echo "Division by zero is not allowed."
else
result=$((num1 / num2))
echo "Result: $num1 / $num2 = $result"
fi ;;
*) echo "Invalid choice." ;;
esac
2. Factorial of a Number
bash
CopyEdit
#!/bin/bash
read -p "Enter a number to calculate its factorial: " num
factorial=1
for ((i=1; i<=num; i++)); do
factorial=$((factorial * i))
done
echo "Factorial of $num is $factorial"
3. Check if a Number is Prime
bash
CopyEdit
#!/bin/bash
read -p "Enter a number to check if it's prime: " num
is_prime=1
if ((num <= 1)); then
is_prime=0
else
for ((i=2; i*i<=num; i++)); do
if ((num % i == 0)); then
is_prime=0
break
fi
done
fi
if ((is_prime == 1)); then
echo "$num is a prime number."
else
echo "$num is not a prime number."
fi
4. Sum of Digits
bash
CopyEdit
#!/bin/bash
read -p "Enter a number: " num
sum=0
while ((num > 0)); do
digit=$((num % 10))
sum=$((sum + digit))
num=$((num / 10))
done
echo "Sum of digits is $sum"
5. Fibonacci Sequence
bash
CopyEdit
#!/bin/bash
read -p "Enter the number of terms for Fibonacci series: " n
a=0
b=1
echo "Fibonacci Series:"
for ((i=0; i<n; i++)); do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo