Loading repository data…
Loading repository data…
mismailzz / repository
This repository contains the quick bash help. The snippets provide help to write the bash script/code fast in short time.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
This repository contains the quick bash help. The snippets of bash scripts/code help to write the script fast. The snippets will further be added by the time, meanwhile if anyone want to contribute, we will be much happy about it. These are those snippets which I mostly use during writing the scripts.
#!/bin/bash
#Define the arguments list in the start of the file
ARGUMENT_LIST=(
"domain"
"expiry"
"dayslimitcri"
"dayslimitwarn"
"detail"
)
#Read arguments
opts=$(getopt \
--longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
--name "$(basename "$0")" \
--options "" \
-- "$@"
)
eval set --$opts
while [[ $# -gt 0 ]]; do
case "$1" in
--domain)
DOMAIN=$2
shift 2
;;
--expiry)
EXPIRY=$2
shift 2
;;
--dayslimitcri)
DAYLIMITCRI=$2
shift 2
;;
--dayslimitwarn)
DAYLIMITWARN=$2
shift 2
;;
--detail)
DETAIL=$2
shift 2
;;
*)
break
;;
esac
done
#Now you can have the values
echo $DOMAIN
echo $EXPIRY
echo $DAYLIMITCRI
echo $DAYLIMITWARN
echo $DETAIL
if [[ "$STR1" == "" ]] || [[ "$STR2" == "" ]] || [[ "$STR2" == "" ]];then
echo "Arguments are not compelete"
exit
fi
if [[ "$STR1" == "" ]] || [[ "$STR2" == "" ]];then
echo "Arguments are not compelete"
exit
else
if [[ "$STR1" -gt "$STR2" ]]
then
echo "String1: " $STR1 " String2: " $STR2
exit
elif [[( "$STR1" -gt "$STR2" )]] && [[( "$STR1" -le "$STR2" )]]
then
echo "String1: " $STR1 " String2: " $STR2
exit 1
elif [[( "$STR1" -le "$STR2" )]]
then
echo "String1: " $STR1 " String2: " $STR2
exit 2
fi
fi
#Ref: https://unix.stackexchange.com/questions/109625/shell-scripting-z-and-n-options-with-if
[ -z "$output2" ] #string is null, that is, has zero length
[ -n "$output" ] #string is not null
[[ "$STR1" == "" ]]
[[ "$1" == "userdel" ]]
[[ "$STR1" -gt "$STR2" ]]
[[( "$STR1" -le "$STR2" )]]
[ ! -d "$dir" ] #dir does not exist
[ "$(echo "$dirperm" | cut -c6)" != "-" ] #Command execution in if statemnet
[ id ] && passing=true #if the command run sucessfull then true the passing flag
DAYS_LEFT=`python -c "from datetime import date; print((date($EXPIRY)-date.today()).days)"`
echo $DAYS_LEFT
#Function definition
help_function(){
echo "-----HELP FUNCTION-------"
echo "script.sh [OPTIONS BELOW]"
echo "PARAMETERS"
echo ""
echo "--domain <Domain Name>"
echo "--dayslimitcri <Days Limit for Critical>"
echo "--dayslimitwarn <Days Limit for Warning>"
echo "--detail <Other detail>"
echo ""
echo "Info: At this moment all parameters should be present for execution of script"
echo "-------------------------"
}
#Function call
help_function
while read line; do
echo "$line"
done <file.txt
for i in element1 element2;
do
echo $i
done
du -sh * | awk '($1~/[0-9]+\.?[0-9]*G$/)' | cut -f2 | (while read -r dir_files; do
find ./$dir_files -prune -exec stat --printf='User: %U | Group: %G | Size: ' {} \; -exec du -sh {} \;
done)
find . -xdev -type f -size +1M -exec stat --printf='User: %U | Group: %G | Size: ' {} \; -exec du -sh {} \;
#Define/Declare below function in /etc/bashrc
#~ confirm anycommand
#Ref: https://askubuntu.com/questions/22233/always-prompt-the-user-before-executing-a-command-in-the-shell
#We can show the message on the binaries in this way
confirm() {
echo -n "Do you want to run $*? [N/y] "
read -N 1 REPLY
echo
if test "$REPLY" = "y" -o "$REPLY" = "Y"; then
"$@"
else
echo "Cancelled by user"
fi
}
#Define/Declare below function in /etc/bashrc
#~ confirm userdel user_name
#We can show the message on the binaries in this way
confirm() {
echo -n "Do you want to run $*? [N/y] "
read -N 1 REPLY
echo
if test "$REPLY" = "y" -o "$REPLY" = "Y"; then
if [[ "$1" == "userdel" ]]; then
echo "Contact to Admin"
fi
else
echo "Cancelled by user"
fi
}