博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

shell学习笔记

Posted on 2011-11-23 13:22  yuanzfy  阅读(254)  评论(0)    收藏  举报

1. 重定向

    0:重定向输入<。1:重定向输出>。2:重定向错误输出2>

    >>和2>>将输出追加到文件末尾不会覆盖文件内容

2. 管道

    |前面命令的输出当作后面命令的输入参数:cat test.txt | sort | more.

3. more $(grep –l bin *) 和 grep –l bin * | more

    这两个输出是不一样的,第一个输出的是grep得到的文件的内容,第二个输出的是grep得到的文件名列表,可以理解成管道。

4. 变量使用引号

var=sdf

echo $var
echo "$var"
echo '$var'
echo \$var

    输出为

sdf
sdf
$var
$var

    可以看出,在单引号中的$var是不会被解析的,\$为转移字符,也不会解析$var.

5. 环境变量

    $PS1 :命令提示符

    $PS2 :二级提示符,通常是>

    $IFS  :输入域分隔符,通常是空格、制表符、换行符

    $0     :shell脚本名称,如bash

    $#    :传递给脚本的参数个数

    $$     :shell脚本的进程号

    $*     :包含shell脚本所有的参数,各参数之间用IFS隔开,在ubuntu11.10下测试,并不受IFS影响

    $@    :同样包含shell脚本所有的参数,但是不使用IFS隔开参数,即使IFS没设置也不会挤在一起

6. test 或 [  ]条件判断

    ubuntu11.10测试test 2 –gt 1; echo $?输出的是0,所以说0表示真,1表示假

    (1)字符串比较

            str1 = str2 、 str1 != str2 :是否相等

            –z string、–n string         :判断是否为空串

    (2)算数比较

            -eq:== ; -ne:!=; –gt:>; –ge:>=; –lt:<; –le:<=; !:!

    (3)文件条件测试

            -d file: 文件是否为目录directory

            -f file : 文件是否为普通文件

            -r file –w file –x file : 文件是否可读可写可执行

            -s file : 文件大小是否不为0

            -g file –u file : 文件的set-group-id位、set-user-id位是否被设置

    (4)命令列表

            AND、OR列表:[ –f test ] && echo “exist” || echo “not exist”

            如果test存在,则输出exist,否则输出not exist    

7. 条件控制语句if

#/bin/sh

echo "please enter is it morning:"
read ismorning
if [ $ismorning = "yes" ]; then
    echo "good morning"
elif [ $ismorning = "no" ]; then
    echo "good afternoon"
else
    echo "error input!"
fi

    有一个潜在的问题,如果你没有输入任何字符给ismorning,那么在匹配的时候,就会出现一个空元素和一个字符串匹配的错误,所以$ismorning要改成"$ismorning"。  

8. for循环控制语句

#!/bin/sh

for name is ss dd ff; do
    echo $name
done

for file in *test; do
    echo $file
done

for file in $(ls *test); do 
    echo $file
done

9. while,until循环控制语句

    使用test([   ])测试语句作为循环继续的条件,而不是像for一样确定好循环的次数

    until同while相似,循环执行直至条件测试为真

#!/bin/sh

echo "please enter the password"
read passwd

while [ "$passwd" != "dd" ]; do
    echo "sorry,try again"
    read passwd
done

echo "welcome"

exit 0 

10. case分支结构语句

#!/bin/sh

echo "what day is today(one of follow) "
echo "monday,tuesday,wednesday,thursday,friday,saturday,sunday:"
    read today

case "$today" in
 
    [sS][aA][tT][uU][rR][dD][aA][yY] | [sS][uU][nN][dD][aA][yY])
                echo "today is weekend,have a great time!"
        ;;   
    [fF][rR][iI][dD][aA][yY])
        echo "tomorrow is weekend,you can take a rest!"
        ;;    
    *)       
        echo "just work hard!"
        ;;
esac

exit 0

11. shell函数

    (1)local关键字定义函数的局部变量

    (2)ubuntu中条件0表示真,1表示假

    (3)res1=$(foo),此语句会把foo中echo输出的东西全部存入res1中,并且在函数运行时不会输出

#!/bin/sh

sample="globle variable"

foo(){
    local sample="local variable"
    echo "function foo is executing"
    echo $sample
    if [ "$1" = "asd" ]; then
       return 1
    else
       return 0
    fi
}

echo "script starting"
echo $sample

res1=$(foo)

if foo "$1"; 
then
    echo "the return of foo() is true"
else
    echo "the return of foo() is false"
fi

echo "script ended"
echo $sample
echo $res1

exit 0

    输出:

    script starting
    globle variable
    function foo is executing
    local variable
    the return of foo() is false
    script ended
    globle variable
    function foo is executing local variable

12. shell命令

    (1)break n,continue n

            break跳出n循环,break=break 1,一般只跳出一层,否则程序很难阅读

    (2):

            :是一个空命令,也可以当作true的别名来使用,while :实现了一个无限循环。

    (3)echo去掉末尾换行符

            echo –n “str”

            echo –e “str\c”

    (4)eval

            foo=10; x=foo; eval y=’$'$x; echo $y  得到10

    (5)exit n

            0表示成功,1-125为可定义错误代码,其余具有保留意义,例如126表示文件不可执行,127表示命令未找到

    (6)expr 对表达式求值

            x=1; y=$(expr Sx + 1); echo $y  输出2

            expr支持的运算符有 |、&、=、>、>=、<、<=、!=、+、-、*、/、%。

    (7)set命令

            set $(date)

            将date命令的输出设置为参数列表,然后可以用$n来引用各个域

    (8)shift命令

            将所有参数变量左移一个位置,值$1丢弃。

    (9)trap命令

#!/bin/sh

trap 'rm -f /tmp/my_tmp_file_$$' INT
echo creating file /tmp/my_tmp_file_$$
date 1&gt; /tmp/my_tmp_file_$$

echo "press (CTRL-C) to interrupt......"
while [ -f /tmp/my_tmp_file_$$ ];do
    echo File exists
    sleep 1
done  
echo the file no longer exists

trap - INT
echo creating file /tmp/my_tmp_file_$$
date 1&gt; /tmp/my_tmp_file_$$

echo "press (CTRL-C) to interrupt......"
while [ -f /tmp/my_tmp_file_$$ ];do
    echo file exists
    sleep 1
done

echo we never get here
exit 0