Linu shell编程学习实例与参数分析

上传人:汽*** 文档编号:508121670 上传时间:2022-11-24 格式:DOCX 页数:25 大小:49.17KB
返回 下载 相关 举报
Linu shell编程学习实例与参数分析_第1页
第1页 / 共25页
Linu shell编程学习实例与参数分析_第2页
第2页 / 共25页
Linu shell编程学习实例与参数分析_第3页
第3页 / 共25页
Linu shell编程学习实例与参数分析_第4页
第4页 / 共25页
Linu shell编程学习实例与参数分析_第5页
第5页 / 共25页
点击查看更多>>
资源描述

《Linu shell编程学习实例与参数分析》由会员分享,可在线阅读,更多相关《Linu shell编程学习实例与参数分析(25页珍藏版)》请在金锄头文库上搜索。

1、Linux shell编程学习实例与参数分析 第一章:shell基础umask-查看当前用户创建文件或文件夹时的默认权限eg:testszbirdora 1$umask0002testszbirdora 1$ls -lh-rw-rw-r-test test myfiledrwxrwxr-xtest test 1上面的例子中我们看到由test默认创建的文件myfile和文件夹1的权限分别为 664, 775.而通过umask查到的默认权限为002.所以可以推断出umask的计算算umaskfile0615243342512601700法为:directory76543连接ln硬连接 lnsour

2、cefiletargetfile 小和source文件一样软连接 ln-ssourcefiletargetfile 式连接后的target文件大类似于windows的快捷方shell script 基本结构 #!/bin/bash# description了阅读方便最好加以说明) variable name=value-controlsegment顺序eg.helloworld.sh#! /bin/bash# This is a helloworld shell script printchar = hello worldecho $printcharbash shell开头必须部分注释部分(

3、可有可无,为-变量部分,声明变量,赋值流程控制结构,如判断、循环、testszbirdora 1$sh helloworld.sheg. ls cat myfile 列举出 cat myfile 的输出项后台运行重定向nohup command&可通过jobs T查看后台运行的脚本,可以改变程序运行的输出来源和输入来源 变量 量 特殊字符可以用$varname来调用变、用来替换命令用来使shell无法认出其后的特殊字符,使其失去特殊含义;允许一行放多个命令 ()创建成组的命令 ? 创建命令块?第二章:变量和运算符shell 特性别名alias管道a |beg. ls |sort将ls列举的项排

4、序命令替换a、b、hello worldeg. alias ll = “ls -l”将a命令的输出作为b命令的输入将b命令的输出作为a命令的输入本地变量:在用户现在的shell生命期的脚本中使用。设置变量: various_name二value.可用set 来查看。用readonly可以使变量只读。环境变量:用于当前用户下所有用户进程(不限于现在的shell)。设置变量:export various_name二value。用 env 查看。 用readonly可以使变量只读。变量替换显示实际值到variable name如果设置了 variable name,则显示其值,如果未设置variab

5、le name,则显现用户如果未设置,则显示其值 如果未设置,则设置其值,并显示unset variable nameecho $variable nameecho $variable name:+value 否则为空echo $variable name:?value 定义错误信息valueecho $variable name:-value echo $variable name:=value 清除变量位置变量位置变量表示$0,$1,$2.$9$0 脚本名字$1 根据参数位置表示参数1eg.#! /bin/bash#parm.shecho This is script name : $0

6、echo This is parameter 1: $1 echo This is parameter 2: $2 testszbirdora 1$sh parm.sh a b This is script name : parm.sh This is parameter 1: a This is parameter 2: b 向系统中传递位置变量 #!/bin/bash#parm.shfind /u01/test/1 -name $1 -printtestszbirdora 1$ sh parm.sh myfile/u01/test/1/myfile 标准变量量,可在/etc/profile

7、中定义EXINITHOMEIFSLOGNAME MAIL MAILPATHPATHTERMTZPS1testszbirdora 1$ testszbirdora 1$ echo $PS1 uh W$-W -documentPS2 提示,如 PWD MAILCHECK 邮件testszbirdora 1$ echo $MAILCHECK 60SHELLMANPATHTERMINFO特殊变量$# 传递到脚本的参数个数bash 默认建立了一些标准环境变-当前登录用户名-终端信息- 时区- 登录提示,如-u -user -h -host- 一命令多行,换行- 当前目录 每隔多少秒检查是否有新- 帮助文

8、档位置-终端信息$*以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个$脚本运行的当前进程ID号$!后台运行的最后一个进程的进程ID号$传递到脚本的参数列表,并在引号中返回每个参数$-显示 shell 使用的当前选项,与 set 命令功能相同$?显示最后命令的退出状态,0 表示没有错误,其他表示有错误eg.#!/bin/bash#parmecho this is shellname: $0echo this is parm1 : $1echo this is parm2 : $2echo show parm number : $#echo show parm list :

9、$*echo show process id: $echo show precomm stat: $?testszbirdora 1$ sh parm.sh a bthis is shellname: parm.shthis is parm1 : athis is parm2 : bshow parm number : 2show parm list : a bshow process id: 24544show precomm stat: 0 影响变量的命令 declare 设置或显示变量-f只显示函数名-r创建只读变量-x创建转出变量-i创建整数变量使用+替代-,可以颠倒选项的含义 exp

10、ort-p 显示全部全局变量shiftn移动位置变量,调整位置变量,使$3 赋予$2,使$2赋予$1n 前移ntypeset 和 declare 同义 注意:双引号不能解析$,三个字符,所以在双引号中可以引用变量、转义字 符、替换变量单引号可以解析,所以单引号中引用变量等无效testszbirdora 1$ echo $testtesttestszbirdora 1$ echo $test$test 运算符类型1. 按位运算符 取反 右移运算符&与|或八异或$ 表示形式告诉 shell 对方括号中表达式求值 $a+b2. 逻辑运算符&|,mylogfile.txttestszbirdora $

11、 sh echod.shthis echos 3 newlneOKthis is echos 3 ewlinennn上面可以看到有-e则可以解析转移字符,没有不能解析。echo空输出为空2.read 可以从键盘或文件的某一行文本中读入信息,并将其赋给一个变量 read variable1 variable2eg.#!/bin/bash#readnameecho -n first name:read firstnameecho -n last name:read lastnameecho this name is $firstname $lastname3.cat显示文件的内容,创建内容,还可以

12、显示控制字符cat optionsfilename1 filename2-v 显示控制字符( Windows 文件)cat 命令不会分页显示,要分页可以采用 more 、 less4. 管道|5. tee把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中,一般与管道合用tee options files-a 在文件中追加eg.testszbirdora 1$ echo |tee myfiletestszbirdora 1$ cat myfile将 myfile 文件置空 6.文件重定向 commandfilename commandfilename commandfilename&1 准错误重定向 commanddelimiter delimiter 分解符 commandfilename 容到命令 commandnullfile.txt -创建字节为 0 的文件 command1command3 eg.说明:myfile为空间 testszbirdora 1$ testszbirdora 1$ Filesystem /dev/sda1 none /dev/sda2 /dev/sda4 testszbirdora 1$ testszbirdora 1$ F

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号