Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 02_bash/hwdetect/hwdetect.sh
Original file line number Diff line number Diff line change
@@ -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