Skip to content

Instantly share code, notes, and snippets.

@rwbot
Last active March 19, 2024 22:19
Show Gist options
  • Save rwbot/afb68bdbb273ecfa614f9959aba58564 to your computer and use it in GitHub Desktop.
Save rwbot/afb68bdbb273ecfa614f9959aba58564 to your computer and use it in GitHub Desktop.
scripts
#!/bin/bash
# Get Machine Serial ID and set as env variable
MACHINE_SERIAL=$(sudo lshw |grep -m1 "serial:")
# Extracting value to the right of the colon
NODE_SERIAL=$(echo "$MACHINE_SERIAL" | awk -F': ' '{print $2}')
echo -e "\n" >> ~/.bashrc
echo -e "export NODE_SERIAL=$NODE_SERIAL" >> ~/.bashrc
# Print the extracted value
echo "NODE_SERIAL"
echo "$NODE_SERIAL"
#!/bin/bash
# Check if -h option is provided
if [[ $1 == "-h" ]]; then
echo "Usage: $0 [options]..."
echo "Find the next available number for a file prefix in a specified location"
echo " location The directory to check for files"
echo " prefix The prefix of the files to check"
echo " [optional] "
echo " outputVar The variable name to print with the index of the first non-existing file"
echo " If outputVar is not provided, only the index is printed"
echo ""
echo "Example: ./next_port_num.sh /dev ttyACM"
echo " |-> 0"
exit 0
fi
# Check if at least two arguments are provided
if [ $# -lt 2 ]; then
echo "Usage: $0 location prefix [outputVar]" >&2
echo "Example: ./next_port_num.sh /dev ttyACM" >&2
exit 1
fi
# location: The directory to check for files
location="$1"
# prefix: The prefix of the files to check
prefix="$2"
# outputVar (optional): The variable name to print with the index of the first non-existing file
# If outputVar is not provided, only the index is printed
key="${3:-}"
needindex=1
index=0
while [ $needindex -eq 1 ]
do
# Check if a file with the current index exists
if [ ! -e $location/$prefix$index ]; then
needindex=0
# If outputVar is not provided, print only the index
if [ -z "$key" ]; then
echo "$index"
# If outputVar is provided, print outputVar=index
else
echo "$key=$index"
fi
else
# If a file with the current index exists, increment the index
(( index++ ))
fi
done
#!/bin/bash
sudo cp ~/node/cfg/99-serialports-udev.rules /etc/udev/rules.d/
sudo service udev restart
sudo udevadm control --reload-rules && sudo service udev restart && sudo udevadm trigger
exit
$SHELL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment