Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
soft:bash [2020/09/20 10:11] – Ce Zhang | soft:bash [2021/06/26 05:24] (current) – Ce Zhang | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ==== a string containinng character ==== | ||
+ | |||
+ | < | ||
+ | if echo " | ||
+ | echo ' | ||
+ | fi | ||
+ | |||
+ | string=' | ||
+ | |||
+ | if [[ $string =~ " | ||
+ | echo " | ||
+ | fi | ||
+ | |||
+ | string=' | ||
+ | if [[ $string == *"My long"* ]]; then | ||
+ | echo " | ||
+ | fi | ||
+ | |||
+ | [[ ! $VAR =~ [0-9] ]]; | ||
+ | |||
+ | # contains only alphabets and numbers | ||
+ | |||
+ | $VAR =~ ^[[: | ||
+ | |||
+ | # Or | ||
+ | |||
+ | $VAR =~ ^[0-9a-zA-Z]+$ | ||
+ | |||
+ | #!/bin/bash | ||
+ | |||
+ | while true; do | ||
+ | read -r -p "Enter a string: " VAR | ||
+ | if [[ $VAR =~ ^[[: | ||
+ | echo "OK: Contains alphabets and numbers" | ||
+ | else | ||
+ | echo "NOK: Contains special character" | ||
+ | fi | ||
+ | done | ||
+ | |||
+ | #!/bin/bash | ||
+ | |||
+ | while true; do | ||
+ | read -r -p "Enter a string: " VAR | ||
+ | if [[ $VAR =~ ^[0-9]+$ ]];then | ||
+ | echo "Input contains number" | ||
+ | else | ||
+ | echo "Input contains non numerical value" | ||
+ | fi | ||
+ | done | ||
+ | |||
+ | </ | ||
+ | |||
+ | reference: | ||
+ | - [[https:// | ||
+ | - [[https:// | ||
+ | |||
+ | ==== date command in bash ==== | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | < | ||
+ | #!/bin/bash | ||
+ | |||
+ | today = `date +%m-%d-%Y` | ||
+ | |||
+ | echo $today | ||
+ | </ | ||
+ | |||
+ | ==== Replace String in a File with the `sed` Command ==== | ||
+ | |||
+ | < | ||
+ | sed -i ' | ||
+ | </ | ||
+ | |||
==== Control and comparison==== | ==== Control and comparison==== | ||
< | < |