libbash.sh is a library of functions to easely write bash scripts.
Add libbash.sh to your script before using functions:
source "/path/to/libbash.sh"
Then call the functions described below.
If you want to use libbash.sh GUI, use the --gui
option as argument when loading libbash:
source "/path/to/libbash.sh" --gui
By default, libbash.sh translation is loaded in the user language. You can specify a language with the --lang
option:
source "/path/to/libbash.sh" --lang fr
Supported languages:
en
: English (default)fr
: French
Note: DO NOT USE variables or functions with lb_
prefix in your scripts
(nor lbg_
if you use libbash.sh GUI) as you could override or broke some libbash.sh features.
When you load libbash.sh, you can have the following return codes:
- 0: libbash.sh is loaded
- 1: libbash.sh file does not exists (in most cases), or is corrupted
- 2: cannot load libbash.sh GUI
- 3: cannot load translation file
- 4: some variables could not be initialized
- 5: cannot set a GUI interface (if GUI loaded)
You can use the following variables that are initialized when you include libbash.sh in your scripts (read only):
$lb_version
: the current libbash.sh version$lb_current_os
: your current Operating System (result oflb_current_os
function)$lb_current_hostname
: your current host name (result ofhostname
command)$lb_current_user
: your user name (result ofwhoami
command)$lb_current_path
: your current path (same aspwd
)$lb_path
: real path of libbash.sh$lb_directory
: libbash.sh directory real path$lb_current_script
: real path of your current script$lb_current_script_directory
: real directory path of your current script
You can use and modify the following variables in your scripts:
$lb_current_script_name
: name of your current script (same asbasename $0
by default)$lb_quietmode
: (boolean, false by default) if set totrue
, it will disable any display in console (including questions inlb_yesno
andlb_choose_option
)$lb_exitcode
: script exit code (integer, 0 by default) that will be send if usinglb_exit
(same asexit $lb_exitcode
)$lb_exit_cmd
: array that contains a command to execute whenlb_exit()
function is called (empty by default)
Be careful with functions with short options (e.g. lb_exit -q -f
): options must be called separately (e.g. lb_exit -qf
will not work).
Functions with a *
are not fully supported on every OS yet (may change in the future).
- Bash utilities
- Display
- Logs
- Configuration files
- Operations on variables
- Filesystem
- Files and directories
- System utilities
- User interaction
Check if a command exists. Works for commands, functions and executable files.
lb_command_exists COMMAND [COMMAND...]
- 0: Command(s) exists
- 1: Command(s) does not exists
if lb_command_exists supertux2 ; then
echo "You're ready to play to supertux!"
fi
Check if a function exists.
lb_function_exists FUNCTION [FUNCTION...]
- 0: Function(s) exists
- 1: Usage error
- 2: Function(s) does not exists
- 3: Command exists, but is not a function
print_hello() {
echo "Hello"
}
if lb_function_exists print_hello ; then
print_hello
fi
Test number of non-empty arguments passed to a script/function.
Note: A common usage of this function would be lb_test_arguments -ge 1 $*
to test if user has passed at least one argument to your script/function.
lb_test_arguments OPERATOR N [ARG...]
OPERATOR common bash comparison pattern: -eq|-ne|-lt|-le|-gt|-ge
N expected number to compare to
ARG your arguments; (e.g. $* without quotes)
- 0: Arguments OK
- 1: Usage error
- 2: Arguments not OK
if lb_test_arguments -lt 2 $* ; then
echo "You have to give at least 2 arguments to this script."
fi
Parse arguments and split concatenated options.
Combined with lb_getopt, you can create scripts with full options,
better than with the getopts
command.
Note: A common usage of this function is lb_getargs "$@"
to split arguments of your current script (see complete example below).
lb_getargs "$@"
Warning: put quotes to support spaces in arguments.
- 0: Arguments parsed
- 1: No arguments
# parse and get arguments
lb_getargs "$@" && set -- "${lb_getargs[@]}"
# if you called the current script with arguments:
# -rp /home
# now arguments ($@) are:
# -r -p /home
# get options
while [ $# -gt 0 ] ; do
case $1 in
-p|--path)
path=$(lb_getopt "$@")
if [ $? != 0 ] ; then
echo "Usage error: missing path"
exit 1
fi
shift # remove the value
;;
-r|--recursive)
recursive=true
;;
-*)
echo "Unknown option: $1"
exit 1
;;
*)
# not an option: stop parsing
break
;;
esac
shift # go the the next argument
done
# other arguments are available in $@ variable
Get value of an option.
lb_getopt "$@"
Warning: put quotes to support spaces in arguments.
- 0: Value returned
- 1: Option value is missing
See the complete example of the lb_getargs function above.
Run a command (optional) and exit script with a specified exit code.
lb_exit [OPTIONS] [EXIT_CODE]
-f, --forward-exitcode Forward exitcode from the exit command
(defined in the $lb_exit_cmd variable)
-q, --quiet Quiet mode (do not print exit command output)
EXIT_CODE Specify an exit code (if not set, $lb_exitcode will be used)
# print a message and exit script with code 42
lb_exit_cmd=(echo "So long and thanks for all the fish!")
lb_exitcode=42
lb_exit
Get the current display (verbose) level (or the id of a level).
See lb_set_log_level for more details on default display/log levels.
lb_get_display_level [OPTIONS] [LEVEL_NAME]
--id Get display level ID instead of its name
- 0: OK
- 1: Display level is not set
- 2: Display level not found
current_display_level=$(lb_get_display_level)
Set the display (verbose) level for messages using lb_display function.
lb_set_display_level LEVEL_NAME
Default display levels are the same as the log level. See lb_set_log_level for more details.
The default display level is set to maximum (DEBUG by default), which means that it will print all messages.
Please note that if you set a display level, every messages with a lower level will also be displayed.
If you display a message with an unknown display level, it will be displayed.
- 0: Display level set
- 1: Usage error
- 2: Specified display level not found
# set normal verbose mode
lb_set_display_level INFO
Print a message to the console, with colors and formatting
lb_print [OPTIONS] TEXT
or
lb_echo [OPTIONS] TEXT
For now, messages are not formatted in macOS terminal.
-n No line return after text
--bold Format text in bold
--cyan,
--green,
--yellow,
--red Format text with colours
Exit code of the echo
command.
lb_print --green "This is a green text."
Print a message to the console, with colors and formatting, redirected to stderr.
lb_error [OPTIONS] TEXT
See lb_print for usage.
lb_error --red "This is an error."
Print a message to the console, can set a verbose level and can append to logs.
If you use the --level MYLEVEL
option, the message will be displayed (and logged if option --log
is set)
only if MYLEVEL
is greater or equal to the current log level.
To set a log level, see lb_set_log_level.
To set a log file, see lb_set_logfile.
lb_display [OPTIONS] TEXT
-n No line return after text
-l, --level LEVEL Choose a display level (will be the same for logs)
-p, --prefix Print "[LOG_LEVEL] " prefix before text
--log Append text to log file if defined
- 0: OK
- 1: Usage error
- 2: Logs could not be written
- 3: Unknown error while printing
lb_display --log "This message you see will be stored in logs."
Shortcuts to display with common log levels.
It uses the lb_display
function with --prefix
and --level
options.
Available functions:
lb_display_critical
lb_display_error
lb_display_warning
(orlb_warning
)lb_display_info
(orlb_info
)lb_display_debug
(orlb_debug
)
lb_display_... [OPTIONS] TEXT
See lb_display for usage.
lb_display_critical "This is a critical error!"
Manage a command result code and print a label to the console to indicate if a command succeeded or failed.
lb_result [OPTIONS] [EXIT_CODE]
--ok-label LABEL Set a success label (default: OK)
--failed-label LABEL Set a failed label (default: FAILED)
--log Append text to log file
-l, --log-level LEVEL Choose a display level (will be the same for logs)
-s, --save-exitcode Save the result to the $lb_exitcode variable
-e, --error-exitcode CODE Set a custom code to the $lb_exitcode variable if error
-x, --exit-on-error Exit if result is not ok (exit code not to 0)
-q, --quiet Quiet mode, do not print anything (just follow result code)
EXIT_CODE Specify an exit code. If not set, variable $? will be used
Exit code forwarded of the command (beware that 1 could also mean an usage error).
echo "Processing..."
mycommand
lb_result
Print a short result label to the console to indicate if a command succeeded or failed.
It uses the lb_result
function with --ok-label [ OK ]
and --failed-label [ FAILED ]
options.
lb_short_result [OPTIONS] [EXIT_CODE]
--log Append text to log file
-l, --log-level LEVEL Choose a display level (will be the same for logs)
-s, --save-exitcode Save the result to the $lb_exitcode variable
-e, --error-exitcode CODE Set a custom code to the $lb_exitcode variable if error
-x, --exit-on-error Exit if result is not ok (exit code not to 0)
-q, --quiet Do not print anything
EXIT_CODE Specify an exit code. If not set, variable $? will be used
Exit code forwarded of the command (beware that 1 could also mean an usage error).
echo -n "Starting service... "
my_service &> /dev/null
lb_short_result
Return path of the defined log file.
To set a log file, see lb_set_logfile.
lb_get_logfile
- 0: OK
- 1: Log file is not set
- 2: Log file is not writable
logfile=$(lb_get_logfile)
Set path of the log file.
lb_set_logfile [OPTIONS] PATH
-a, --append Append to the file if exists
-x, --overwrite Overwrite file if exists
- 0: Log file set
- 1: Usage error
- 2: Log file cannot be created or is not writable
- 3: Log file already exists, but append option is not set
- 4: Path exists, but is not a regular file
lb_set_logfile /path/to/logfile.log
Get the current log level (or the id of a level).
See lb_set_log_level for more details on default log levels.
lb_get_log_level [OPTIONS] [LEVEL_NAME]
--id Get log level ID instead of its name
- 0: OK
- 1: Log level is not set
- 2: Log level not found
current_log_level=$(lb_get_log_level)
Set the log level for logging.
lb_set_log_level LEVEL_NAME
Default log levels are (from lower to higher):
-
- CRITICAL
-
- ERROR
-
- WARNING
-
- INFO
-
- DEBUG
The default log level is set to maximum (DEBUG by default), which means that it will print logs of every levels.
Please note that if you set a log level, every messages with a lower level will also be logged.
If you log a message with an unknown log level, it will always be logged.
- 0: Log level set
- 1: Usage error
- 2: Specified log level not found
# set normal logs
lb_set_log_level INFO
Print text into the log file.
If you use the --level MYLEVEL
option, the message will be logged
only if MYLEVEL
is greater or equal to the current log level.
To set a log level, see lb_set_log_level.
To set a log file, see lb_set_logfile.
lb_log [OPTIONS] TEXT
-n No line return after text
-l, --level LEVEL Choose a log level
-p, --prefix Print "[LOG_LEVEL] " prefix before text
-d, --date-prefix Print [date] prefix
-a, --all-prefix Print level and date prefixes
-x, --overwrite Clean log file before print text
- 0: OK
- 1: Log file is not set
- 2: Error while writing into file
lb_log "This line will be printed in the log file."
Shortcuts to log with common log levels.
It uses the lb_log
function with --prefix
and --level
options.
lb_log_... [OPTIONS] TEXT
See lb_log for usage.
lb_log_error "There was an error in your script!"
Read a config file and put each line that is not a comment or empty into the ${lb_read_config[@]}
array variable.
Config file definition:
- Simple text file
- Using
#
or;
at start of line as comment - INI files are required if using sections filter
lb_read_config [OPTIONS] PATH
-s, --section SECTION Read parameters only in the specified section(s)
- 0: File read
- 1: Usage error or file(s) does not exists
- 2: File exists but is not readable
- 3: Specified section was not found
lb_read_config my_config.conf
# print config lines
for ((i = 0 ; i < ${#lb_read_config[@]} ; i++)) ; do
echo "${lb_read_config[$i]}"
done
Import a config file and assign values to bash variables.
Config file definition:
- Simple text file
- Using
#
or;
at start of line as comment - INI files are required if using sections filter
- Values with spaces should have quotes like:
param = 'my value'
orparam = "my value"
- Lines that contains $ and ` characters are not imported to avoid shell injection. You can import them anyway with the
--unsecure
option (see below).
lb_import_config [OPTIONS] PATH
-s, --section SECTION Import parameters only in the specified section(s)
-e, --all-errors Return all errors in exit codes
-u, --unsecure Do not prevent shell injection (could be dangerous)
- 0: Configuration imported
- 1: Usage error or file(s) does not exists
- 2: One or more parameters were not imported, or specified section not found
- 3: One or more line has a bad syntax (if
--all-errors
option is enabled) - 4: One or more line contains shell commands or variables (if
--all-errors
option is enabled) - 5: File exists but is not readable
### content of my_config.conf
# my config file
hello_message="Hello dear users"
users = (John Mark)
### end content of my_config.conf
lb_import_config my_config.conf
# print bye message
echo "$hello_message ${users[@]}"
Get a parameter in a config file.
Config file definition:
- Simple text file
- Using
#
or;
at start of line as comment - INI files are required if using sections filter
lb_get_config [OPTIONS] FILE PARAM
-s, --section SECTION Get the parameter only in the specified section
- 0: Configuration file updated
- 1: Usage error or file(s) does not exists
- 2: Configuration file is not readable
- 3: Parameter not found
myoption=$(lb_get_config my_config.conf myoption)
Set a parameter in a config file.
Config file definition:
- Simple text file
- Using
#
or;
at start of line as comment - INI files are required if using sections filter
lb_set_config [OPTIONS] FILE PARAM VALUE
-s, --section SECTION Set the parameter only in the specified section
--strict Strict mode: do not insert parameter if it does not exists
- 0: Configuration file updated
- 1: Usage error or file(s) does not exists
- 2: Configuration file is not writable
- 3: Parameter does not exists (if strict mode)
- 4: Error in setting config
lb_set_config my_config.conf myoption "My value"
Test if a value is a number.
lb_is_number VALUE
- 0: Value is a number
- 1: Value is not a number
x="-42.9"
if lb_is_number $x ; then
echo "x is a number"
fi
Test if a value is a integer.
lb_is_integer VALUE
- 0: Value is an integer
- 1: Value is not an integer
x="-1"
if lb_is_integer $x ; then
echo "x is an integer"
fi
Test if a value is a boolean.
lb_is_boolean VALUE
- 0: Value is a boolean
- 1: Value is not a boolean
x=false
if lb_is_boolean $x ; then
echo "x is a boolean"
fi
Test if a string is a valid email address.
lb_is_email STRING
- 0: Is an email address
- 1: Is not an email address
x="[email protected]"
if lb_is_email $x ; then
echo "x is an email address"
fi
Test if a text is a comment.
In source codes, comments are preceded by a symbol like #
, //
, ...
lb_is_comment [OPTIONS] TEXT
-s, --symbol STRING Comment symbol (can use multiple values, '#' by default)
-n, --not-empty Empty text are not considered as comments
- 0: Text is a comment
- 1: Usage error
- 2: Text is not a comment
- 3: Text is empty (if
--not-empty
option is set)
# read config file without comments
while read line ; do
if ! lb_is_comment $line ; then
echo "$line"
fi
done < "config.sh"
Deletes spaces before and after a string.
lb_trim STRING
Exit code of the echo
command.
# get config line without spaces before and after text
config_line=" param='value with spaces' "
config=$(lb_trim "$config_line")
Split a string into array.
lb_split DELIMITER STRING
- 0: Split OK
- 1: Usage error
users="john,peter"
lb_split , "$users"
for u in "${lb_split[@]}" ; do
echo "User $u exists"
done
Join an array into string.
lb_join DELIMITER "${ARRAY[@]}"
Warning: put your array between quotes or search will fail if you have spaces in values.
- 0: Join OK
- 1: Usage error
users=(john peter)
echo "Users: $(lb_join ", " "${users[@]}")"
Check if an array contains a value.
Note: This function was called lb_array_contains()
and has been renamed in
version 1.9.0. You can still use the old name because there is an alias for
compatibility, but it is no longer recommended.
lb_in_array VALUE "${ARRAY[@]}"
Warning: put your array between quotes or search will fail if you have spaces in values.
- 0: Value is in array
- 1: Usage error
- 2: Value is NOT in array
array=(one two three)
if lb_in_array one "${array[@]}" ; then
echo "one is in array"
fi
Convert a date into a timestamp.
lb_date2timestamp [OPTIONS] DATE
Note: Date must be formatted in YYYY-MM-DD HH:MM:SS
-u, --utc Date and timestamp are using UTC
- 0: Date converted
- 1: Usage error
- 2: Conversion error; date may be invalid
timestamp=$(lb_date2timestamp '2017-12-31 23:59:59')
Convert a timestamp to a date.
lb_timestamp2date [OPTIONS] TIMESTAMP
-f, --format FORMAT Specify a date format to return
-u, --utc Timestamp and date are using UTC
Date formats: see the date
command help for available formats; do not put the
+
at the beginning.
- 0: Timestamp converted
- 1: Usage error
- 2: Conversion error; timestamp may be invalid
date=$(lb_timestamp2date -f '%Y-%m-%d %H:%M:%S' 1514764799)
Compare 2 software versions.
Versions must be in semantic versionning format (http://semver.org), but the function can support incomplete versions (e.g. 1.0 and 2 are converted to 1.0.0 and 2.0.0 respectively).
lb_compare_versions VERSION_1 OPERATOR VERSION_2
VERSION_1 Software version
OPERATOR Bash comparison pattern: -eq|-ne|-lt|-le|-gt|-ge
VERSION_2 Software version
- 0: Comparison OK
- 1: Usage error
- 2: Comparison NOT OK
version1="2.0.1"
version2="1.8.9"
if lb_compare_versions $version1 -ge $version2 ; then
echo "Newer version: $version1"
else
echo "Newer version: $version2"
fi
Give the filesystem type of a partition.
lb_df_fstype PATH
Note: PATH may be any folder/file (not only mount points) or a device path (e.g. /dev/sda1)
Results for each filesystem type:
- FAT16/FAT32:
- Linux/Windows:
vfat
- macOS:
msdos
- Linux/Windows:
- exFAT:
exfat
(for Linux systems without lsblk command, will returnfuseblk
) - NTFS:
ntfs
(for Linux systems without lsblk command, will returnfuseblk
) - HFS+:
- Linux:
hfsplus
- macOS:
hfs
- Windows: not supported
- Linux:
- ext2/ext3/ext4:
- Linux:
ext2
/ext3
/ext4
- macOS/Windows: not supported
- Linux:
- btrfs:
- Linux:
btrfs
- Linux:
- 0: OK
- 1: Usage error
- 2: Given PATH does not exists
- 3: Unknown error
root_fstype=$(lb_df_fstype /)
Get space left on partition in kilobytes (1KB = 1024 bytes).
lb_df_space_left PATH
Note: PATH may be any folder/file (not only mount points) or a device path (e.g. /dev/sda1)
- 0: OK
- 1: Usage error
- 2: Given PATH does not exists
- 3: Unknown error
space_left=$(lb_df_space_left /)
Get the mount point path of a partition.
lb_df_mountpoint PATH
Note: PATH may be any folder/file (not only mount points) or a device path (e.g. /dev/sda1)
- 0: OK
- 1: Usage error
- 2: Given PATH does not exists
- 3: Unknown error
mountpoint=$(lb_df_mountpoint /)
Get the disk UUID for a given path.
WARNING: This function is not supported yet on Windows.
lb_df_uuid PATH
Note: PATH may be any folder/file (not only mount points) or a device path (e.g. /dev/sda1)
- 0: OK
- 1: Usage error
- 2: Given PATH does not exists
- 3: Unknown error
- 4: Not supported
disk_uuid=$(lb_df_uuid /media/usbkey)
Get home path of an user.
lb_homepath [USER]
If USER not set, using current user.
- 0: OK
- 1: Home path not found
home=$(lb_homepath)
Test if a directory is empty.
Note: This function was called lb_dir_is_empty()
and has been renamed in
version 1.9.0. You can still use the old name because there is an alias for
compatibility, but it is no longer recommended.
lb_is_dir_empty PATH
- 0: Directory is empty
- 1: Given PATH is not a directory
- 2: Access rights error
- 3: Directory is not empty
# if directory is empty, delete it
if lb_is_dir_empty /empty/directory/ ; then
rmdir /empty/directory/
fi
Get the absolute path of a file or directory.
lb_abspath PATH
- 0: OK
- 1: Usage error
- 2: Cannot resolve path (parent directory does not exists)
abs_path=$(lb_abspath file.txt)
Get the real path of a file or directory.
- If the given path, it will return its absolute path.
- If the given path is a symbolic link, it will return the absolute path of the link target.
- If the given path has a parent directory that is a symbolic link, it will return the real absolute path.
- If a Windows path is given, it will be converted to Cygwin path
lb_realpath PATH
- 0: OK
- 1: Usage error
- 2: Path not found
real_path=$(lb_realpath /path/link_to_file)
Test if a path (file or directory) is writable.
On Windows, this may fails if you are not owner on a network share file/folder. We recommand you to do something like the following if you can be in this case:
if ! lb_is_writable "$logfile" ; then
if [ "$(lb_df_fstype "$(dirname "$logfile")")" != smbfs ] ; then
echo "File is not writable!"
fi
fi
lb_is_writable PATH
- 0: Is writable (exists or can be created)
- 1: Usage error
- 2: Exists but is not writable
- 3: Does not exists and parent directory is not writable
- 4: Does not exists and parent directory does not exists
# create file if pat his writable
if lb_is_writable /path/to/file ; then
touch /path/to/file
fi
Edit a file withe the sed -i
command.
lb_edit PATTERN PATH
Exit codes are forwarded from the sed
command.
See the sed manual for more information about them.
# replace a by b in a file
lb_edit 's/a/b/g' myfile.txt
Detect current operating system family.
lb_current_os
Available results:
- Linux
- macOS
- Windows
case $(lb_current_os) in
macOS)
echo "You are on a macOS system."
;;
Windows)
echo "It seems you are on cygwin on Windows!"
;;
*)
echo "You are on a Linux system."
;;
esac
Test if an user exists.
lb_user_exists USER [USER...]
- 0: User(s) exists
- 1: User(s) does not exists
if lb_user_exists darthvader ; then
echo "Darth Vader rules your computer!"
fi
Test if an user is member of a group.
lb_in_group GROUP [USER]
Note: if USER is not specified, current user is used
- 0: User is member of the group
- 1: Usage error
- 2: User is NOT member of the group
- 3: User does not exists
if lb_in_group empire ; then
echo "You are part of the empire."
fi
List users member of a group.
WARNING: This function is supported only on Linux systems.
lb_group_members GROUP
- 0: Members are returned
- 1: Usage error
- 2: Group does not exists
- 3: Not supported
# get system administrators
administrators=(lb_group_members adm)
Generate a random password.
lb_generate_password [SIZE]
SIZE Set the password size (16 by default; use value between 1 and 32)
- 0: OK
- 1: Usage error
- 2: Unknown command error
# generate a password of 12 characters
password=$(lb_generate_password 12)
Send an email.
You must have sendmail installed and a proper SMTP server or relay configured.
You can install the ssmtp
program (on Linux) to easely send emails via an existing account (like GMail or else).
WARNING: This function is not supported yet on Windows.
lb_email [OPTIONS] RECIPIENT[,RECIPIENT,...] MESSAGE
-s, --subject TEXT Email subject
-r, --reply-to EMAIL Email address to reply to
-c, --cc EMAIL[,EMAIL,...] Add email addresses in the CC field
-b, --bcc EMAIL[,EMAIL,...] Add email addresses in the BCC field
-a, --attachment PATH Add attachments to the email
--sender EMAIL Specify a sender email address
--html MESSAGE Send a HTML version of the TEXT
- 0: Email sent
- 1: Usage error (or attachment does not exists)
- 2: No program available to send email
- 3: Unknown error from the program sender
To avoid bugs, be sure that you have correctly set your message and HTML body between quotes.
For example, if you import content from a file, call it like this: "$(cat mail.txt)"
.
lb_email --subject "Test" [email protected] "Hello, this is an email!"
Ask a question to user to answer by yes or no.
lb_yesno [OPTIONS] TEXT
-y, --yes Set yes as default option
-c, --cancel Add a cancel option
--yes-label TEXT Label to use for "YES"
--no-label TEXT Label to use for "NO"
--cancel-label TEXT Label to use for cancel option
- 0: Yes
- 1: Usage error
- 2: No
- 3: Cancelled
if ! lb_yesno "Do you want to continue?" ; then
exit
fi
Ask user to choose one or multiple options.
The chosen IDs are set into the $lb_choose_option
(array) variable.
lb_choose_option [OPTIONS] CHOICE [CHOICE...]
-d, --default ID[,ID...] Option(s) to use by default (IDs starts to 1)
-m, --multiple Allow user to choose between multiple options
-l, --label TEXT Set a label question (default: "Choose an option:")
-c, --cancel-label TEXT Set a cancel label (default: c)
- 0: OK
- 1: Usage error
- 2: Cancelled
- 3: Bad choice
if lb_choose_option --label "Choose a planet:" --default 1 "Earth" "Jupiter" ; then
chosen_planet=$lb_choose_option
fi
if lb_choose_option --label "Choose valid countries:" --default 1,2 "France" "USA" "Neverland" ; then
chosen_countries=(${lb_choose_option[@]})
fi
Ask user to enter a text.
Input text is set into the $lb_input_text
variable.
lb_input_text [OPTIONS] QUESTION_TEXT
-d, --default TEXT Default text
-n No line return after question
- 0: OK
- 1: Usage error
- 2: User entered an empty text (cancelled)
if lb_input_text "Please enter your name:" ; then
user_name=$lb_input_text
fi
Ask user to enter a password.
Input password is set into the $lb_input_password
variable.
lb_input_password [OPTIONS] [QUESTION_TEXT]
-c, --confirm Ask user to confirm password
--confirm-label TEXT Set a label for the confirm question
-m, --min-size N Force password to have at least N characters
QUESTION_TEXT Set a label for the question
- 0: OK
- 1: Usage error
- 2: Password is empty (cancelled)
- 3: Passwords mismatch
- 4: Password is too short (if
--min-size
option is set)
# ask user password twice
if lb_input_password --confirm ; then
user_password=$lb_input_password
fi
libbash.sh is licensed under the MIT License. See LICENSE.md for the full license text.
Author: Jean Prunneaux http://jean.prunneaux.com