shell script学习需要几天?

  1. #后面的故事

    • $$: 最后一个脚本执行的进程号
    • $0 功能:指脚本本身,并且如果执行时带脚本路径,路径也会被包括进来

    • $n n=1,2,3,4..{10} 取脚本的参数,将第一个名的参数给$1,依次类推

    • $# 传参的个数

    • $? 获取上一个任务的返回值,是上个exit的参数。如果是函数用retrun 0等。

    • 返回值说明
      0命令执行正确
      2权限拒绝
      1~125找到了命令,但运行失败
      126没找到命令
      128系统被强制结束
    • $* 指所有参数, 将所有参数视为一个字符串”$1\$2\$3…”

    • $@ 指所有参数, 用时加引号和不加引号有。下面是oldboy老师的例子,个人认为参数带空格的情况不多见,暂且认为$*和$@差不多吧,遇到再具体来查文档。
      [root@oldboy ~]# set – “I am” handsome oldboy. #==>传入三个参数
      *[root@oldboy ~]# echo $# #==>现在有三个参数
      3
      [root@oldboy ~]# for i in $*;do echo $i;done #==>循环打印这些参数,用$*,无引号
      I
      am
      handsome
      oldboy.
      [root@oldboy ~]# for i in $@;do echo $i;done #==>$@这里可以省略。
      I
      am
      handsome
      Oldboy.
      加了引号
      [root@oldboy ~]# for i in “$*”;do echo $i;done #==> “$*”相当于“$1$2…”
      I am handsome oldboy.
      [root@oldboy ~]# for i in “$@”;do echo $i;done #==>$@这里可以省略。相当于参数$1,$2,$3
      I am
      handsome
      oldboy.*
      niu#bility
      niu&bility

  2. eval 命令 此处为转载http://blog.chinaunix.net/uid-21411227-id-1826706.html

    • eval command-line
      其中command-line是在终端上键入的一条普通命令行。然而当在它前面放上eval时,其结果是shell在执行命令行之前扫描它两次。如:
      pipe=”|”
      eval ls $pipe wc -l
      shell第1次扫描命令行时,它替换出pipe的值|,接着eval使它再次扫描命令行,这时shell把|作为管道符号了。
      如果变量中包含任何需要shell直接在命令行中看到的字符(不是替换的结果),就可以使用eval。命令行结束符(; | &),I/o重定向符(< >)和引号就属于对shell具有特殊意义的符号,必须直接出现在命令行中。
    • eval echo \$$# 取得最后一个参数
      如:cat last
      eval echo \$$#
      ./last one two three four
      four
      第一遍扫描后,shell把反斜杠去掉了。当shell再次扫描该行时,它替换了$4的值,并执行echo命令
    • 以下示意如何用eval命令创建指向变量的“指针”:
      x=100
      ptrx=x
      eval echo \$$ptrx 指向ptrx,用这里的方法可以理解b中的例子
      100 打印100
      eval $ptrx=50 将50存到ptrx指向的变量中。
      echo $x
      50 打印50
  3. shift 命令

  4. set 命令
    set的用法很多
    这里只提:模拟输入参数
    set – argu1 argu2 ..
    lzc# set – argu1 argu2
    lzc# echo $?
    2
    lzc#eval “echo \$$1”
    lzc#argu1

  5. dirname 和basename
    注意不显示全路径
    [root@localhost-ziyi home]# dirname ./test1/file99
    ./test1
    [root@localhost-ziyi home]# basename ./test1/file99
    file99

  6. shift命令
    Rename the positional parameters $N+1,$N+2 … to $1,$2 … If N is
    not given, it is assumed to be 1.

  7. 脚本中的 >/dev/null 2>&1
    一般来讲标准输出和标准错误输出都是屏幕,那为什么还要这么用呢?原因是标准输出的重定向。你的例子是重定向到了null,如果重定向到文件,例如:
    dir > out.txt
    表示标准输出重定向到out.txt文件。此时如果dir命令出错,那么错误信息不会输出到out.txt文件,错误信息仍然会输出到屏幕——标准错误输出。为了使正确的信息和错误的信息都重定向到out.txt文件,那么需要将错误信息的标准错误输出重定向到标准输出。即命令如下:
    dir > out.txt 2>&1

  8. 变量子串的操作

    1. ${#string} 返回string的长度==wc -L命令
    2. ${string/substring/replace}: 用replace来替换substring
    3. ${value-word}: 当value未定义时,返回word
      例如
      lzc#result=${test:word}
      lzc#echo$result
      lzc#word

    4. ${value:=word}:若value变量未定义或者为空时,返回word的值的同时将word赋给value
      [root@localhost-ziyi ~]# echo ${test-word}
      word
      [root@localhost-ziyi ~]# echo $test
      [root@localhost-ziyi ~]# echo ${test:=word}
      word

    5. ${test:?”not defined”}:如果变量test不存在,则返回-bash:test: not defined
      [root@localhost-ziyi ~]# echo $test
      word[root@localhost-ziyi ~]# echo ${test:? “not defined”}
      word
      [root@localhost-ziyi ~]# echo ${t:? “not defined”}
      -bash: t: not defined
      [root@localhost-ziyi ~]#

    6. ${test:+word1}:如果变量存在,返回word1
      [root@localhost-ziyi ~]# echo \${test:+word1}
      word1
      [root@localhost-ziyi ~]# echo \$test
      word

    7. shell内置了许多内置命令,用这些内置命令出来程序时,效率比较高

  9. 变量的数值计算常见格式
    (()), let,expr,bc,$[]
    其中bc可计算浮点数

    1. (())用法: a =$((2**3-2%3))或$((a=..))
    2. let 等同于(()),但效率相对低
    3. expr的三种用法:
      第一: 算数运算
      lzc#expr 2+2
      第二: 判断文件名扩展名

    #!/bin/sh
    if expr “$1” : “.*.pub” then :判断$1是否为.pub结尾
    第三:计算字符串的长度
    lzc#echo ${#chars}==echo $(expr length “$chars”)

     2. bc可交给管道,可运算小数 :
     lzc#echo 1+1.5|bc
    
  10. read命令 通过读入的方式给变量赋值
    read 【参数】【变量名】
    -p:prompt
    -t:timeout
    [root@localhost-ziyi home]# read -t 5 -p “pls input: ” a
    pls input: 44
    [root@localhost-ziyi home]# echo $a
    44
    3.脚本1 read的方式比较两个数大小;如果传参的方式在脚本中把a=$1, b=$2即可;
    #!/bin/bash
    read -p “plsease inoyt two integrities number:” a b
    [ -z “$a” ] || [ -z “$b” ] &&{
    echo “please input two integrities again”
    exit 1
    }

expr $a + 0 &>/dev/null
RETURN1=$?
expr $b + 0 &>/dev/null
RETURN2=$?

test $RETURN1 -eq 0 -a $RETURN2 -eq 0 ||{
echo “plse input two ‘num’ again ”
exit 2
}

[ $a -lt $b ] &&{
echo “$a < $b”
exit 0
}
[ $a -eq $b ] &&{
echo “$a = $b”
exit 0
}[ $a -gt $b ] &&{
echo “$a > $b”
exit 0
}

  1. 条件表达式

    1. 快捷if

      1. [ ] &&
    2. if语句: if …;then… elif…;then…else…if

      1. if test expression 跟if [ expression ]是等价的
      2. [[ expression1 && expression2]] 双中括号中可以同时用两条来判断文件是否存在
      3. expresson的字符串需要”“, <>需要转义,= !=不需要
      4. 逻辑操作符使用: [ -a ] =[[ && ]] , [-o]=[[||]], [ ! ]=[[!]]
      5. 不支持整数变量直接if,必须:if [ i –ne 0 ];但支持字符串变量直接if,if [ str ] 如果字符串非0
      6. 函数可以作为if的条件
      7. if command 等价于 command+if $?
        $ vi testsh.sh
        #!/bin/sh
        if
        cat 111-tmp.txt | grep ting1
        then
        echo found
        else
        echo “no found”
        fi $ vi testsh.sh
        #!/bin/sh
        cat 111-tmp.txt | grep ting1
        if [ \$? -eq 0 ]
        then
        echo \$?
        echo found
        else
        echo \$?
        echo “no found”
        fi

test expression [expression]

Evaluate an expression and, if its value is true, return a zero exit status; otherwise, return a nonzero exit status. In shell scripts, you can use the alternate form [expression] . This command is generally used with conditional constructs in shell programs. Also exists as a built-in in most shells.

File testers
The syntax for all of these options is test option file. If the specified file does not exist, they return false. Otherwise, they test the file as specified in the option description.
-d
Is the file a directory?
-e
Does the file exist?
-f
Is the file a regular file?
-L, -h
Is the file a symbolic link?
-r
Is the file readable by the current user?
-s
Is the file nonempty?
-S
Is the file a socket?
-w
Is the file writable by the current user?
-x
Is the file executable?
-z
the length of STRING is zero

File comparisons
The syntax for file comparisons is test file1 option file2. A string by itself, without options, returns true if it’s at least one character long.
-nt
Is file1 newer than file2? Check modification date, not creation date.
-ot
Is file1 older than file2? Check modification date, not creation date.
-ef
Do the files have identical device and inode numbers?

String tests
The syntax for string tests is test option string or test

string1 = string2
Are the two strings equal?
string1 != string2
Are the strings unequal?

Expression tests
Note that an expression can consist of any of the previous tests.
(expression)
Is the expression true?
! expression
Is the expression false?
expression -a expression
Are the expressions both true?
expression -o expression
Is either expression true?

Integer tests
The syntax for integer tests is test integer1 option integer2. You may substitute -l string for an integer; this evaluates to string’s length.
-eq
Are the two integers equal?
-ne
Are the two integers unequal?
-lt
Is integer1 less than integer2?
-le
Is integer1 less than or equal to integer2?
-gt
Is integer1 greater than integer2?
-ge
Is integer1 greater than or equal to integer2?

 1. case语句: case \$变量名  in "")...;;*)...;;esac
  1. shell循环语句和跳出语句

    1. while:while 条件;do statement..done
    2. for:风格1– for 变量 in 列表;do 循环体 done
      风格2– for((变量赋值;循环条件;步长));do 循环体 done

    3. until: until条件;do statement done

    4. break N 跳出当前的N个循环体
    5. continue 跳出当前的N-1个循环体,如果n=1,则不跳,马上终止此次循环,继续执行下一次循环
  2. shell 数组 可以不用
    7. 数组定义: lzc# array=(1 2 3) array=($(ls))—-ls 为命令
    8. 数组长度:lzc # echo ${#array[*]}
    9. 输出全部lzc# echo ${array[@]}
    10. 删除数组的某个:lzc# unset array[0]

  3. shell的函数
    9. 语法:【funciton】 函数名(){ return n返回}
    9. 函数的参数: $1,$#,$*,$?及$@
    $0比较特殊,它仍然是父脚本的名称

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值