linux shell.docx

上传人:b****7 文档编号:10803123 上传时间:2023-02-23 格式:DOCX 页数:60 大小:42.85KB
下载 相关 举报
linux shell.docx_第1页
第1页 / 共60页
linux shell.docx_第2页
第2页 / 共60页
linux shell.docx_第3页
第3页 / 共60页
linux shell.docx_第4页
第4页 / 共60页
linux shell.docx_第5页
第5页 / 共60页
点击查看更多>>
下载资源
资源描述

linux shell.docx

《linux shell.docx》由会员分享,可在线阅读,更多相关《linux shell.docx(60页珍藏版)》请在冰豆网上搜索。

linux shell.docx

linuxshell

介绍:

BourneShell基础及其他很多有用的特性,shell编程及组织。

A9ELinux联盟

 A9ELinux联盟

主要内容:

A9ELinux联盟

.shell基础     基本介绍,环境,选项,特殊字符A9ELinux联盟

.shell变量     用户定义变量,环境变量,位置变量(shell参数)A9ELinux联盟

.shellscript编程A9ELinux联盟

       条件测试,循环及重复控制A9ELinux联盟

.shell定制A9ELinux联盟

 A9ELinux联盟

1.shell基础知识A9ELinux联盟

 作者:

StephenBourne在Bell实验室开发A9ELinux联盟

 建议:

mansh 查看相关UNIX上的改进或特性A9ELinux联盟

 A9ELinux联盟

(1)shell提示符及其环境A9ELinux联盟

  /etc/passwd文件A9ELinux联盟

  提示符:

$A9ELinux联盟

  /etc/profile$HOME/.profileA9ELinux联盟

(2)shell执行选项A9ELinux联盟

  -n  测试shellscript语法结构,只读取shellscript但不执行A9ELinux联盟

  -x  进入跟踪方式,显示所执行的每一条命令,用于调度A9ELinux联盟

  -a  TagallvariablesforexportA9ELinux联盟

  -c"string" 从strings中读取命令A9ELinux联盟

  -e  非交互方式A9ELinux联盟

  -f  关闭shell文件名产生功能A9ELinux联盟

  -h  locateandrememberfunctionsasdefindA9ELinux联盟

  -i  交互方式A9ELinux联盟

  -k  从环境变量中读取命令的参数A9ELinux联盟

  -r  限制方式A9ELinux联盟

  -s  从标准输入读取命令A9ELinux联盟

  -t  执行命令后退出(shellexits)A9ELinux联盟

  -u  在替换中如使用未定义变量为错误A9ELinux联盟

  -v  verbose,显示shell输入行A9ELinux联盟

 A9ELinux联盟

  这些选项可以联合使用,但有些显然相互冲突,如-e和-i.A9ELinux联盟

 A9ELinux联盟

(3)受限制shell(RestirctedShell)A9ELinux联盟

   sh-r或/bin/rshA9ELinux联盟

 A9ELinux联盟

   不能执行如下操作:

cd,更改PATH,指定全路径名,输出重定向,因此可以提供一个较A9ELinux联盟

   好的控制和安全机制。

通常rsh用于应用型用户及拨号用户,这些用户通常是看不到提A9ELinux联盟

   示符的。

通常受限制用户的主目录是不可写的。

A9ELinux联盟

 A9ELinux联盟

   不足:

如果用户可以调用sh,则rsh的限制将不在起作用,事实上如果用户在vi及moreA9ELinux联盟

       程序中调用shell,而这时rsh的限制将不再起作用。

A9ELinux联盟

 A9ELinux联盟

(4)用set改变shell选项A9ELinux联盟

  用户可以在$提示符下用set命令来设置或取消shell的选项。

使用-设置选项,+取消相应A9ELinux联盟

  选项,大多数UNIX系统允许a,e,f,h,k,n,u,v和x的开关设置/取消。

A9ELinux联盟

 A9ELinux联盟

  set-xvA9ELinux联盟

       启动跟踪方式;显示所有的命令及替换,同样显示输入。

A9ELinux联盟

  set-tuA9ELinux联盟

       关闭在替换时对未定义变量的检查。

A9ELinux联盟

 A9ELinux联盟

  使用echo$-显示所有已设置的shell选项。

A9ELinux联盟

 A9ELinux联盟

 A9ELinux联盟

(5)用户启动文件.profileA9ELinux联盟

       PATH=$PATH:

/usr/loacl/bin;exportPATHA9ELinux联盟

 A9ELinux联盟

(6)shell环境变量A9ELinux联盟

       CDPATH 用于cd命令的查找路径A9ELinux联盟

       HOME   /etc/passwd文件中列出的用户主目录A9ELinux联盟

       IFS    InternalFieldSeparator,默认为空格,tab及换行符A9ELinux联盟

       MAIL   /var/mail/$USERNAME    mail等程序使用A9ELinux联盟

       PATHA9ELinux联盟

       PS1,PS2       默认提示符($)及换行提示符(>)A9ELinux联盟

       TERM   终端类型,常用的有vt100,ansi,vt200,xterm等A9ELinux联盟

 A9ELinux联盟

       示例:

$PS1="test:

";exportPS1A9ELinux联盟

             test:

PS1="$";exportPS1A9ELinux联盟

             $echo$MAILA9ELinux联盟

             /var/mail/usernameA9ELinux联盟

(7)保留字符及其含义A9ELinux联盟

$     shell变量名的开始,如$varA9ELinux联盟

  |   管道,将标准输出转到下一个命令的标准输入A9ELinux联盟

  #   注释开始A9ELinux联盟

  &   在后台执行一个进程A9ELinux联盟

  ?

  匹配一个字符A9ELinux联盟

  *   匹配0到多个字符(与DOS不同,可在文件名中间使用,并且含.)A9ELinux联盟

  $-  使用set及执行时传递给shell的标志位A9ELinux联盟

  $!

  最后一个子进程的进程号A9ELinux联盟

  $#  传递给shellscript的参数个数A9ELinux联盟

  $*  传递给shellscript的参数A9ELinux联盟

  $@  所有参数,个别的用双引号括起来A9ELinux联盟

  $?

  上一个命令的返回代码A9ELinux联盟

  $0  当前shell的名字A9ELinux联盟

  $n   (n:

1-)位置参数A9ELinux联盟

  $$  进程标识号(ProcessIdentifierNumber,PID)A9ELinux联盟

  >file       输出重定向A9ELinux联盟

  

  `command`   命令替换,如   filename=`basename/usr/local/bin/tcsh`A9ELinux联盟

  >>fiile     输出重定向,appendA9ELinux联盟

 A9ELinux联盟

  转义符及单引号:

A9ELinux联盟

       $echo"$HOME$PATH"A9ELinux联盟

       /home/hbwork/opt/kde/bin:

/usr/local/bin:

/bin:

/usr/bin:

/usr/X11R6/bin:

A9ELinux联盟

       $echo'$HOME$PATH'A9ELinux联盟

       $HOME$PATHA9ELinux联盟

       $echo$HOME$PATHA9ELinux联盟

       $HOME/opt/kde/bin:

/usr/local/bin:

/bin:

/usr/bin:

/usr/X11R6/bin:

/home/hbwA9ELinux联盟

ork/binA9ELinux联盟

 A9ELinux联盟

  其他:

A9ELinux联盟

       $dir=lsA9ELinux联盟

       $$dirA9ELinux联盟

       $aliasdirlsA9ELinux联盟

       $dirA9ELinux联盟

 A9ELinux联盟

       ls>filelistA9ELinux联盟

       ls>>filelistA9ELinux联盟

       wc-l

       wc-lfilelistA9ELinux联盟

       sleep5;echo5secondsreaches;ls-lA9ELinux联盟

       psax|egrepinetdA9ELinux联盟

       find/-namecore-execrm{};&A9ELinux联盟

       filename=`date"+%Y%m%d"`.logA9ELinux联盟

 A9ELinux联盟

2.shell变量A9ELinux联盟

 变量:

代表某些值的符号,如$HOME,cd命令查找$HOME,在计算机语言中可以使用变量可以A9ELinux联盟

       进行多种运算和控制。

A9ELinux联盟

 A9ELinux联盟

 BourneShell有如下四种变量:

A9ELinux联盟

       .用户自定义变量A9ELinux联盟

       .位置变量即shellscript之参数A9ELinux联盟

       .预定义变量(特殊变量)A9ELinux联盟

       .环境变量(参考shell定制部分)A9ELinux联盟

(1)用户自定义变量(数据的存储)A9ELinux联盟

       $COUNT=1A9ELinux联盟

       $NAME="HeBinwu"A9ELinux联盟

 A9ELinux联盟

    技巧:

因为大部分UNIX命令使用小写字符,因此在shell编程中通常使用全大写变量,A9ELinux联盟

       当然这并不是强制性的,但使用大写字符可以在编程中方便地识别变量。

A9ELinux联盟

 A9ELinux联盟

    变量的调用:

在变量前加$A9ELinux联盟

       $echo$HOMEA9ELinux联盟

       /home/hbworkA9ELinux联盟

       $WEEK=SaturA9ELinux联盟

       $echoTodayis$WEEKdayA9ELinux联盟

       TodayisA9ELinux联盟

       $echoTodayis${WEEK}dayA9ELinux联盟

       TodayisSaturdayA9ELinux联盟

 A9ELinux联盟

    Shell变量赋值从右从左进行(LinuxShell/bash从左向右赋值!

)A9ELinux联盟

    $X=$YY=yA9ELinux联盟

    $echo$XA9ELinux联盟

    yA9ELinux联盟

    $Z=zY=$ZA9ELinux联盟

    $echo$YA9ELinux联盟

 A9ELinux联盟

    $A9ELinux联盟

 A9ELinux联盟

    使用unset命令删除变量的赋值A9ELinux联盟

    $Z=helloA9ELinux联盟

    $echo$ZA9ELinux联盟

    helloA9ELinux联盟

    $unsetZA9ELinux联盟

    $echo$ZA9ELinux联盟

 A9ELinux联盟

    $A9ELinux联盟

 A9ELinux联盟

    有条件的命令替换A9ELinux联盟

       在BourneShell中可以使变量替换在特定条件下执行,即有条件的环境变量替换。

A9ELinux联盟

       这种变量替换总是用大括号括起来的。

A9ELinux联盟

 A9ELinux联盟

       .设置变量的默认值A9ELinux联盟

           在变量未赋值之前其值为空。

BourneShell允许对变量设置默认值,其格式如A9ELinux联盟

下:

A9ELinux联盟

           ${variable:

-defaultvalue}A9ELinux联盟

           例:

A9ELinux联盟

               $echoHello$UNAMEA9ELinux联盟

$echoHello${UNAME:

-there}A9ELinux联盟

               HellothereA9ELinux联盟

               $echo$UNAME  #变量值并未发生变化A9ELinux联盟

 A9ELinux联盟

               $UNAME=hbworkA9ELinux联盟

               $echoHello${UNAME:

-there}A9ELinux联盟

               HellohbworkA9ELinux联盟

               $A9ELinux联盟

       .另一种情况:

改变变量的值,格式如下:

A9ELinux联盟

           ${variable:

=value}A9ELinux联盟

 A9ELinux联盟

           例:

A9ELinux联盟

               $echoHello$UNAMEA9ELinux联盟

               HelloA9ELinux联盟

               $echoHello${UNAME:

=there}A9ELinux联盟

               HellothereA9ELinux联盟

               $echo$UNAME  #变量值并未发生变化A9ELinux联盟

               thereA9ELinux联盟

               $A9ELinux联盟

       .变量替换中使用命令替换A9ELinux联盟

               $USERDIR=${$MYDIR:

-`pwd`}A9ELinux联盟

 A9ELinux联盟

       .在变量已赋值时进行替换 ${variable:

+value}A9ELinux联盟

       .带有错误检查的有条件变量替换A9ELinux联盟

         ${variable:

?

value}A9ELinux联盟

         例:

A9ELinux联盟

         $UNAME=A9ELinux联盟

         $echo${UNAME:

?

"UNAMEhasnotbeenset"}A9ELinux联盟

         UNAME:

UNAMEhasnotbeensetA9ELinux联盟

         $echo${UNAME:

?

}A9ELinux联盟

         UNAME:

parameternullornotsetA9ELinux联盟

 A9ELinux联盟

 

(2)位置变量(Shell参数)A9ELinux联盟

 在shellscript中位置参数可用$1..$9表示,$0表示内容通常为当前执行程序的文件名。

A9ELinux联盟

 .防止变量值被替换readonlyvariableA9ELinux联盟

 .使用export命令输出变量,使得变量对子shell可用,当shell执行一下程序时,shellA9ELinux联盟

  将为其设置一个新的环境让其执行,这称之了subshell. 在BourneShell中变量通常A9ELinux联盟

  被认为是本地变量,也就是说在对其赋值之外的shell环境之外是不认识此变量的。

使A9ELinux联盟

  用export对subshell可用。

A9ELinux联盟

 A9ELinux联盟

  例:

对变量PS1的export操作,shell的提示符将发生变化。

A9ELinux联盟

  $PS1=`hostname`$A9ELinux联盟

       peony$shA9ELinux联盟

       $echo$PS1A9ELinux联盟

       $ <-输出结果A9ELinux联盟

       $exitA9ELinux联盟

       peony$exportPS1A9ELinux联盟

       peony$shA9ELinux联盟

       peony$echo$PS1A9ELinux联盟

       peony$ <-输出结果A9ELinux联盟

       peony$A9ELinux联盟

 A9ELinux联盟

 A9ELinux联盟

3.ShellScript编程A9ELinux联盟

目的:

使用UNIX所提供的最常用工具来完成所需复杂任务的强大功能。

A9ELinux联盟

 A9ELinux联盟

(1)最简单的Shell编程A9ELinux联盟

  $ls-R/|grepmyname|moreA9ELinux联盟

 A9ELinux联盟

  每天数据的备份:

A9ELinux联盟

  $cd/usr/yourname;ls*|cpio-o>/dev/rmt/0hA9ELinux联盟

 A9ELinux联盟

  书写程序的目的是一次编程,多次使用(执行)!

A9ELinux联盟

 A9ELinux联盟

  $cat>backup.shA9ELinux联盟

  cd/home/hbworkA9ELinux联盟

  ls*|cpio-o>/dev/rmt/0hA9ELinux联盟

  ^DA9ELinux联盟

 A9ELinux联盟

  执行:

A9ELinux联盟

  $shbackup.shA9ELinux联盟

 A9ELinux联盟

  或:

A9ELinux联盟

  $chmod+xbackup.shA9ELinux联盟

  $./backup.shA9ELinux联盟

 A9ELinux联盟

  技巧:

在shellscript中加入必要的注释,以便以后阅读及维护。

A9ELinux联盟

 A9ELinux联盟

(2)shell是一个(编程)语言A9ELinux联盟

 同传统的编程语言一样,shell提供了很多特性,这些特性可以使你的shellscriptA9ELinux联盟

 编程更为有用,如:

数据变量、参数传递、判断、流程控制、数据输入和输出,子A9ELinux联盟

 程序及以中断处理等。

A9ELinux联盟

 A9ELinux联盟

 .在shell编程中使用数据变量可以将程序变量更为通用,如在上面backup.sh中:

A9ELinux联盟

   cd$WORKDIRA9ELinux联盟

   ls*|cpio-o>/dev/rmt/0hA9ELinux联盟

 A9ELinux联盟

 .Shell编程中的注释以#开头A9ELinux联盟

 .对shell变量进行数字运算,使用expr命令A9ELinux联盟

       exprintegeroperatorintegerA9ELinux联盟

       其中operator为+-*/%,但对*的使用要用转义符,如:

A9ELinux联盟

       $expr4*5A9ELinux联盟

       20A9ELinux联盟

       $int=`expr5+7`A9ELinux联盟

       $echo$intA9ELinux联盟

       12A9ELinux联盟

 A9ELinux联盟

(3)Shell编程的参数传递,可通过命令行参数以及交互式输入变量(read)A9ELinux联盟

 A9ELinux联盟

    restoreall.sh对backup.sh程序的备份磁带进行恢复A9ELinux联盟

       $cat>restoreall.shA9ELinux联盟

       cd$WORKDIRA9ELinux联盟

       cpio-i

       ^DA9ELinux联盟

    restore1.sh:

只能恢复一个文件A9ELinux联盟

       #restore1--programtorestoreasinglefileA9ELinux联盟

       cd$WORKDIRA9ELinux联盟

       cpio-i$i

 A9ELinux联盟

       $restore1file1A9ELinux联盟

 A9ELinux联盟

       恢复多个文件restoreany:

A9ELinux联盟

       #restoreany--programtorestoreasinglefileA9ELinux联盟

       cd$WORKDIRA9ELinux联盟

       cpio-i$*

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 哲学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1