Skip to content

Instantly share code, notes, and snippets.

@ledangtuanbk
Last active May 7, 2024 13:57
Show Gist options
  • Save ledangtuanbk/33b1f4e749bf4b2d039b210ea828651b to your computer and use it in GitHub Desktop.
Save ledangtuanbk/33b1f4e749bf4b2d039b210ea828651b to your computer and use it in GitHub Desktop.
Save connected wifi by date
echo "start `date`"
# Get the directory of the currently running script
script_dir=$(dirname "$0")
# Change the working directory to the directory of the script
cd "$script_dir"
echo `pwd`
results="results.csv"
if [[ -f $results ]]; then
echo "File exists."
else
echo "File does not exist."
touch $results
fi
# Now you're in the directory of the running script
# Get network name
wifiName=$(/usr/sbin/networksetup -getairportnetwork en0)
date=$(date +"%Y-%m-%d")
wifiName="${wifiName#"${wifiName%%[![:space:]]*}"}"
wifiName="${wifiName%"${wifiName##*[![:space:]]}"}"
out="$date;$wifiName"
#out="2024-04-24, Current Wi-Fi Network: Deputy_VN"
echo $out
if grep "$out" $results; then
echo "Text found in the file."
else
echo "Text not found in the file."
echo $out >> $results
fi
@ledangtuanbk
Copy link
Author

ledangtuanbk commented Apr 25, 2024

Configure to run automatically

Because MacOS have some security System Integrity Protection (SIP)

1 save file as ~/run.sh

2 create a crontab by run command

crontab -e
as content below
* * * * * ~/run.sh >> ~/crontab.log 2>&1

Check log at

~/crontab.log

@TXLuong
Copy link

TXLuong commented May 7, 2024

haha Nice work bro!

#!/bin/bash

# Function to count rows with first column value containing "Deputy_VN"
count_rows() {
    local file="$1"
    local count=0
    # Read the file line by line
    while IFS=',' read -r col1 _; do
        # Check if the first column contains "Deputy_VN"
        if [[ $col1 == *"Deputy_VN"* ]]; then
            ((count++))
        fi
    done < "$file"
    echo "🌷֒✧ ༘ ⋆。♡ The number of days you came to office 🌷֒✧ ༘ ⋆。♡ : $count"
}

# Main script
if [ $# -ne 1 ]; then
    echo "Usage: $0 <csv_file>"
    exit 1
fi

csv_file="$1"
if [ ! -f "$csv_file" ]; then
    echo "Error: File '$csv_file' not found."
    exit 1
fi

count_rows "$csv_file"

I also add one more script to count number of days coming to work.
Save it as counter.sh and execute like this:
./counter.sh results.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment