|
| 1 | +#!/usr/bin/env bash |
| 2 | +# This script was generated by bashly (https://github.com/DannyBen/bashly) |
| 3 | +# Modifying it manually is not recommended |
| 4 | + |
| 5 | +# :command.root_command |
| 6 | +root_command() { |
| 7 | + # :src/root_command.sh |
| 8 | + file=${args[filename]} |
| 9 | + |
| 10 | + if is existing "$file" ; then |
| 11 | + echo "File exists" |
| 12 | + |
| 13 | + if is writeable "$file" ; then |
| 14 | + echo "... and is writeable" |
| 15 | + else |
| 16 | + echo "... and is NOT writeable" |
| 17 | + fi |
| 18 | + |
| 19 | + else |
| 20 | + echo "File does not exist" |
| 21 | + |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | +# :command.version_command |
| 26 | +version_command() { |
| 27 | + echo "$version" |
| 28 | +} |
| 29 | + |
| 30 | +# :command.usage |
| 31 | +exist_usage() { |
| 32 | + if [[ -n $long_usage ]]; then |
| 33 | + printf "exist - Sample application that uses the is.sh functions\n" |
| 34 | + echo |
| 35 | + else |
| 36 | + printf "exist - Sample application that uses the is.sh functions\n" |
| 37 | + echo |
| 38 | + fi |
| 39 | + |
| 40 | + printf "Usage:\n" |
| 41 | + printf " exist FILENAME\n" |
| 42 | + printf " exist --help | -h\n" |
| 43 | + printf " exist --version | -v\n" |
| 44 | + echo |
| 45 | + |
| 46 | + if [[ -n $long_usage ]]; then |
| 47 | + printf "Options:\n" |
| 48 | + # :command.usage_fixed_flags |
| 49 | + echo " --help, -h" |
| 50 | + printf " Show this help\n" |
| 51 | + echo |
| 52 | + echo " --version, -v" |
| 53 | + printf " Show version number\n" |
| 54 | + echo |
| 55 | + |
| 56 | + # :command.usage_args |
| 57 | + printf "Arguments:\n" |
| 58 | + |
| 59 | + # :argument.usage |
| 60 | + echo " FILENAME" |
| 61 | + printf " The file to check if exists\n" |
| 62 | + echo |
| 63 | + |
| 64 | + # :command.usage_examples |
| 65 | + printf "Examples:\n" |
| 66 | + |
| 67 | + printf " exist somefile\n" |
| 68 | + echo |
| 69 | + |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +# :command.inspect_args |
| 74 | +inspect_args() { |
| 75 | + echo args: |
| 76 | + for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done |
| 77 | +} |
| 78 | + |
| 79 | +# :command.user_lib |
| 80 | +# :src/lib/is.sh |
| 81 | +# is.sh copyright notice: |
| 82 | +# Copyright (c) 2016 Józef Sokołowski |
| 83 | +# Distributed under the MIT License |
| 84 | +# For most current version checkout repository: |
| 85 | +# https://github.com/qzb/is.sh |
| 86 | +is() { |
| 87 | + if [ "$1" == "--help" ]; then |
| 88 | + cat << EOF |
| 89 | +Conditions: |
| 90 | + is equal VALUE_A VALUE_B |
| 91 | + is matching REGEXP VALUE |
| 92 | + is substring VALUE_A VALUE_B |
| 93 | + is empty VALUE |
| 94 | + is number VALUE |
| 95 | + is gt NUMBER_A NUMBER_B |
| 96 | + is lt NUMBER_A NUMBER_B |
| 97 | + is ge NUMBER_A NUMBER_B |
| 98 | + is le NUMBER_A NUMBER_B |
| 99 | + is file PATH |
| 100 | + is dir PATH |
| 101 | + is link PATH |
| 102 | + is existing PATH |
| 103 | + is readable PATH |
| 104 | + is writeable PATH |
| 105 | + is executable PATH |
| 106 | + is available COMMAND |
| 107 | + is older PATH_A PATH_B |
| 108 | + is newer PATH_A PATH_B |
| 109 | + is true VALUE |
| 110 | + is false VALUE |
| 111 | +
|
| 112 | +Negation: |
| 113 | + is not equal VALUE_A VALUE_B |
| 114 | +
|
| 115 | +Optional article: |
| 116 | + is not a number VALUE |
| 117 | + is an existing PATH |
| 118 | + is the file PATH |
| 119 | +EOF |
| 120 | + exit |
| 121 | + fi |
| 122 | + |
| 123 | + if [ "$1" == "--version" ]; then |
| 124 | + echo "is.sh 1.1.0" |
| 125 | + exit |
| 126 | + fi |
| 127 | + |
| 128 | + local condition="$1" |
| 129 | + local value_a="$2" |
| 130 | + local value_b="$3" |
| 131 | + |
| 132 | + if [ "$condition" == "not" ]; then |
| 133 | + shift 1 |
| 134 | + ! is "${@}" |
| 135 | + return $? |
| 136 | + fi |
| 137 | + |
| 138 | + if [ "$condition" == "a" ] || [ "$condition" == "an" ] || [ "$condition" == "the" ]; then |
| 139 | + shift 1 |
| 140 | + is "${@}" |
| 141 | + return $? |
| 142 | + fi |
| 143 | + |
| 144 | + case "$condition" in |
| 145 | + file) |
| 146 | + [ -f "$value_a" ]; return $?;; |
| 147 | + dir|directory) |
| 148 | + [ -d "$value_a" ]; return $?;; |
| 149 | + link|symlink) |
| 150 | + [ -L "$value_a" ]; return $?;; |
| 151 | + existent|existing|exist|exists) |
| 152 | + [ -e "$value_a" ]; return $?;; |
| 153 | + readable) |
| 154 | + [ -r "$value_a" ]; return $?;; |
| 155 | + writeable) |
| 156 | + [ -w "$value_a" ]; return $?;; |
| 157 | + executable) |
| 158 | + [ -x "$value_a" ]; return $?;; |
| 159 | + available|installed) |
| 160 | + which "$value_a"; return $?;; |
| 161 | + empty) |
| 162 | + [ -z "$value_a" ]; return $?;; |
| 163 | + number) |
| 164 | + echo "$value_a" | grep -E '^[0-9]+(\.[0-9]+)?$'; return $?;; |
| 165 | + older) |
| 166 | + [ "$value_a" -ot "$value_b" ]; return $?;; |
| 167 | + newer) |
| 168 | + [ "$value_a" -nt "$value_b" ]; return $?;; |
| 169 | + gt) |
| 170 | + is not a number "$value_a" && return 1; |
| 171 | + is not a number "$value_b" && return 1; |
| 172 | + awk "BEGIN {exit $value_a > $value_b ? 0 : 1}"; return $?;; |
| 173 | + lt) |
| 174 | + is not a number "$value_a" && return 1; |
| 175 | + is not a number "$value_b" && return 1; |
| 176 | + awk "BEGIN {exit $value_a < $value_b ? 0 : 1}"; return $?;; |
| 177 | + ge) |
| 178 | + is not a number "$value_a" && return 1; |
| 179 | + is not a number "$value_b" && return 1; |
| 180 | + awk "BEGIN {exit $value_a >= $value_b ? 0 : 1}"; return $?;; |
| 181 | + le) |
| 182 | + is not a number "$value_a" && return 1; |
| 183 | + is not a number "$value_b" && return 1; |
| 184 | + awk "BEGIN {exit $value_a <= $value_b ? 0 : 1}"; return $?;; |
| 185 | + eq|equal) |
| 186 | + [ "$value_a" = "$value_b" ] && return 0; |
| 187 | + is not a number "$value_a" && return 1; |
| 188 | + is not a number "$value_b" && return 1; |
| 189 | + awk "BEGIN {exit $value_a == $value_b ? 0 : 1}"; return $?;; |
| 190 | + match|matching) |
| 191 | + echo "$value_b" | grep -xE "$value_a"; return $?;; |
| 192 | + substr|substring) |
| 193 | + echo "$value_b" | grep -F "$value_a"; return $?;; |
| 194 | + true) |
| 195 | + [ "$value_a" == true ] || [ "$value_a" == 0 ]; return $?;; |
| 196 | + false) |
| 197 | + [ "$value_a" != true ] && [ "$value_a" != 0 ]; return $?;; |
| 198 | + esac > /dev/null |
| 199 | + |
| 200 | + return 1 |
| 201 | +} |
| 202 | + |
| 203 | +# :command.command_functions |
| 204 | + |
| 205 | +# :command.parse_requirements |
| 206 | +parse_requirements() { |
| 207 | + # :command.fixed_flag_filter |
| 208 | + case "$1" in |
| 209 | + --version | -v ) |
| 210 | + version_command |
| 211 | + exit |
| 212 | + ;; |
| 213 | + |
| 214 | + --help | -h ) |
| 215 | + long_usage=yes |
| 216 | + exist_usage |
| 217 | + exit 1 |
| 218 | + ;; |
| 219 | + |
| 220 | + esac |
| 221 | + # :command.environment_variables_filter |
| 222 | + # :command.dependencies_filter |
| 223 | + # :command.command_filter |
| 224 | + action="root" |
| 225 | + # :command.required_args_filter |
| 226 | + if [[ $1 && $1 != -* ]]; then |
| 227 | + args[filename]=$1 |
| 228 | + shift |
| 229 | + else |
| 230 | + printf "missing required argument: FILENAME\nusage: exist FILENAME\n" |
| 231 | + exit 1 |
| 232 | + fi |
| 233 | + # :command.required_flags_filter |
| 234 | + # :command.parse_requirements_while |
| 235 | + while [[ $# -gt 0 ]]; do |
| 236 | + key="$1" |
| 237 | + case "$key" in |
| 238 | + |
| 239 | + -* ) |
| 240 | + printf "invalid option: %s\n" "$key" |
| 241 | + exit 1 |
| 242 | + ;; |
| 243 | + |
| 244 | + * ) |
| 245 | + # :command.parse_requirements_case |
| 246 | + if [[ ! ${args[filename]} ]]; then |
| 247 | + args[filename]=$1 |
| 248 | + shift |
| 249 | + else |
| 250 | + printf "invalid argument: %s\n" "$key" |
| 251 | + exit 1 |
| 252 | + fi |
| 253 | + ;; |
| 254 | + |
| 255 | + esac |
| 256 | + done |
| 257 | + # :command.default_assignments |
| 258 | +} |
| 259 | + |
| 260 | +# :command.initialize |
| 261 | +initialize() { |
| 262 | + version="0.1.0" |
| 263 | + long_usage='' |
| 264 | + set -e |
| 265 | + |
| 266 | + # :src/initialize.sh |
| 267 | + # Code here runs inside the initialize() function |
| 268 | + # Use it for anything that you need to run before any other function, like |
| 269 | + # setting environment vairables: |
| 270 | + # CONFIG_FILE=settings.ini |
| 271 | + # |
| 272 | + # Feel free to empty (but not delete) this file. |
| 273 | +} |
| 274 | + |
| 275 | +# :command.run |
| 276 | +run() { |
| 277 | + declare -A args |
| 278 | + parse_requirements "$@" |
| 279 | + |
| 280 | + if [[ ${args[--version]} ]]; then |
| 281 | + version_command |
| 282 | + elif [[ ${args[--help]} ]]; then |
| 283 | + long_usage=yes |
| 284 | + exist_usage |
| 285 | + elif [[ $action == "root" ]]; then |
| 286 | + root_command |
| 287 | + fi |
| 288 | +} |
| 289 | + |
| 290 | +initialize |
| 291 | +run "$@" |
0 commit comments