checks A library of checks.
lb_command_exists
command
Return 0
if command
exists.
Parameters: Name Type Description Default command
string
The command to check for required
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
lb_file_exists
file
Return 0
if file
exists.
Parameters: Name Type Description Default file
string
The file to check for required
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
lb_dir_exists
dir
Return 0
if dir
exists.
Parameters: Name Type Description Default dir
string
The directory to check for required
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
lb_is_null
var
Return 0
if var
is null.
Parameters: Name Type Description Default var
string
The var to check for required
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
lb_is_int
var
Return 0
if a var
is an integer.
Parameters: Name Type Description Default var
string
The var to check for required
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
lb_is_float
var
Return 0
if a var
is a float.
Parameters: Name Type Description Default var
string
The var to check for required
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
Note
A decimal point with no leading number, such as 1.
, returns 1
.
lb_is_bool
var
Return 0
if a var
is a boolean.
Parameters: Name Type Description Default var
string
The var to check for required
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
Note
1
is returned if the var
is blank or upper case (TRUE
/FALSE
)
lb_contains
var
substring
Return 0
if a var
contains substring
.
Parameters: Name Type Description Default var
string
The var to check in for substring
required substring
string
The substring to check for in var
required
string
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
array
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
Note
var
can be both a string
or an array
. When checking in an array
, use the ${arr[*]}
parameter expansion.
lb_starts_with
var
substring
Return 0
if a var
starts with substring
.
Parameters: Name Type Description Default var
string
The var to check at teh start for substring
required substring
string
The substring to check for at the start of var
required
string
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
array
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
Note
var
can be both a string
or an array
. When checking in an array
, use the ${arr[*]}
parameter expansion. If var
is an array, the function checks if the first element is substring
.
lb_ends_with
var
substring
Return 0
if a var
ends with substring
.
Parameters: Name Type Description Default var
string
The var to check the end for substring
required substring
string
The substring to check for at the end of var
required
string
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
array
Examples: #!/usr/bin/env bash
# shellcheck source=/dev/null
source ../init
source " ${ LIBBASH_DIR } /checks"
function check_command(){
if lb_command_exists " ${ 1 } " ; then
printf '"%s" command exists\n' " ${ 1 } "
else
printf '"%s" command does not exist\n' " ${ 1 } "
fi
}
function check_file(){
if lb_file_exists " ${ 1 } " ; then
printf '"%s" file exists\n' " ${ 1 } "
else
printf '"%s" file does not exist\n' " ${ 1 } "
fi
}
function check_dir(){
if lb_dir_exists " ${ 1 } " ; then
printf '"%s" dir exists\n' " ${ 1 } "
else
printf '"%s" dir does not exist\n' " ${ 1 } "
fi
}
function check_var(){
if lb_is_null " ${ 1 } " ; then
printf '"%s" var is null\n' "FOO"
else
printf '"%s" var is not null\n' "LIBBASH_DIR"
fi
}
function check_int(){
if lb_is_int " ${ 1 } " ; then
printf '"%s" is an int\n' " ${ 1 } "
else
printf '"%s" is not an int\n' " ${ 1 } "
fi
}
function check_float(){
if lb_is_float " ${ 1 } " ; then
printf '"%s" is a float\n' " ${ 1 } "
else
printf '"%s" is not a float\n' " ${ 1 } "
fi
}
function check_bool(){
if lb_is_bool " ${ 1 } " ; then
printf '"%s" is a bool\n' " ${ 1 } "
else
printf '"%s" is not a bool\n' " ${ 1 } "
fi
}
function check_contain(){
if lb_contains " ${ 1 } " " ${ 2 } " ; then
printf '"%s" contains "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not contain "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_starts_with(){
if lb_starts_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" starts with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not start with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_ends_with(){
if lb_ends_with " ${ 1 } " " ${ 2 } " ; then
printf '"%s" ends with "%s"\n' " ${ 1 } " " ${ 2 } "
else
printf '"%s" does not end with "%s"\n' " ${ 1 } " " ${ 2 } "
fi
}
function check_commands(){
check_command "foo"
check_command "ls"
}
function check_files(){
check_file " ${ LIBBASH_DIR } /foo"
check_file " ${ LIBBASH_DIR } /README.md"
}
function check_dirs(){
check_dir " ${ LIBBASH_DIR } /foo"
check_dir " ${ LIBBASH_DIR } "
}
function check_vars(){
FOO = ""
check_var " ${ FOO } "
check_var " ${ LIBBASH_DIR } "
}
function check_ints(){
check_int 1
check_int 11
check_int -1
check_int -11
check_int 1 .1
check_int 12 .12
check_int "foo"
}
function check_floats(){
check_float 1
check_float 12
check_float -1
check_float -12
check_float 12 .12
check_float -12.12
check_float .12
check_float 0 .12
check_float -.12
check_float 1 .
check_float "foo"
}
function check_bools(){
check_bool 0
check_bool 1
check_bool 2
check_bool -0
check_bool -1
check_bool true
check_bool TRUE
check_bool false
check_bool FALSE
check_bool "true"
check_bool "false"
check_bool "foo"
check_bool ""
}
function check_str_contains(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_contain " ${ string } " "foo"
check_contain " ${ string } " "bar"
check_contain " ${ string } " "baz"
}
function check_arr_contains(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_contain " ${ arr [*] } " "foo"
check_contain " ${ arr [*] } " "bar"
check_contain " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "baz"
}
function check_str_starts_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_starts_with " ${ string } " "foo"
check_starts_with " ${ string } " "bar"
}
function check_arr_starts_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_starts_with " ${ arr [*] } " "foo"
check_starts_with " ${ arr [*] } " "bar"
}
function check_str_ends_with(){
string = "foo bar"
printf "str: %s\n" " ${ string } "
check_ends_with " ${ string } " "foo"
check_ends_with " ${ string } " "bar"
}
function check_arr_ends_with(){
arr =( "foo" "bar" )
printf "arr: %s\n" " ${ arr [*] } "
check_ends_with " ${ arr [*] } " "foo"
check_ends_with " ${ arr [*] } " "bar"
}
function main() {
check_commands
check_files
check_dirs
check_vars
check_ints
check_floats
check_bools
check_str_contains
check_arr_contains
check_str_starts_with
check_arr_starts_with
check_str_ends_with
check_arr_ends_with
}
main " ${ @ } "
Show source code in checks
#!/usr/bin/env bash
: " ${ LIBBASH_DIR :?LIBBASH_DIR must be set. Please source libbash/init before other libraries. } "
#------------------------------ Global Variables ------------------------------
#------------------------------ Private Functions -----------------------------
#------------------------------ Public Functions ------------------------------
# Check if command exists
function lb_command_exists(){
lb_check_args_num " ${ @ } " 1
command -v " ${ 1 } " & > /dev/null
}
# Check if file exists
function lb_file_exists(){
lb_check_args_num " ${ @ } " 1
[ -f " ${ 1 } " ]
}
# Check is variable is null
function lb_is_null {
lb_check_args_num " ${ @ } " 1
[ -z " ${ 1 } " ]
}
# Checkifdir exists
function lb_dir_exists() {
lb_check_args_num " ${ @ } " 1
[ -d " ${ 1 } " ]
}
function lb_is_int() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/2210386/1061279
[[ " ${ 1 } " = ~ ^-?[ 0 -9] +$ ]]
}
function lb_is_float() {
lb_check_args_num " ${ @ } " 1
# https://stackoverflow.com/a/12643073/1061279
[[ " ${ 1 } " = ~ ^[ +-] ?([ 0 -9] *[ .]) ?[ 0 -9] +$ ]]
}
function lb_is_bool() {
lb_check_args_num " ${ @ } " 1
case ${ 1 } in
"true" | 1 | 0 | "false" ) return 0 ;;
*) return 1 ;;
esac
}
function lb_contains(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " * ]]
}
function lb_starts_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == " ${ 2 } " * ]]
}
function lb_ends_with(){
lb_check_args_num " ${ @ } " 2
[[ " ${ 1 } " == *" ${ 2 } " ]]
}
Note
var
can be both a string
or an array
. When checking in an array
, use the ${arr[*]}
parameter expansion. If var
is an array, the function checks if the last element is substring
.