$0 = script name
$1, $2, etc. = arguments passed to script
$# = number of arguments
$@ = all arguments
What is the difference between $@ and $*?
$@ → treats each argument separately
$* → treats all arguments as a single string
ls -l | grep "^d" --> to get dir only
How do you remove blank lines from a file?
sed '/^$/d' file.txt
sed -i 's/oldword/newword/g' file.txt -- replace
awk '{print $2, $4}' file.txt
sed -n '5p' file.txt --5th line
find /path -type f -size +2G -mtime +30
find /path -type f -size +2G -mtime +30 -exec rm -f {} \;
----to find process is running
#!/bin/bash
if ! pgrep -x "tomcat" > /dev/null
then
echo "Tomcat is down! Restarting..."
systemctl start tomcat
else
echo "Tomcat is running."
fi
----Disk usage alert
#!/bin/bash
usage=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ $usage -gt 80 ]; then
echo "Warning: Disk usage is ${usage}% on $(hostname)" | mail -s "Disk Alert" [email protected]
fi
-------top 5 memory consuming process
ps -eo pid,comm,%mem --sort=-%mem | head -6
---read line by line
while read line; do
echo "Line: $line"
done < file.txt
---extrat and email error
grep -i "error" /var/log/app.log > /tmp/error.log
if [ -s /tmp/error.log ]; then
mail -s "Error Alert - $(hostname)" [email protected] < /tmp/error.log
fi