linux_lab_pgms
linux_lab_pgms
Programs:
1. Write a script to find the factorial of a number using loops
Source code:
#!/bin/bash
Output:
Case 1:
Enter a number: 4
The factorial of 4 is 24
Case 2:
Enter a number: -6
Factorial of a negative number is undefined.
Source code:
#!/bin/bash
Output:
case 1:
Enter a year: 2026
2026 is not a leap year.
case 2:
Enter a year: 2016
2016 is a leap year.
3. Write a script to sum the digits of a given number using a while loop
source code:
#!/bin/bash
sum=0
Output:
Source code:
#!/bin/bash
reversed=""
Output:
5. Write a script to count the number of lines, words, and characters in a file.
Source code:
#!/bin/bash
step 2: type a word or line or sentence in that file and click CTRL + C.
cat>demo1.txt
hello, welcome to linux programming
^C
Output:
File: demo1.txt
Number of lines: 1
Number of words: 5
Number of characters: 36
6. Write a script to check if a file exists and whether it is readable, writable, and executable.
Source code:
#!/bin/bash
filename=$1
Output:
Source code:
#!/bin/bash
Output:
8. Write a script to search for a specific word in a file and print the line containing that word.
Source code:
#!/bin/bash
# Check the exit status of grep to determine if the word was found
if [ $? -eq 0 ]; then
echo "Search complete."
else
echo "The word '$word' was not found in the file '$filename'."
fi
step 2: type a word or line or sentence in that file and click CTRL + C.
cat>demo1.txt
hello, welcome to linux programming
^C
Output: