From 69bfec3a8f73c74f2a9044af69e87a209c81b9ef Mon Sep 17 00:00:00 2001 From: Vadym Furmanchuk Date: Sun, 12 Dec 2021 01:07:27 +0200 Subject: [PATCH] TASK02: Add hot plugged/unplugged hardware detector script Add bash task --- 02_bash/hwdetect/hwdetect.sh | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 02_bash/hwdetect/hwdetect.sh diff --git a/02_bash/hwdetect/hwdetect.sh b/02_bash/hwdetect/hwdetect.sh new file mode 100755 index 0000000..460bb24 --- /dev/null +++ b/02_bash/hwdetect/hwdetect.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +OLDLIST="/tmp/oldlist" +NEWLIST="/tmp/newlist" + +update_list() +{ + ls /dev/ > $1 +} + +get_diff() +{ + local oldlist_num=`wc -l < $OLDLIST` + local newlist_num=`wc -l < $NEWLIST` + local diff=$(($oldlist_num - $newlist_num)) + + if [ $(($diff + 0)) -eq 0 ]; then + return 0 + fi + + return 1 +} + +print_diff() +{ + comm -23 <(sort $OLDLIST) <(sort $NEWLIST) | awk '{print $1 " unplugged" }' + + comm -13 <(sort $OLDLIST) <(sort $NEWLIST) | awk '{print $1 " plugged" }' +} + + + +update_list $OLDLIST + +while : +do + sleep 1 + update_list $NEWLIST + get_diff + diff=$? + if [ $(($diff + 0)) -ne 0 ]; then + print_diff + cp $NEWLIST $OLDLIST + fi +done