Linux shell编程学习笔记.docx

上传人:b****1 文档编号:1273289 上传时间:2023-04-30 格式:DOCX 页数:14 大小:20.53KB
下载 相关 举报
Linux shell编程学习笔记.docx_第1页
第1页 / 共14页
Linux shell编程学习笔记.docx_第2页
第2页 / 共14页
Linux shell编程学习笔记.docx_第3页
第3页 / 共14页
Linux shell编程学习笔记.docx_第4页
第4页 / 共14页
Linux shell编程学习笔记.docx_第5页
第5页 / 共14页
Linux shell编程学习笔记.docx_第6页
第6页 / 共14页
Linux shell编程学习笔记.docx_第7页
第7页 / 共14页
Linux shell编程学习笔记.docx_第8页
第8页 / 共14页
Linux shell编程学习笔记.docx_第9页
第9页 / 共14页
Linux shell编程学习笔记.docx_第10页
第10页 / 共14页
Linux shell编程学习笔记.docx_第11页
第11页 / 共14页
Linux shell编程学习笔记.docx_第12页
第12页 / 共14页
Linux shell编程学习笔记.docx_第13页
第13页 / 共14页
Linux shell编程学习笔记.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Linux shell编程学习笔记.docx

《Linux shell编程学习笔记.docx》由会员分享,可在线阅读,更多相关《Linux shell编程学习笔记.docx(14页珍藏版)》请在冰点文库上搜索。

Linux shell编程学习笔记.docx

Linuxshell编程学习笔记

Linuxshell编程学习笔记

(一)

第一章:

shell基础

●umask  --查看当前用户创建文件或文件夹时的默认权限

eg:

[test@szbirdora1]$umask

0002

[test@szbirdora1]$ls-lh

-rw-rw-r--    testtest  myfile   

drwxrwxr-x    testtest1

上面的例子中我们看到由test默认创建的文件myfile和文件夹1的权限分别为664,775.而通过umask查到的默认权限为002.所以可以推断出umask的计算算法为:

umask                 file                     directory

0                          6                           7

1                          5                            6

2                          4                           5

3                          3                           4

4                           2                           3

5      1          2

6      0          1 

7      0           0

●连接ln

硬连接lnsourcefiletargetfile                 连接后的target文件大小和source文件一样

软连接ln-ssourcefiletargetfile             类似于windows的快捷方式

●shellscript基本结构

#!

/bin/bash                         --------bashshell开头必须部分

#description                         --------注释部分(可有可无,为了阅读方便最好加以说明)

variablename=value            ---------变量部分,声明变量,赋值

controlsegment                   ---------流程控制结构,如判断、循环、顺序

eg.

helloworld.sh

#!

/bin/bash

#Thisisahelloworldshellscript

printchar="helloworld"

echo$printchar

[test@szbirdora1]$shhelloworld.sh

helloworld

●shell特性

①别名         alias                 eg.aliasll=“ls-l”

②管道         a|b                  将a命令的输出作为b命令的输入eg.ls|sort  将ls列举的项排序

③命令替换  a`b`               将b命令的输出作为a命令的输入eg.  ls`catmyfile`列举出catmyfile的输出项

④后台运行  nohupcommand&   可通过jobs-l查看后台运行的脚本

⑤重定向      >,<                     可以改变程序运行的输出来源和输入来源

⑥变量                                      可以用$varname来调用变量

