Skip to content

Instantly share code, notes, and snippets.

@hlapidez
Last active April 8, 2020 07:45
Show Gist options
  • Select an option

  • Save hlapidez/0f2e40cc66f8aa351d9a1a9ee2e2ce39 to your computer and use it in GitHub Desktop.

Select an option

Save hlapidez/0f2e40cc66f8aa351d9a1a9ee2e2ce39 to your computer and use it in GitHub Desktop.

Revisions

  1. hlapidez revised this gist Apr 8, 2020. 1 changed file with 18 additions and 2 deletions.
    20 changes: 18 additions & 2 deletions bash_constructs.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@

    # ----------------------------------------------------------------------------
    # How to check if a string begins with some value in bash
    #
    #
    # more cool examples here: https://www.cyberciti.biz/faq/bash-check-if-string-starts-with-character-such-as/
    # ----------------------------------------------------------------------------
    #
    # Let us define a shell variable called vech as follows:
    vech="Bus"
    @@ -23,3 +24,18 @@ then
    else
    echo "No match"
    fi

    # ----------------------------------------------------------------------------
    # Check number of arguments passed to a Bash script
    #
    # https://stackoverflow.com/questions/18568706/check-number-of-arguments-passed-to-a-bash-script
    # ----------------------------------------------------------------------------

    if [ "$#" -ne 1 ]; then
    echo "Illegal number of parameters"
    fi
    # Or

    if test "$#" -ne 1; then
    echo "Illegal number of parameters"
    fi
  2. hlapidez revised this gist Apr 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bash_constructs.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@

    # How to check if a string begins with some value in bash
    #
    # source: https://www.cyberciti.biz/faq/bash-check-if-string-starts-with-character-such-as/
    # more cool examples here: https://www.cyberciti.biz/faq/bash-check-if-string-starts-with-character-such-as/
    #
    # Let us define a shell variable called vech as follows:
    vech="Bus"
  3. hlapidez revised this gist Apr 8, 2020. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion bash_constructs.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@

    # How to check if a string begins with some value in bash
    #
    # source: https://www.cyberciti.biz/faq/bash-check-if-string-starts-with-character-such-as/
    #
    # Let us define a shell variable called vech as follows:
    @@ -11,4 +12,14 @@ vech="Bus"
    # The [[ used to execute the conditional command. It checks if $vech starts with “B” followed by any character. Set vech to something else and try it again:
    vech="Car"
    [[ $vech = B* ]] && echo "Start with B"
    [[ $vech = B* ]] && echo "Start with B" || echo "Not matched"
    [[ $vech = B* ]] && echo "Start with B" || echo "Not matched"

    # longer version with if
    input="xBus"

    if [[ $input = B* ]]
    then
    echo "Start with B"
    else
    echo "No match"
    fi
  4. hlapidez revised this gist Apr 8, 2020. No changes.
  5. hlapidez created this gist Apr 8, 2020.
    14 changes: 14 additions & 0 deletions bash_constructs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@

    # How to check if a string begins with some value in bash
    # source: https://www.cyberciti.biz/faq/bash-check-if-string-starts-with-character-such-as/
    #
    # Let us define a shell variable called vech as follows:
    vech="Bus"

    # To check if string “Bus” stored in $vech starts with “B”, run:
    [[ $vech = B* ]] && echo "Start with B"

    # The [[ used to execute the conditional command. It checks if $vech starts with “B” followed by any character. Set vech to something else and try it again:
    vech="Car"
    [[ $vech = B* ]] && echo "Start with B"
    [[ $vech = B* ]] && echo "Start with B" || echo "Not matched"