Skip to content

Instantly share code, notes, and snippets.

@marufmax
Last active April 7, 2023 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marufmax/c19c4c391264dea9f339b1f7eeea80a3 to your computer and use it in GitHub Desktop.
Save marufmax/c19c4c391264dea9f339b1f7eeea80a3 to your computer and use it in GitHub Desktop.
my Most commonly used cli commands
** Finding a file**
```
find / -name "ansible" 2>&1 | grep -v "Permission denied"
```
** find a text in directory:**
grep -rnw /PATH/ -e 'minikube'
detect_os ()
{
if [[ ( -z "${os}" ) && ( -z "${dist}" ) ]]; then
# some systems dont have lsb-release yet have the lsb_release binary and
# vice-versa
if [ -e /etc/lsb-release ]; then
. /etc/lsb-release
if [ "${ID}" = "raspbian" ]; then
os=${ID}
dist=`cut --delimiter='.' -f1 /etc/debian_version`
else
os=${DISTRIB_ID}
dist=${DISTRIB_CODENAME}
if [ -z "$dist" ]; then
dist=${DISTRIB_RELEASE}
fi
fi
elif [ `which lsb_release 2>/dev/null` ]; then
dist=`lsb_release -c | cut -f2`
os=`lsb_release -i | cut -f2 | awk '{ print tolower($1) }'`
elif [ -e /etc/debian_version ]; then
# some Debians have jessie/sid in their /etc/debian_version
# while others have '6.0.7'
os=`cat /etc/issue | head -1 | awk '{ print tolower($1) }'`
if grep -q '/' /etc/debian_version; then
dist=`cut --delimiter='/' -f1 /etc/debian_version`
else
dist=`cut --delimiter='.' -f1 /etc/debian_version`
fi
else
echo "Unknown"
fi
fi
if [ -z "$dist" ]; then
echo "Unknown"
fi
# remove whitespace from OS and dist name
os="${os// /}"
dist="${dist// /}"
echo "Detected operating system as $os/$dist."
}
detect_version_id () {
# detect version_id and round down float to integer
if [ -f /etc/os-release ]; then
. /etc/os-release
version_id=${VERSION_ID%%.*}
elif [ -f /usr/lib/os-release ]; then
. /usr/lib/os-release
version_id=${VERSION_ID%%.*}
else
version_id="1"
fi
}
#!/bin/bash
# Get a list of all exited Docker containers
exited_containers=$(docker ps -a -q -f status=exited)
# Check if there are any exited containers
if [ -n "$exited_containers" ]; then
# Remove all exited containers
docker rm $exited_containers
echo "All exited Docker containers have been removed."
else
echo "There are no exited Docker containers to remove."
fi
#!/bin/bash
container_names=("test1" "test2")
for container in "${container_names[@]}"
do
# Check if container is running
if [ "$(docker inspect -f '{{.State.Running}}' $container)" != "true" ]; then
if [ "$(docker ps -aq -f status=exited -f name=$container)" ]; then
# Cleanup
echo ":: $container has an error. So removing it..."
docker rm $container
fi
# Run your container
docker run -d --name $container your-image-name-here
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment