The document contains a series of Bash scripts for various tasks including printing 'Hello, World!', checking if a number is even or odd, performing basic arithmetic operations, checking if a name is a file or directory, displaying the current date and time, checking for palindromes, printing numbers up to N, counting files in a directory, displaying system uptime, and creating a backup of a specified directory. Each script is provided with the necessary code and comments for clarity. These scripts serve as practical examples for beginners learning Bash scripting.
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 ratings0% found this document useful (0 votes)
12 views5 pages
Assignmet 2
The document contains a series of Bash scripts for various tasks including printing 'Hello, World!', checking if a number is even or odd, performing basic arithmetic operations, checking if a name is a file or directory, displaying the current date and time, checking for palindromes, printing numbers up to N, counting files in a directory, displaying system uptime, and creating a backup of a specified directory. Each script is provided with the necessary code and comments for clarity. These scripts serve as practical examples for beginners learning Bash scripting.
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/ 5
Assignment-2
1. Write a script to print "Hello, World!".
echo "Hello World"
2.Create a script to check if a number is even or odd.
#!/bin/bash echo -n "Enter a number: " read num if [ $((num % 2)) -eq 0 ]; then echo "The number $num is even." else echo "The number $num is odd." fi
3.Write a simple calculator script for addition, subtraction,
multiplication, and division. #!/bin/bash echo "Enter two numbers: " read a b echo "Choose an operation (+, -, *, /): " read op case $op in +) echo "Result: $((a + b))";; -) echo "Result: $((a - b))";; \*) echo "Result: $((a * b))";; /) echo "Result: $((a / b))";; *) echo "Invalid operation";; esac 4.Check if a given name is a file or a directory. #!/bin/bash echo -n "Enter a name: " read name if [ -d "$name" ]; then echo "$name is a directory." elif [ -f "$name" ]; then echo "$name is a file." else echo "$name is neither a file nor a directory." fi
5.Write a script to display the current date and time.
#!/bin/bash echo "Current date and time: $(date)"
6.Create a script to check if a string is a palindrome.
#!/bin/bash echo -n "Enter a string: " read str rev=$(echo "$str" | awk '{ for(i=length;i!=0;i--) x=x substr($0,i,1); print x }') if [ "$str" == "$rev" ]; then echo "$str is a palindrome." else echo "$str is not a palindrome." fi
7 Print numbers from 1 to N based on user input.
#!/bin/bash echo -n "Enter the value of N: " read N for ((i = 1; i <= N; i++)); do echo $i done
8 Count and display the number of files in the current directory.
#!/bin/bash echo "Number of files in the current directory: $(ls -l | grep -v '^d' | wc -l)" 9 Write a script to display the system uptime.
10 Create a backup script for a directory provided by the user.
#!/bin/bash echo -n "Enter the directory to back up: " read dir if [ -d "$dir" ]; then backup_name="backup_$(basename "$dir")_$(date +%Y%m%d%H%M%S).tar.gz" tar -czf "$backup_name" "$dir" echo "Backup created: $backup_name" else echo "Directory $dir does not exist." fi