0% found this document useful (0 votes)
8 views14 pages

Lab2 3

The document contains a series of scripting tasks for a lab exercise, including printing 'Hello World', checking if a number is even or odd, performing basic calculations, and managing files and directories. It also includes tasks for string manipulation, generating random passwords, and analyzing user information on a system. Each task is accompanied by a script example demonstrating the required functionality.
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)
8 views14 pages

Lab2 3

The document contains a series of scripting tasks for a lab exercise, including printing 'Hello World', checking if a number is even or odd, performing basic calculations, and managing files and directories. It also includes tasks for string manipulation, generating random passwords, and analyzing user information on a system. Each task is accompanied by a script example demonstrating the required functionality.
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
You are on page 1/ 14

LAB 2

1. Write Script to print “Hello World !!”

echo "Hello, World!"

2. Check if number is even or odd


read -p "Enter a number: " num
if ((num % 2 == 0)); then
echo "$num is even."
else
echo "$num is odd."
Fi

3. calculator :
echo "Enter two numbers:"
read num1 num2
echo "Enter operation (+, -, *, /):"
read op
case $op in
"+") result=$((num1 + num2)) ;;
"-") result=$((num1 - num2)) ;;
"*") result=$((num1 * num2)) ;;
"/") result=$((num1 / num2)) ;;
*) echo "Invalid operation"; exit 1 ;;
esac

echo "Result: $result"

4. check if given name is file or directory :


read -p "Enter a name: " name
if [ -f "$name" ]; then
echo "$name is a file."
elif [ -d "$name" ]; then
echo "$name is a directory."
else
echo "$name does not exist."
fi
5. Display Current Date and Time :
echo "Current date and time: $(date)"

6. check if String is palindrome or not.


echo "Enter a string:"
read str

rev_str = $(echo "$str" | rev)


# Check if the original and reversed strings are the same
if [ "$str" == "$rev_str" ]; then
echo "The string '$str' is a palindrome."
else
echo "The string '$str' is not a palindrome."
fi

7. print numbers from 1 to N based on user input:


read -p "Enter a number: " n
for ((i = 1; i <= n; i++)); do
echo $i
done

8. Count number of files in current directory:


count=$(ls -1 | wc -l)
echo "Number of files in the current directory: $count"

9. write a script to display system uptime


echo "System uptime: $(uptime -p)"
10.
read -p "Enter the directory to back up: " dir
read -p "Enter the destination directory: " dest

if [ -d "$dir" ]; then
mkdir -p "$dest"
cp -r "$dir" "$dest"
echo "Backup completed successfully."
else
echo "The directory $dir does not exist."
LAB 3

11. Print a given number in reverse order.

echo "Enter a number:"


read num
rev=0
while [ $num -ne 0 ]; do
digit=$((num % 10))
rev=$((rev * 10 + digit))
num=$((num / 10))
done
echo "Reversed Number: $rev"

12. Read ‘n’ from the user and print the Fibonacci sequence until ‘n’.
echo "Enter the value of n:"
read n
a=0
b=1
echo "Fibonacci Sequence:"
echo $a
echo $b
for ((i=2; i<n; i++)); do
fib=$((a + b))
echo $fib
a=$b
b=$fib
done

13. Say “Hello” to a user and greet them based on the time of the day (Good Morning /
Eve etc.).

hour=$(date +%H)
if [ $hour -lt 12 ]; then
echo "Good Morning!"
elif [ $hour -lt 18 ]; then
echo "Good Afternoon!"
else
echo "Good Evening!"
fi

14. Write a script for printing all file-related information in the present
working directory (size, permissions, etc.).

for file in *; do
if [ -f "$file" ]; then
echo "File: $file"
ls -lh "$file"
fi

done

15. Print the length of each and every string using arrays.
echo "Enter strings separated by space:"
read -a strings
for str in "${strings[@]}"; do
echo "Length of '$str': ${#str}"
done

16. Display the longest and the shortest usernames on a system.

#!/bin/bash
longest=""
shortest=""
users=("alice" "bob" "charlie" "david")
for user in "${users[@]}"; do
if [ -z "$longest" ] || [ ${#user} -gt ${#longest} ]; then
longest=$user
fi
if [ -z "$shortest" ] || [ ${#user} -lt ${#shortest} ]; then
shortest=$user
fi
done
echo "Longest username: $longest"
echo "Shortest username: $shortest"
17. Generate five random 8-character passwords having alphanumeric
characters.

for i in {1..5}; do
pw=$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 8)
echo "Password $i: $pw"
done

18. Display the names of all file systems that have less than 10% free space
available.
#!/bin/bash
df -h | awk '$5+0 > 90 {print $1}'
19. Write a script to search whether a user exists on the system or not.

df -h | awk '$5+0 > 90 {print $1, $5}'


echo "Enter username:"
read user
if id "$user" &>/dev/null; then
echo "User exists."
else
echo "User does not exist."
fi

20. Display the current date in words.

date +"%A, %B %d, %Y"

21. Print the total number of lines in a C program.


echo "Enter C program file name:"
read file
if [ -f "$file" ]; then
lines=$(wc -l < "$file")
echo "Total lines: $lines"
else
echo "File not found."
fi

22. Find whether a C file contains the printf() method or not.

echo "Enter C program file name:"


read file
if grep -q "printf" "$file"; then
echo "The file contains printf."
else
echo "The file does not contain printf."
fi
23. Find whether a C program uses void main() or int main().

echo "Enter C program file name:"


read file
if grep -q "void main" "$file"; then
echo "The program contains void main."
elif grep -q "int main" "$file"; then
echo "The program contains int main."
else
echo "No valid main function found."
fi

You might also like