Home >Operation and Maintenance >Linux Operation and Maintenance >Classic Thirteen Questions about Shell Scripts You Should Know

Classic Thirteen Questions about Shell Scripts You Should Know

Linux中文社区
Linux中文社区forward
2023-08-03 14:50:011308browse

Classic Thirteen Questions about Shell Scripts You Should Know

  • #Classic Thirteen Questions about Shell

    • 1. Why is it called shell?

    • 2 . What is the relationship between Shell prompt (PS1) and Carriage Return (CR)?

    • 3. When someone else echoes, you also echo. How much do you know about echo?

    • 4. What is the difference between "" (double quotation mark) and (single quotation mark)?

    • 5. var=value? What is the difference between export and export?

    • 6. What is the difference between exec and source?

    • 7. What is the difference between ( ) and { }?

    • 8. () and ${}. What’s the difference?

    • 9. and *?

    • 10. What is the difference between && and ||?

    • 11. What is the difference between > and 0c4af0acd46919dd0ff35ca00a788b6e 与 c01b8772d0f6397290d6d9199e3ce834 来改变送出的数据信道(stdout, stderr),使之输出到指定的档案。

      ls my.file no.such.file 1> file.out 2>file.err
      # 2>&1 就是将stderr并进stdout做输出
      ls my.file no.such.file 1> file.out 2>&1
      # /dev/null 空
      ls my.file no.such.file >/dev/null 2>&1
      
      cat < file > file
      # 在 IO Redirection 中,stdout 与 stderr 的管道会先准备好,才会从 stdin 读进资料。 
      # 也就是说,在上例中,> file 会先将 file 清空,然后才读进 < file , 
      # 但这时候档案已经被清空了,因此就变成读不进任何数据了

      12. 你要if还是case呢?

      # if
      echo -n "Do you want to continue?(Yes/No):"
      read YN
      if [ "$YN"=Y -o "$YN"=y -o "$YN"="Yes" -o "$YN"="yes" -o "$YN"="YES"];then
      echo "continue"
      else
      exit 0
      fi
      
      # case
      echo -n "Do you want to continue?(Yes/No):"
      read YN
      case "$YN" in
      [Yy]|[Yy][Ee][Ss])
      echo "continue"
      ;;
      *)
      exit 0
      esac

      13. for what? while与until差在哪?

      # for
      for ((i=1;i<=10;i++))
      do
       echo "num is $i"
      done
      
      # while
      num=1
      while [ "$num" -le 10 ]; do
       echo "num is $num"
       num=$(($num + 1))
      done
      
      # until
      num=1
      until [ "$num" -gt 10 ]; do
       echo "num is $num"
       num=$(($nu + 1))
      done
      • break 是结束 loop
      • return 是结束 function
      • exit 是结束 script/shell

The above is the detailed content of Classic Thirteen Questions about Shell Scripts You Should Know. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Linux中文社区. If there is any infringement, please contact admin@php.cn delete