ksh学习笔记

1.重定向形式

文件描述符:

   标准输入  stdin  默认为键盘

   标准输出  stdout

   标准错误  stderr

 

Cmd > file  将cmd的结果输出到file(覆盖)
Cmd >> file  将cmd的结果输出到file(追加)
Cmd < file  从file中获取cmd 的输入
Cmd << text  将shell脚本的内容(直到遇见一个和text一样的标记为止)作为cmd的输入
Cmd <> file  在标准输入上打开文件以便读写
Cmd >&n  将输出发送到文件描述符n。ll >&1
Cmd m>&n 将本来输出的m中的内容转发到n中。Ll 3>&2
Cmd >&-  关闭标准输出
Cmd <&n 获取输入
Cmd m<&n
Cmd <&- 关闭标准输入
在文件描述符和一个重定向符号间不允许有空格。
 
Cmd 2>file  将标准错误发到file中
Cmd > file 2>&1  将标准错误和标准输出都发到file
Cmd > f1 2>f2  将标准输出发到f1,标准错误发到f2
Cmd | tee files  将输出发送到标准输出和files中
Cmd 2>&1 | tee files  将输出和错误同时发到标准输出和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"  (注意大小写)
2011-06-10 11:45:26
# 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 [[]].
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值