`

初涉linux(三)之shell编程

阅读更多
如果系统学习,使用<<高级Bash脚本编程指南>>这本书
shell初级编程                                              by hayabusa
3.1讲义
  3.1.1 shell
     set //所有变量
     unset //取消变量设置
     export 变量 //导出变量,使其全局可用
     env //环境变量
  shell基本格式:
  #!/bin/bash
  //edit...
  chmod u+x script

  $(command)或者`command` //命令嵌套的写法
  3.1.2
    3.1.2.1
      stdin 标准输入
      stdout 标准输出
      stderr 标准错误输出
    3.1.2.2重定向:
      > 输出重定向
      2> 错误重定向
      &> 全部重定向
      tr 'a-z' 'A-Z' < var3.sh
   3.1.2.3
    管道:前者输出作为后者输入
       ls -l /etc | less    
   3.1.2.4
     多目标重定向
     command |tee t2.txt
3.1.3正则表达式
    ^  $   *
3.1.4 shell 带参数
     $1   $2    $*    $#
3.1.5 条件判断
      = -eq
      != -ne
      -gt
      -lt
      -ge
      -le
3.1.6控制流程 -a -o !

     while 条件
       do
        命令
       done
       
     if 条件
        then  命令(可以嵌套if)
     elif 条件
        then  命令
     else
        命令(可以嵌套if)
     fi

  @#!!//数值运算加两对圆括号
   case
     case 值 in
     模式)
        命令
    ;;
     模式)
        命令
    ;;
    ...
    esac


for 变量 in 'seq 1 100'
do
command
done
  //break continue 类似于C:exit直接退出,可带返回值
 
  until 条件
   do
   //command
   done
3.2 扩展
  3.2.1
     读老师的程序偷师到的几个命令
     basename /path/filename //printf the real name of a file
     cut  -d: -f2 // 这个命令用好了非常的强大
  3.2.2 userlist.sh
   # list all the users
#!/bin/bash
n=0;
for i in `cut -d: -f3 /etc/passwd`
do
((n=n+1))
m=`echo "$n " |tr ' ' 'p'`
if [ $i -le 60000 -a $i -ge 500 ]
   then cut -d: -f1 /etc/passwd|sed -n $m
elif [ $i = 0 ]
then cut -d: -f1 /etc/passwd|sed -n $m
fi
done
3.3 问题
    3.3.1
     caseselect 文件中的此句(鄙视自己,看了解释还不懂
     echo "`basename $0`:This is not between 1 and 5" >&2
    已解决:
    这句话的意思就是将stdout重定向到stderr之中,但是由于stdout与stderr的默认输出都是monitor,所以看不到效果,下边的代码好些:
    echo "`basename $0`:This is not between 1 and 5"  2>ver  >&2

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics