Some Bash Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Some Bash Programs.

These programs provide a range of use cases, from user management and system monitoring
to practical automation and string manipulation. Below are 30 Bash scripts for common use
cases, categorized into different functionalities:

1. Hello World
#!/bin/bash
echo "Hello, World!"

2. Check Even or Odd


#!/bin/bash
echo "Enter a number:"
read num
if (( num % 2 == 0 )); then
echo "$num is Even"
else
echo "$num is Odd"
fi

3. Factorial Calculation
#!/bin/bash
echo "Enter a number:"
read num
fact=1
for (( i=1; i<=num; i++ )); do
fact=$(( fact * i ))
done
echo "Factorial of $num is $fact"

4. Print Multiplication Table


#!/bin/bash
echo "Enter a number:"
read num
for (( i=1; i<=10; i++ )); do
echo "$num x $i = $(( num * i ))"
done

5. File Existence Check


#!/bin/bash
echo "Enter the file name:"
read file
if [[ -e $file ]]; then
echo "$file exists."
else
echo "$file does not exist."
fi

6. Reverse a String
#!/bin/bash
echo "Enter a string:"
read str
echo "$str" | rev

7. Palindrome Check
#!/bin/bash
echo "Enter a string:"
read str
if [[ "$str" == "$(echo $str | rev)" ]]; then
echo "$str is a palindrome."
else
echo "$str is not a palindrome."
fi

8. Disk Usage Monitor


#!/bin/bash
threshold=80
usage=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if (( usage > threshold )); then
echo "Warning: Disk usage is above $threshold%."
else
echo "Disk usage is under control."
fi

9. Check Prime Number


#!/bin/bash
echo "Enter a number:"
read num
is_prime=1
for (( i=2; i<=num/2; i++ )); do
if (( num % i == 0 )); then
is_prime=0
break
fi
done
if (( is_prime == 1 )); then
echo "$num is a prime number."
else
echo "$num is not a prime number."
fi

10. User Information Display


#!/bin/bash
echo "Username: $USER"
echo "Home Directory: $HOME"
echo "Shell: $SHELL"

11. System Uptime


#!/bin/bash
uptime | awk -F, '{print $1}'

12. File Word Count


#!/bin/bash
echo "Enter file name:"
read file
if [[ -f $file ]]; then
wc -w $file
else
echo "File does not exist."
fi

13. Backup Script


#!/bin/bash
echo "Enter directory to backup:"
read dir
echo "Enter destination for backup:"
read dest
if [[ -d $dir ]]; then
tar -cvzf "$dest/backup.tar.gz" "$dir"
echo "Backup created successfully at $dest."
else
echo "Directory does not exist."
fi

14. Countdown Timer


#!/bin/bash
echo "Enter the number of seconds:"
read seconds
while (( seconds > 0 )); do
echo "$seconds"
((seconds--))
sleep 1
done
echo "Time's up!"

15. Find Largest of Three Numbers


#!/bin/bash
echo "Enter three numbers:"
read a b c
if (( a > b && a > c )); then
echo "$a is the largest."
elif (( b > c )); then
echo "$b is the largest."
else
echo "$c is the largest."
fi

16. Search for a String in a File


#!/bin/bash
echo "Enter file name:"
read file
echo "Enter the string to search:"
read str
if grep -q "$str" "$file"; then
echo "String found in $file"
else
echo "String not found."
fi

17. Rename Multiple Files


#!/bin/bash
echo "Enter file extension:"
read ext
echo "Enter prefix for new files:"
read prefix
count=1
for file in *.$ext; do
mv "$file" "${prefix}${count}.$ext"
((count++))
done

18. Create a User


#!/bin/bash
echo "Enter username to create:"
read username
sudo useradd $username
echo "User $username created."
19. Memory Usage
#!/bin/bash
free -h

20. Directory Size


#!/bin/bash
echo "Enter directory path:"
read dir
du -sh "$dir"

21. Fibonacci Series


#!/bin/bash
echo "Enter number of terms:"
read n
a=0
b=1
for (( i=0; i<n; i++ )); do
echo "$a"
temp=$(( a + b ))
a=$b
b=$temp
done

22. Kill a Process


#!/bin/bash
echo "Enter process name:"
read pname
pid=$(pgrep $pname)
kill -9 $pid
echo "$pname killed."
23. Convert Lowercase to Uppercase
#!/bin/bash
echo "Enter a string:"
read str
echo "${str^^}"

24. Line Count in a File


#!/bin/bash
echo "Enter file name:"
read file
if [[ -f $file ]]; then
wc -l $file
else
echo "File does not exist."
fi

25. Display System Information


#!/bin/bash
echo "System Information:"
uname -a

26. List Files by Size


#!/bin/bash
echo "Files sorted by size:"
ls -lhS

27. Log User Login Times


#!/bin/bash
who > user_logins.txt
echo "User login times saved to user_logins.txt."
28. Calculator
#!/bin/bash
echo "Enter two numbers:"
read a b
echo "Enter operator (+, -, *, /):"
read op
result=$(echo "$a $op $b" | bc)
echo "Result: $result"

29. Generate Random Password


#!/bin/bash
echo "Generated Password: $(openssl rand -base64 12)"

30. Crontab Example

Schedule a job to run every minute:

#!/bin/bash
echo "Scheduling a job..."
echo "* * * * * /path/to/script.sh" | crontab -

You might also like