---
title: "Simple ksh knowledge"
author: "RobertLeeJesus"
date: "2008-07-27T17:12:48Z"
tags: []
slug: "simple-ksh-knowledge"
draft: false
lastmod: "2008-07-27T17:12:48Z"
id: 220155
url: "https://hidefromall.iteye.com/blog/220155"
---

1\. Parameter handover.

     The ksh paramerter comprises $order, for example, $0, $1. The $0 means the program itself or the function itself, just like the C++, so the $1 is the first parameter, $2 second...

     There are other parameter information,

       $# ----传递给程序的总的参数数目  
　    $? ----上一个代码或者shell程序在shell中退出的情况，如果正常退出则返回0，反之为非0值。  
　    $* ----传递给程序的所有参数组成的字符串。  
       $n ----表示第几个参数，$1 表示第一个参数，$2 表示第二个参数 ...   
       $0 ----当前程序的名称  
       $@----以"参数1" "参数2" ... 形式保存所有参数  
       $$ ----本程序的(进程ID号)PID  
       $!  ----上一个命令的PID

2\. How to obtain the program option paramters.

> #!/bin/bash  
> #getopts  
> ALL=false  
> HELP=false  
> FILE=false  
> VERBOSE=false  
> while getopts ahfvc: OPTION   #将ahfvc依次传给OPTION c后面的:表示-c时需传入参数  
> do  
>   case ${OPTION} in  
>     a)  
>       ALL=true  
>       echo "ALL IS ${ALL}"  
>       ;  
>     h)  
>       HELP=true  
>       echo "HELP IS ${HELP}"  
>       ;  
>     f)  
>       FILE=true  
>       echo "FILE IS ${FILE}"  
>       ;  
>     v)  
>       VERBOS=false  
>       echo "VERBOSE IS ${VERBOSE}"  
>       ;  
>     c)  
>       c=${OPTARG}  
>       echo "c value is $c"  
>       ;  
>     \\?)  
>       echo "\`basename $0\` -\[a h f v] -\[c value] file"  
>       ;  
>     esac  
> done
> 
> 输入./getopts -a   输出：ALL IS true   #执行case a模式的命令  
> 输入./getopts -h   输出：HLEP IS true #执行case h模式的命令  
> 输入./getopts -f   输出：FILE IS true   #执行case f模式的命令  
> 输入./getopts -v   输出：VERBOSE IS true   #执行case v模式的命令  
> 输入./getopts -c   提示错误：需要传入参数 #c后面有“：”所以需传参数  
> 输入./getopts -c hello 输出：c value is hello     #执行case c模式的命令  
> 输入./getopts -b   输出：basename ./getopts -\[a h f v] -\[c value] file   #其他情况

3\. How to compare 2 string?

            The sign = is used to compare the 2 stirngs while -eq is used to compare the 2 numbers.

> 比较两个字符串是否相等的办法是：
> 
> if \[ "$test"x = "test"x ]; then
> 
> 这里的关键有几点：
> 
> > 1) 使用单个等号
> > 
> > 2) 注意到等号两边各有一个空格：这是unix shell的要求
> > 
> > 3) 注意到"$test"x最后的x，这是特意安排的，因为当$test为空的时候，上面的表达式就变成了x = testx，显然是不相等的。而如果没有这个x，表达式就会报错：[: =: unary operator expected
> > 
> > 4) There is bank right behind \[ and before ], they are necessary.

4\. Simple commands.

       stty erase &lt;BackSpace&gt; ###use the backslash as the delete key.

       PS1="[\`/usr/ucb/whoami\`@\`uname](mailto:%60/usr/ucb/whoami%60@%60uname) -n\`(KSH)\[!]&gt; " ###used to cumtomiz the command echo.

       set -o vi ###set the edition mode as vi, you have the command history with that.

      find . -name "test" -type f ! -newer source\_test -exec cp source\_test {} \\; ###How to copy file when the destination file is newer file?

 

**How to use the options parameters.**  

[http://blog.chinaunix.net/u/5591/showart\_371375.html](http://blog.chinaunix.net/u/5591/showart_371375.html)

 

**Please refer to the tutorial.**

[http://www.xxlinux.com/linux/article/unix/tigao/2006-06-24/1958.html](http://www.xxlinux.com/linux/article/unix/tigao/2006-06-24/1958.html)

[http://www.xxlinux.com/linux/article/unix/tigao/2006-06-24/1959.html](http://www.xxlinux.com/linux/article/unix/tigao/2006-06-24/1959.html)

[http://www.xxlinux.com/linux/article/unix/tigao/2006-06-24/1960.html](http://www.xxlinux.com/linux/article/unix/tigao/2006-06-24/1960.html)

 

**Ksh reference manual.**

[http://docs.sun.com/app/docs/doc/819-2239/ksh-1?l=en&a=view&q=ksh](http://docs.sun.com/app/docs/doc/819-2239/ksh-1?l=en&amp%3Ba=view&amp%3Bq=ksh)