helpful-code-snippets/scripts/add-to-bin

35 lines
947 B
Plaintext
Raw Normal View History

#!/bin/bash
# this script links the script given as an argument to the /usr/bin directory, and makes it executable
# it also checks if the script is already in the bin directory
# check if the script is given as an argument
if [ -z "$1" ]; then
echo "Usage: $0 <script>"
exit 1
fi
# check if the script exists
if [ ! -f "$1" ]; then
echo "Error: $1 does not exist"
exit 1
fi
# check if the script is already in the bin directory
if [ -f "/usr/bin/$(basename $1)" ]; then
echo "Error: $(basename $1) is already in the bin directory"
exit 1
fi
# make the script executable
sudo chmod +x "$1"
# link the script to the bin directory, the origin has to be an absolute path
sudo ln -s "$(realpath $1)" /usr/bin/$(basename $1)
# check if the link was successful
if [ $? -eq 0 ]; then
echo "Success: $(basename $1) is now in the bin directory"
else
echo "Error: failed to link $(basename $1) to the bin directory"
fi