shell脚本基础
来源:互联网 发布:mac官网彩妆口红色号 编辑:程序博客网 时间:2024/06/01 07:25
<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}p{mso-margin-top-alt:auto;margin-right:0cm;mso-margin-bottom-alt:auto;margin-left:0cm;mso-pagination:widow-orphan;font-size:12.0pt;font-family:宋体;mso-bidi-font-family:宋体;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:595.3pt 841.9pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;layout-grid:15.6pt;}div.Section1{page:Section1;} /* List Definitions */ @list l0{mso-list-id:2001880180;mso-list-type:hybrid;mso-list-template-ids:-1103085224 655366822 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}@list l0:level1{mso-level-text:"%1/)";mso-level-tab-stop:18.0pt;mso-level-number-position:left;margin-left:18.0pt;text-indent:-18.0pt;}ol{margin-bottom:0cm;}ul{margin-bottom:0cm;}-->
shell脚本如同批处理文件一样,让shell执行一批命令。让你很方便。
这里对shell脚本进行了一些 总结。
1.注释:#!bin/sh。
2.变量:
给变量赋值:a = “helloworld.”
显示变量: echo $a
Echo “${a}heheh”
3.环境变量:由export关键字处理过的变量叫做环境变量
4.shell命令:在shell脚本中可以使用3类命令:unix命令、管道, 重定向和 backtick
Unix命令:这里列举一些unix常用命令:
echo“hello word!”
ls
wc–l filename
cpsf df
mvoldfile newfile
rmfile
grep ‘pattern’ file
cut –b colum file指定欲显示的文件内容范围
例如:cut –b5-9 file.txt,输出每行第5个到第9个字符
sed: Sed是一个基本的查找替换程序,该命令采用正则表达式(见参考)进行搜索
不要和shell中的通配符相混淆,比如:将linuxfocus替换为LinuxFocus:
cat text.file | sed `s/linuxfocus/LinuxFocus/`> newtext.file
awk: awk 用来从文本文件中提取字段。缺省地,字段分割符是空格,可以使用-F指定其他分割符。
cat file.txt | awk –F, `{print $1 “,” $3}` 这里我们使用,作为字段分隔符,同时打印第一个和第三个字段,如果该内容如下:Adam Bor, 34, IndiaKerryMiller,22,USA,命令输出结果为:
Adam Bor, idiaKerry miller,USA
管道,重定向和backtick:这些不是系统命令,但真的很重要。
管道(|)将一个命令的输出作为另外一个命令的输入。
grep “hello” file.txt | wc –l 在file.txt中搜索含有”hello”的行并计算其行数
重定向:将命令的结果输出到文件,而不是标准输出。
> 写入文件并覆盖旧文件
>> 加到文件的尾部,保留旧文件内容。
反短斜线``:使用反短斜线可以将一个命令的输出作为另外一个命令的一个命令行参数。
#!/bin/sh
tar –zcvf lastmod.tar.gz `find . –mtime -1 –typef -print` 查找过去24小时内修改过的文件,将所有查找的文件打一个包。
5流程控制
1)if …; then
elif…; then
….
else
…
fi
大多数情况下,可以使用测试命令来对条件进行测试。比如可以比较字符串、判断文件是否存在及是否可读等等..
2)通常用” [ ] ”来表示条件测试。这里空格很重要,要确保方括号里面的空格。
[ -f “somefile” ]:判断是否是一个文件
[ -x “/bin/ls” ]:判断/bin/ls是否存在并有可执行权限
[ -n “$var” ]:判断$var变量是否有值
[ “$a”= “$b” ]:判断$a和$b是否相等
3)select:select表达式似乎一种bash扩展应用,用户可以从一组不同值中进行选择。
select var in …;do
break
done
下面是一个例子
#!/bin/sh
echo “What is your favorite OS?”
select var in “Linux” “Gnu Hurd” “Free BSD”“Other”; do
break
done
echo “You have selected $var”
下面是该脚本运行结果:
What is your favorite OS?
1) Lunix
2) Gnu Hurd
3) Free BSD
4) Other
#?1
You have selected Linux
5) loop
loop表达式:
while …;do
…
done
for-loop表达式查看一个字符串列表,然后将其赋给一个变量:
for var in ….; do
…
done
- Linux Shell脚本基础
- Linux Shell脚本基础
- Shell脚本编程基础
- Shell脚本编程基础
- shell脚本基础
- shell脚本基础
- shell脚本基础
- shell脚本基础
- Shell脚本编程基础
- shell 脚本基础学习
- shell脚本基础语法
- Linux shell脚本基础
- Shell脚本基础
- shell脚本基础
- Linux shell脚本基础
- Shell脚本基础总结
- shell脚本基础
- Shell脚本基础介绍
- CIT模拟器在呼叫中心中的应用
- 指针总结
- Linux环境变量问题
- 学习电脑。
- Ant入门教程
- shell脚本基础
- Linux内核能够识别的设备,对应源码中Documentation/devices.txt文件(转)
- Struts2中if Tag得注意的问题
- WAVE PCM 声音文件格式
- 关于编译memcached时加--enable-memcached-igbinary报错
- Cisco 生成树协议STP/RSTP
- 位运算各种技巧
- Websphere下修改web.xml注意事项
- 外键约束