The document provides a comprehensive collection of advanced shell script solutions covering various tasks such as user checks, file operations, process monitoring, and system management. Each solution is presented with a brief description and a corresponding script example. It serves as a practical guide for users looking to enhance their shell scripting skills.
The document provides a comprehensive collection of advanced shell script solutions covering various tasks such as user checks, file operations, process monitoring, and system management. Each solution is presented with a brief description and a corresponding script example. It serves as a practical guide for users looking to enhance their shell scripting skills.
#!/bin/bash file="/etc/passwd" if [ -f "$file" ]; then echo "$file exists." else echo "$file does not exist." fi
4. How do you list all files in a directory?
#!/bin/bash directory="/etc" echo "Files in $directory:" ls "$directory"
5. How do you find the number of lines in a file?
#!/bin/bash file="/etc/passwd" lines=$(wc -l < "$file") echo "The file $file has $lines lines."
6. How do you create a backup of a directory?
#!/bin/bash src="/etc" dest="/tmp/backup" mkdir -p "$dest" cp -r "$src" "$dest" echo "Backup completed to $dest" 7. How do you check for disk space usage? #!/bin/bash df -h
8. How do you monitor a running process?
#!/bin/bash process_name="sshd" if pgrep "$process_name" > /dev/null; then echo "$process_name is running." else echo "$process_name is not running." fi
9. How do you calculate the sum of two numbers?
#!/bin/bash read -p "Enter first number: " num1 read -p "Enter second number: " num2 sum=$((num1 + num2)) echo "The sum is: $sum"
10. How do you compress and extract files?
Compress:
#!/bin/bash tar -czf archive.tar.gz /etc echo "Files compressed to archive.tar.gz"
Extract:
#!/bin/bash tar -xzf archive.tar.gz echo "Files extracted from archive.tar.gz"
11. How do you schedule a task to run in the background?
#!/bin/bash str1="hello" str2="world" if [ "$str1" = "$str2" ]; then echo "Strings are equal." else echo "Strings are not equal." fi
17. How do you find the largest file in a directory?
#!/bin/bash directory="/etc" largest_file=$(find "$directory" -type f -exec du -h {} + | sort -rh | head -1) echo "Largest file: $largest_file"
18. How do you rename multiple files at once?
#!/bin/bash for file in *.txt; do mv "$file" "${file%.txt}.bak" done echo "Renamed all .txt files to .bak"
19. How do you kill a process by name?
#!/bin/bash pkill -f "process_name" echo "Process killed." 20. How do you check if a service is active? #!/bin/bash service_name="ssh" systemctl is-active --quiet "$service_name" && echo "$service_name is active" || echo "$service_name is inactive"
21. How do you loop through files in a directory?
#!/bin/bash for file in /etc/*; do echo "File: $file" done
41. How do you calculate the factorial of a number?
#!/bin/bash read -p "Enter a number: " num factorial=1 for ((i = 1; i <= num; i++)); do factorial=$((factorial * i)) done echo "Factorial of $num is $factorial"
42. How do you extract a compressed .tar.gz file to a specific directory?
#!/bin/bash file="archive.tar.gz" dest="/path/to/extract" mkdir -p "$dest" tar -xzf "$file" -C "$dest" echo "Extracted to $dest" 43. How do you check the status of multiple services? #!/bin/bash services=("ssh" "cron" "nginx") for service in "${services[@]}"; do systemctl is-active --quiet "$service" && echo "$service is running" || echo "$service is stopped" done
44. How do you find and replace text in multiple files?
#!/bin/bash find /path/to/files -type f -name "*.txt" -exec sed -i 's/old_text/new_text/g' {} + echo "Text replaced in all files."
45. How do you calculate the disk usage of a directory?
48. How do you restart a service if it is not running?
#!/bin/bash service="nginx" if ! systemctl is-active --quiet "$service"; then systemctl restart "$service" echo "$service restarted." else echo "$service is running." fi
49. How do you check the number of open file descriptors?
#!/bin/bash echo "Open file descriptors: $(lsof | wc -l)" 50. How do you create a script that executes commands in parallel? #!/bin/bash commands=("sleep 3" "ls -l" "date") for cmd in "${commands[@]}"; do ($cmd &) done wait echo "All commands executed."