1.重定向形式
文件描述符:
0
1
2
Cmd > file
Cmd >> file
Cmd < file
Cmd << text
Cmd <> file
Cmd >&n
Cmd m>&n 将本来输出的m中的内容转发到n中。Ll 3>&2
Cmd >&-
Cmd <&n 获取输入
Cmd m<&n
Cmd <&- 关闭标准输入
在文件描述符和一个重定向符号间不允许有空格。
Cmd 2>file
Cmd > file 2>&1
Cmd > f1 2>f2
Cmd | tee files
Cmd 2>&1 | tee files
2.内置变量
$#
$? 上一条命令执行后返回的值
$$ 当前进程的进程号(PID), 通常用于在shell脚本中创建临时文件的名称
$0 第一个参数即命令名
$n 命令行上的第n个参数
$* 将命令行上所有参数作为一个字符串
$@ 命令行上所有参数,但每个参数都被引号引起来
Special variables
- PWD - always the current directory
- RANDOM - a different number every time you access it
- $$ - the current process id (of the script, not the user's shell)
- PPID - the "parent process"s ID. (BUT NOT ALWAYS, FOR FUNCTIONS)
- $? - exit status of last command run by the script
- PS1 - your "prompt". "PS1='$PWD:> '" is interesting.
- $1 to $9 - arguments 1 to 9 passed to your script or function (you can actually have higher, but you need to use braces for those)
/dev/null和/dev/zero是非常有趣的两个设备,它们都犹如一个黑洞,什么东西掉进去都会消失殆尽
shell命令怎么得到yyyymmddhhmmss格式的时间?:
# date +"%Y-%m-%d %H:%M:%S"
# date +"%D %H:%M:%S"
06/10/11 11:47:46
3.sript
case $var in
john|fred) print $invitation;;
martin) print $declination;;
*) print "Wrong name...";;
esac
if [ $? -eq 0 ] ; then
print we are okay
else
print something failed
fi
if [[ $? -ne 0 ]] ; then echo status is bad ; fi if [[ "$var" != "good" ]] ; then echo var is not good ; fi if [[ ! -f /file/name ]] ; then echo /file/name is not there ; fi if [[ ! -d /dir/name ]] ; then echo /dir/name is not a directory ; fi
Please note that [[]] is a special built-in version of test, that is almost, but not 100%, like the standard []. The main difference being that wildcard expansion does not work within [[]].
4.chattr +i test.sh
一旦设立这个属性,就算是root也不能修改这个文件(cracker入侵主机把后门程序弄成隐藏文件,一般设立这个属性),如果不知道chattr,就会不明白原因,删除属性方法:chattr -i test.sh