Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
soft:bash [2020/09/20 06:52] – created Ce Zhangsoft:bash [2021/06/26 05:24] (current) Ce Zhang
Line 1: Line 1:
 +==== a string containinng character ====
 +
 +<code>
 +if echo "${word}" | grep '[0-9]' >/dev/null; then
 +    echo 'Invalid input'
 +fi
 +
 +string='My string';
 +
 +if [[ $string =~ "My" ]]; then
 +   echo "It's there!"
 +fi
 +
 +string='My long string'
 +if [[ $string == *"My long"* ]]; then
 +  echo "It's there!"
 +fi
 +
 +[[ ! $VAR =~ [0-9] ]];
 +
 +# contains only alphabets and numbers
 +
 +$VAR =~ ^[[:alnum:]]+$
 +
 +# Or
 +
 +$VAR =~ ^[0-9a-zA-Z]+$
 +
 +#!/bin/bash
 +
 +while true; do
 +   read -r -p "Enter a string: " VAR
 +   if [[ $VAR =~ ^[[:alnum:]]+$ ]];then
 +      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
 +
 +</code>
 +
 +reference: 
 +  - [[https://www.golinuxcloud.com/check-if-string-contains-numbers-shell-script/#5_Check_if_string_contains_special_characters| link1]]
 +  - [[https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php|if statement]]
 +
 +==== date command in bash ====
 +
 +[[https://linuxhint.com/date-command-bash/|reference]]
 +
 +<code>
 +#!/bin/bash
 +
 +today = `date +%m-%d-%Y`
 +
 +echo $today
 +</code>
 +
 +==== Replace String in a File with the `sed` Command ====
 +
 +<code>
 +sed -i 's/search_string/replace_string/' filename
 +</code>
 +
 ==== Control and comparison==== ==== Control and comparison====
 <code> <code>
Line 6: Line 80:
 小于或等于 -le (less than or equal) 小于或等于 -le (less than or equal)
 不相等 -ne (not equal) 不相等 -ne (not equal)
 +
 +if (( $(echo "$num1 > $num2" |bc -l) )); then
 +  …
 +fi
  
 ############### and ############### and