0% found this document useful (0 votes)
3 views25 pages

OS Shell Scripts CIA1

The document contains a series of Bash scripts that perform various tasks including calculating factorials, generating Fibonacci series, performing arithmetic operations, checking for prime numbers, and more. Each script includes input validation and outputs results based on user-provided arguments. The scripts cover a wide range of functionalities such as file operations, mathematical calculations, and string manipulations.

Uploaded by

sunitadsouza1975
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)
3 views25 pages

OS Shell Scripts CIA1

The document contains a series of Bash scripts that perform various tasks including calculating factorials, generating Fibonacci series, performing arithmetic operations, checking for prime numbers, and more. Each script includes input validation and outputs results based on user-provided arguments. The scripts cover a wide range of functionalities such as file operations, mathematical calculations, and string manipulations.

Uploaded by

sunitadsouza1975
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

1.

Factorial
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 number"
exit 1
fi
n=$1
fact=1
for ((i=1;i<=n;i++)); do
fact=$((fact*i))
done
echo "Factorial of $n is $fact"
2. Fibonacci
#!/bin/bash
if [ $# -ne 1 ]; then echo "Usage: $0 N"; exit 1; fi
n=$1
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
3. Calculator
#!/bin/bash
if [ $# -ne 3 ]; then echo "Usage: $0 num1 operator num2"; exit 1; fi
a=$1; op=$2; b=$3
case $op in
+) echo $((a+b));;
-) echo $((a-b));;
x|\*) echo $((a*b));;
/) echo $((a/b));;
*) echo "Invalid";;
esac
4. Check Directory
#!/bin/bash
if [ $# -ne 1 ]; then exit 1; fi
[ -d "$1" ] && echo "Directory" || echo "Not Directory"
5. Prime
#!/bin/bash
n=$1; flag=0
for ((i=2;i<=n/2;i++)); do
if [ $((n%i)) -eq 0 ]; then flag=1; break; fi
done
[ $flag -eq 0 ] && echo "Prime" || echo "Not Prime"
6. Even Odd
#!/bin/bash
[ $(( $1 % 2 )) -eq 0 ] && echo "Even" || echo "Odd"
7. Largest of 3
#!/bin/bash
a=$1; b=$2; c=$3
max=$a
[ $b -gt $max ] && max=$b
[ $c -gt $max ] && max=$c
echo $max
8. Reverse Number
#!/bin/bash
n=$1; rev=0
while [ $n -gt 0 ]; do
rev=$((rev*10 + n%10))
n=$((n/10))
done
echo $rev
9. Palindrome String
#!/bin/bash
str=$1
rev=$(echo $str | rev)
[ "$str" = "$rev" ] && echo "Palindrome" || echo "Not"
10. Count LWC
#!/bin/bash
wc "$1"
11. Table
#!/bin/bash
n=$1
for ((i=1;i<=10;i++)); do
echo "$n x $i = $((n*i))"
done
12. Sum Digits
#!/bin/bash
n=$1; sum=0
while [ $n -gt 0 ]; do
sum=$((sum+n%10))
n=$((n/10))
done
echo $sum
13. Swap
#!/bin/bash
a=$1; b=$2
a=$((a+b)); b=$((a-b)); a=$((a-b))
echo "a=$a b=$b"
14. File Exists
#!/bin/bash
[ -e "$1" ] && echo "Exists" || echo "Not"
15. File Details
#!/bin/bash
ls -lh "$1"
16. Print 1..N
#!/bin/bash
for ((i=1;i<=$1;i++)); do echo -n "$i "; done
17. Smallest N
#!/bin/bash
min=$1
for i in "$@"; do [ $i -lt $min ] && min=$i; done
echo $min
18. Power
#!/bin/bash
echo $(( $1 ** $2 ))
19. Leap Year
#!/bin/bash
y=$1
if (( (y%4==0 && y%100!=0) || y%400==0 )); then
echo "Leap"
else
echo "Not"
fi
20. Count Files
#!/bin/bash
ls -1 "$1" | wc -l
21. Reverse File
#!/bin/bash
tac "$1"
22. Executable Files
#!/bin/bash
find "$1" -type f -executable
23. GCD
#!/bin/bash
a=$1; b=$2
while [ $b -ne 0 ]; do
t=$b; b=$((a%b)); a=$t
done
echo $a
24. Natural Numbers Sum
#!/bin/bash
sum=0
for ((i=1;i<=$1;i++)); do
echo -n "$i "
sum=$((sum+i))
done
echo; echo $sum
25. Case Menu
#!/bin/bash
case $1 in
1) date;;
2) cal;;
3) ls;;
*) echo "Invalid";;
esac

You might also like