shell study

1、Exit Status
If the command executed successfully (or true), the value of $? is zero. If the command failed for some reason, $? will contain a positive integer between 1 and 255, inclusive.A failed command usually returns 1.

2、Testing an Expression
The test command evaluates many kinds of expressions,from file properties to integers to strings.
1)file tests,A file’s existence can be tested with -e (or the nonstandard -a). The type of file can be checked with -f for a regular file, -d for a directory, and -h or -L for a symbolic link. for example:
test -f /etc/fstab ## true if a regular file
test -h /etc/rc.local ## true if a symbolic link
[ -x "$HOME/bin/hw" ] ## true if you can execute the file
[[ -s $HOME/bin/hw ]] ## true if the file exists and is not empty

2)Comparisons between integers use the -eq, -ne, -gt,-lt,-ge,and -le operators.
below is a example:
$ test 1 -eq 1
$ echo $?

3) String Tests,The = operator tests for equality, in other words,whether they are identical;
!= tests for inequality.The -z and -n operators return successfully if their arguments are empty or nonempty.
here ara some example:
test "$a" = "$b"
[ "$q" != "$b" ]

$ [ -z "" ]
$ echo $?
0 #判空

$ test -n ""
$ echo $?
1

3、[[ ... ]]: Evaluate an Expression
4、(( ... )): Evaluate an Arithmetic Expression
5、Conditional Execution
example:
read name
if [[ -z $name ]]
then
echo "No name entered" >&2
exit 1 ## Set a failed return code
fi

read number
if (( number > 10 ))
then
printf "%d is too big\n" "$number" >&2
exit 1
else
printf "You entered %d\n" "$number"
fi

6、Conditional Operators, && and ||
Lists containing the AND and OR conditional operators are evaluated from left to right. A
command following the AND operator (&&) is executed if the previous command is
successful. The part following the OR operator (||) is executed if the previous command
fails.

7、case
语法如下:
case WORD in
PATTERN) COMMANDS ;;
PATTERN) COMMANDS ;; ## optional
esac

8、Loop
1)while语法:
while <list>
do
<list>
done
实例:
n=1
while [ $n -le 10 ]
do
echo "$n"
n=$(( $n + 1 ))
done

2)util 很少用,跟while刚好相反(循环会条件fails),实例如下,
n=1
until [ $n -gt 10 ]
do
echo "$n"
n=$(( $n + 1 ))
done

3)for,实例:
for (( n=1; n<=10; ++n ))
do
echo "$n"
done

4)break
do
read x
[ -z "$x" ] && break
done

5)continue
for n in {1..9} ## See Brace expansion in Chapter 4
do
x=$RANDOM
[ $x -le 20000 ] && continue
echo "n=$n x=$x"
done

最新文章

  1. python中转义用法 r&#39;&#39;
  2. Linux Shell ---系统命令(1)
  3. Seismic Unix的一些历史
  4. lib和dll的例子
  5. Centos7最小化安装后(minimal)安装图形界面
  6. [LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
  7. python学习之路-1 python简介及安装方法
  8. 【Linux学习三】Linux系统目录架构
  9. ARouter基础使用(一)
  10. Dubbo(二) —— dubbo配置
  11. 增加cookie和表单提交的安全
  12. Spring-boot之 swagger2
  13. Window环境下,PHP调用Python脚本
  14. 移动端页面滑动时候警告:Unable to preventDefault inside passive event listener due to target being treated as passive.
  15. Maven-pom.xml文件报错 Plugin execution not covered by lifecycle configuration
  16. HDU 2061 Treasure the new start, freshmen!
  17. 〖Android〗/system/etc/fallback_fonts.xml
  18. php入门(三)
  19. 【Leetcode 338】 Counting Bits
  20. 使用SQL命令查看MYSQL数据库大小

热门文章

  1. 用WAR的方式部署SpringBoot项目
  2. Oracle书籍资料链接——更新ing
  3. 换个思维,boot结合vue做后台管理
  4. Mac 终端 Termial 高亮配置(PS1变量配置)
  5. spring3: helloword
  6. pipenv安装库使用国内镜像
  7. pip使用国内镜像解决安装超时
  8. 通过使用scrapy爬取某学校全网
  9. Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem
  10. LeetCode OJ:Merge Sorted Array(合并排序的数组)