⑦特殊字符

                                        `用来替换命令

                   \用来使shell无法认出其后的特殊字符,使其失去特殊含义

                   ;允许一行放多个命令

                   ()创建成组的命令  ?

                   {}创建命令块      ?

    

第二章:

变量和运算符

●本地变量:

在用户现在的shell生命期的脚本中使用。

设置变量:

various_name=value.可用set来查看。

用readonly可以使变量只读。

●环境变量:

用于当前用户下所有用户进程(不限于现在的shell)。

                     设置变量:

exportvarious_name=value。

用env查看。

                     用readonly可以使变量只读。

●变量替换   

echo${variablename}                  显示实际值到variablename

echo${variablename:

+value}    如果设置了variablename,则显示其值,否则为空

echo${variablename:

?

value}    如果未设置variablename,则显现用户定义错误信息value

echo${variablename:

-value}     如果未设置,则显示其值

echo${variablename:

=value}     如果未设置,则设置其值,并显示

●清除变量                                   unsetvariablename

●位置变量

位置变量表示$0,$1,$2...$9

$0----脚本名字

$1----根据参数位置表示参数1

eg.

#!

/bin/bash

#parm.sh

echo"Thisisscriptname:

$0"

echo"Thisisparameter1:

$1"

echo"Thisisparameter2:

$2"

[test@szbirdora1]$shparm.shab

Thisisscriptname:

parm.sh

Thisisparameter1:

a

Thisisparameter2:

b

●向系统中传递位置变量

#!

/bin/bash

#parm.sh

find/u01/test/1-name$1-print

[test@szbirdora1]$shparm.shmyfile

/u01/test/1/myfile

●标准变量                              bash默认建立了一些标准环境变量,可在/etc/profile中定义

EXINIT

HOME

IFS

LOGNAME                                      --当前登录用户名

MAIL

MAILPATH

PATH

TERM                                              --终端信息

TZ                                                  --时区

PS1                                                --登录提示,如[test@szbirdora1]$

[test@szbirdora1]$echo$PS1

[\u@\h\W]\$                                   --\u-user--\h-host--\W-document

PS2                                               --一命令多行,换行提示,如>

PWD                                               --当前目录

MAILCHECK                                  --每隔多少秒检查是否有新邮件

[test@szbirdora1]$echo$MAILCHECK

60

SHELL

MANPATH                                      --帮助文档位置

TERMINFO                                    --终端信息

●特殊变量

$#         传递到脚本的参数个数

$*         以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个

$$         脚本运行的当前进程ID号

$!

         后台运行的最后一个进程的进程ID号

$@        传递到脚本的参数列表,并在引号中返回每个参数

$-         显示shell使用的当前选项,与set命令功能相同

$?

        显示最后命令的退出状态,0表示没有错误,其他表示有错误

eg.

#!

/bin/bash

#parm

echo"thisisshellname:

$0"

echo"thisisparm1:

   $1"

echo"thisisparm2:

   $2"

echo"showparmnumber:

$#"

echo"showparmlist:

  $*"

echo"showprocessid:

  $$"

echo"showprecommstat:

$?

"

[test@szbirdora1]$shparm.shab

thisisshellname:

parm.sh

thisisparm1:

   a

thisisparm2:

   b

showparmnumber:

2

showparmlist:

  ab

showprocessid:

  24544

showprecommstat:

0

●影响变量的命令

declare设置或显示变量

     -f    只显示函数名

      -r   创建只读变量

     -x    创建转出变量

      -i   创建整数变量

     使用+替代-,可以颠倒选项的含义

export

     -p  显示全部全局变量

shift[n]   移动位置变量,调整位置变量,使$3赋予$2,使$2赋予$1    n前移n

typeset    和declare同义

注意:

双引号不能解析$,\,`三个字符,所以在双引号中可以引用变量、转义字符、替换变量

单引号可以解析,所以单引号中引用变量等无效

[test@szbirdora1]$echo"$test"

test

[test@szbirdora1]$echo'$test'

$test

●运算符类型

⒈按位运算符

~   取反

<<  左移运算符

>>  右移运算符

&   与

|    或

^    异或

$[]   表示形式告诉shell对方括号中表达式求值$[a+b]

2.逻辑运算符

&&

||

>,<,=,!

=

3.赋值运算符

letvariablename1+=variablename1+varablename2

第三章shell的输入和输出

1.echo   echo[option]string

           -e解析转移字符

          -n回车不换行,linux系统默认回车换行

           转移字符\c\t\f\n

#!

/bin/bash

#echo

echo-e"thisecho's3newlne\n\n\n"

echo"OK"

echo

echo"thisisecho's3ewline\n\n\n"

echo"thislogfilehaveallbeendone">mylogfile.txt

[test@szbirdora~]$shechod.sh

thisecho's3newlne

OK

thisisecho's3ewline\n\n\n

上面可以看到有-e则可以解析转移字符,没有不能解析。

echo空输出为空

2.read可以从键袒蛭募哪骋恍形谋局卸寥胄畔ⅲ⒔涓掣桓霰淞?

br>readvariable1variable2

eg.

#!

/bin/bash

#readname

echo-n"firstname:

"

readfirstname

echo-n"lastname:

"

readlastname

echo"thisnameis$firstname$lastname"

3.cat       显示文件的内容,创建内容,还可以显示控制字符

           cat[options]filename1filename2

                  -v  显示控制字符(Windows文件)

            cat命令不会分页显示,要分页可以采用more、less

4.管道|

