Last active
April 8, 2020 07:45
-
-
Save hlapidez/0f2e40cc66f8aa351d9a1a9ee2e2ce39 to your computer and use it in GitHub Desktop.
collection of bash constructs like tests and if statements and stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ---------------------------------------------------------------------------- | |
| # 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" | |
| # 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" | |
| # longer version with if | |
| input="xBus" | |
| if [[ $input = B* ]] | |
| then | |
| echo "Start with B" | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment