Shell学习笔记-基本点

来源:互联网 发布:ipad登qq游戏网络异常 编辑:程序博客网 时间:2024/04/30 03:04

1.echo

“  双引号引用的字符串,可以引用出了  $ \ ` 之外的任意字符。其中 对于 \ 转义字符必须加  -e 选项才会进行解释转义,否则默认不转...

‘  单引号则屏蔽一切转义字符

-n 去掉要打印字符串的换行。


2.重定向

command > filename 重定向输出

command >> filename 追加输出


command 1 > filename  命令的标准输出 重定向到 文件

command 2 > filename  命令的标准错误输出 重定向到 文件


command > filename  2>&1 命令的标准输出和错误输出都 重定向到 文件,意思就是  1和2 都输出到文件

command >> filename  2>&1 命令的标准输出和错误输出都 重定向到 文件,意思就是  1和2 都输出到文件,与上边的区别就是追加


command < filename 文件内容作为标准输入

command << delimiter 从标准输入中读入,直到遇见 delimiter分界符为止,意思就是输入串很长,但是真正的输入就是 这个 分界符之前的内容


command < filename1 > filename2  一个是输入一个是输出


command <&m 文件描述符m对应的文件作为输入

command >&m 文件描述符m对应的文件作为输出

command <&-   关闭该命令的标准输入


>&m  或者 <&m  m为文件描述符,也是重定向,把m作为输入或者输出....这个运算符前后的参数  都可以理解为 文件描述符,其实 command时也可以理解为 标准输出1...

      其实就是文件描述符对应的文件内容之间的重定向...


3.test
       test EXPRESSION
       [ EXPRESSION ]

       ( EXPRESSION )
              EXPRESSION is true
       ! EXPRESSION
              EXPRESSION is false
       EXPRESSION1 -a EXPRESSION2
              both EXPRESSION1 and EXPRESSION2 are true
       EXPRESSION1 -o EXPRESSION2
              either EXPRESSION1 or EXPRESSION2 is true
       -n STRING
              the length of STRING is nonzero
       STRING equivalent to -n STRING
       -z STRING
              the length of STRING is zero
       STRING1 = STRING2
              the strings are equal
       STRING1 != STRING2
              the strings are not equal
       INTEGER1 -eq INTEGER2
              INTEGER1 is equal to INTEGER2
       INTEGER1 -ge INTEGER2
              INTEGER1 is greater than or equal to INTEGER2
       INTEGER1 -gt INTEGER2
              INTEGER1 is greater than INTEGER2
       INTEGER1 -le INTEGER2
              INTEGER1 is less than or equal to INTEGER2
       INTEGER1 -lt INTEGER2
              INTEGER1 is less than INTEGER2
       INTEGER1 -ne INTEGER2
              INTEGER1 is not equal to INTEGER2
       FILE1 -ef FILE2
              FILE1 and FILE2 have the same device and inode numbers
       FILE1 -nt FILE2
              FILE1 is newer (modification date) than FILE2
       FILE1 -ot FILE2
              FILE1 is older than FILE2
       -b FILE
              FILE exists and is block special
       -c FILE
              FILE exists and is character special
       -d FILE
              FILE exists and is a directory
       -e FILE
              FILE exists
       -f FILE
              FILE exists and is a regular file
       -g FILE
              FILE exists and is set-group-ID
       -G FILE
              FILE exists and is owned by the effective group ID
       -h FILE
              FILE exists and is a symbolic link (same as -L)
       -k FILE
              FILE exists and has its sticky bit set
       -L FILE
              FILE exists and is a symbolic link (same as -h)
       -O FILE
              FILE exists and is owned by the effective user ID
       -p FILE
              FILE exists and is a named pipe
       -r FILE
              FILE exists and read permission is granted
       -s FILE
              FILE exists and has a size greater than zero
       -S FILE
              FILE exists and is a socket
       -t FD  file descriptor FD is opened on a terminal
       -u FILE
              FILE exists and its set-user-ID bit is set
       -w FILE
              FILE exists and write permission is granted
       -x FILE
              FILE exists and execute (or search) permission is granted

4.各种括号总结

[  $var1 - gt $var2 ] 判断条件  和 test 的意义相同

${Var} 取变量的值,和$var意义相同

$[ var1 + var2 ] 中括号内位表达式,这个是计算表达式的值

$((var1+var2))和上边的意思是一样的...都是计算表达式的值

待续


5.脚本参数

shift n 表示跳过n个参数,是$n表示参数内容发生变化

$n 表示第n个参数

$# 表示参数个数

getopts

getopts  参数可能值  var

例子:getopts afdg var 意思是 var可以去 afdg中的某个值,调用脚本时 书写形式为 -a -f -d -g ,如果输入错了,还会有提示....