5.tee     把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中,一般与管道合用

             tee[options]files

             -a在文件中追加

eg.

[test@szbirdora1]$echo|teemyfile

[test@szbirdora1]$catmyfile

将myfile文件置空

6.文件重定向

command>filename                                       ---覆盖输出

command>>filename                                     ---追加输出

command>filename>&1                                ---把标准输出和标准错误重定向

command<

command

command<-                                                    ---关闭标准输入

>nullfile.txt                                                      ---创建字节为0的文件

command1command3              ---按从左到右顺序执行

eg.

说明:

myfile为空间

[test@szbirdora1]$df-lh>myfile

[test@szbirdora1]$catmyfile

Filesystem           SizeUsedAvailUse%Mountedon

/dev/sda1             20G3.3G  16G18%/

none                 2.0G    02.0G  0%/dev/shm

/dev/sda2             79G  17G  59G23%/u01

/dev/sda4             28G3.9G  22G15%/u02

[test@szbirdora1]$df-lh>myfile

[test@szbirdora1]$catmyfile

Filesystem           SizeUsedAvailUse%Mountedon

/dev/sda1             20G3.3G  16G18%/

none                 2.0G    02.0G  0%/dev/shm

/dev/sda2             79G  17G  59G23%/u01

/dev/sda4             28G3.9G  22G15%/u02

[test@szbirdora1]$df-lh>>myfile

[test@szbirdora1]$catmyfile

Filesystem           SizeUsedAvailUse%Mountedon

/dev/sda1             20G3.3G  16G18%/

none                 2.0G    02.0G  0%/dev/shm

/dev/sda2             79G  17G  59G23%/u01

/dev/sda4             28G3.9G  22G15%/u02

Filesystem           SizeUsedAvailUse%Mountedon

/dev/sda1             20G3.3G  16G18%/

none                 2.0G    02.0G  0%/dev/shm

/dev/sda2             79G  17G  59G23%/u01

/dev/sda4             28G3.9G  22G15%/u02

[test@szbirdora1]$cat>>myfile<

>China

>Hubei

>Suizhou

>exit

[test@szbirdora1]$catmyfile

China

Hubei

Suizhou

7.exec       可以用来替代当前shell。

现有任何环境变量都会清除

第四章控制流结构

1.if语句

if条件1

then

    命令1

elif条件2

then

    命令2

else

    命令3

fi

------------------

if条件

then命令

fi

eg:

#!

/bin/bash

#iftest

#thisisacommentline

if["10"-lt"12"];then

#yes10islessthan12

echo"yes,10islessthan12"

else

echo"no"

fi

注意:

if语句必须以fi终止

  "10"前一个空格,“12”后也有一个空格。

这个条件都是通过test命令来指定。

条件表达为testexpression或者[expression]

条件表达式中的比较函数

mantest

NAME

      test-checkfiletypesandcomparevalues

SYNOPSIS

      testEXPRESSION

      [EXPRESSION]

      [OPTION

DESCRIPTION

      ExitwiththestatusdeterminedbyEXPRESSION.

      --helpdisplaythishelpandexit

      --version

             outputversioninformationandexit

      EXPRESSIONistrueorfalseandsetsexitstatus.Itisoneof:

      (EXPRESSION)

             EXPRESSIONistrue

      !

EXPRESSION

             EXPRESSIONisfalse

      EXPRESSION1-aEXPRESSION2

             bothEXPRESSION1andEXPRESSION2aretrue

      EXPRESSION1-oEXPRESSION2

             eitherEXPRESSION1orEXPRESSION2istrue

      [-n]STRING

             thelengthofSTRINGisnonzero

      -zSTRING

             thelengthofSTRINGiszero

      STRING1=STRING2

             thestringsareequal

      STRING1!

=STRING2

              thestringsarenotequal

      INTEGER1-eqINTEGER2

             INTEGER1isequaltoINTEGER2

      INTEGER1-geINTEGER2

             INTEGER1isgreaterthanorequaltoINTEGER2

      INTEGER1-gtINTEGER2

             INTEGER1isgreaterthanINTEGER2

      INTEGER1-leINTEGER2

             INTEGER1islessthanorequaltoINTEGER2

      INTEGER1-ltINTEGER2

             INTEGER1islessthanINTEGER2

      INTEGER1-neINTEGER2

             INTEGER1isnotequaltoINTEGER2

      FILE1-efFILE2

             FILE1andFILE2havethesamedeviceandinodenumbers

    

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

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2