Linux学习-shell脚本编程基础(节选)
来源:互联网 发布:unity3d 录屏 编辑:程序博客网 时间:2024/06/13 10:53
第一记
1、 简单的helloworld编写
Shell输入下输入命令:vi helloworld.sh
随后进入文本编辑:
#!/bin/shell#this is ahelloworld testa=”helloworld”echo $a
执行helloworld.sh文件
命令:
# sh helloworld.sh
2、变量赋值的方式是
# variable_name = variable_value
如果对一个已经有值的变量赋值,新值将取代旧值。取值的时候要在变量名前加$,$variable_name可以在引号中使用,如果出现混淆的情况,可以使用花括号来区分,例如:
# echo "Hi, $as"
就不会输出“Hi, helloworlds”,而是输出“Hi,”。这是因为Shell把$as当成一个变量,而$as未被赋值,其值为空。正确的方法是:
$ echo "Hi, ${a}s"
单引号中的变量不会进行变量替换操作。
3.who 命令查看有谁登陆系统
who| wc –l 计算用户总数
4.cat > nusers 建立文件nusers,使用cat复制终端的输入
echo “haha”
然后 ctrl+d表示end of file
使用chmod +x nesers 让文件拥有执行的权限
测试:./nusers
shell识别三种命令:内建命令、shell函数以及外部命令
5.printf和echo的区别
printf不像echo那样能够自动换行
6.重定向与管道
7.shell命令
# who 查看有谁登陆# date查看当前日期# chmod +x file修改file文件,让其成为可执行文件# echo 显示文本字符串内容# cat file显示file文件的内容,和more差不多# ./file执行file文件# set显示完整的环境变量配置列表# testing=`date `反引号的作用:输出值;此处吧date值输出并赋值给testing# $testing$用于输出变量的值# command > outputfile将command命令的结果写入到outputfile文件中(覆盖式写入)# command >> outputfile将command命令的结果写入到outputfile文件中(追加式写入)# wc << EOF提示符一直提示输入数据,知道输入EOF,然后wc命令开始对内联输入重定向提供的数据执行行,词和字节计数。# rpm –qa > rpm.list将已经安装的软件包列表数据输入到rpm.list# sort rpm.list排序rpm.list# rpm –qa | sort通过管道|将两条命令合成一条# expr 1 + 5expr执行加法运算,结果为6#bc进行浮点数运算命令# $?查看最后一条命令的退出状态码
第二记
1.for命令
格式:for var in listdocommandsdone
写一个用for遍历list的程序,如下:
[root@master test]# vi sh1201#!/bin/shfor i in liudiwei xuxu xiao liang haodo echo "this is my friend $i"done测试
[root@master test]# ./sh1201this is my friend liudiweithis is my friend xuxuthis is my friend xiaothis is my friend liangthis is my friend hao
如果list里面有单引号,如I’m a student.
可以使用两种方式来解决:
(1) 使用转义字符 I\’m a student
(2) 使用双引号来定义用到单引号的值。”I’m” a student.
2.while命令
while命令的格式:
while test commanddo other commandsdone
编写testwhile
[root@master test]# vi testwhile#!/bin/shi=0while [ $i -le 10 ]do echo "i=$i" i=$[ $i + 1 ]done
测试:
[root@master test]# ./testwhilei=0i=1i=2i=3i=4i=5i=6i=7i=8i=9i=10
3.util命令
命令格式
until test commandsdo other commandsdone
说明:直到 commands为真,不然就一直执行other commands.
4.嵌套循环输出九九乘法表
编辑一个新文件
[root@master test]# vi mutil99
九九乘法表代码:
#/bin/shfor (( i=1;i<=9;i++))do for ((j=1;j<=i;j++)) do echo -n "$j*$i=$[$i*$j] " done echodone
结果:
[root@master test]# ./mutil991*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
5.break和continue
6.处理循环输出
循环遍历文件夹并判断是一个目录还是一个文件
[root@master test]# vi isdirorfile#/bin/shfor file in /root/*do if [ -d "$file" ] then echo "$file is a directory!" else echo "$file is a file!" fidone > output.txt #讲结果输出到output.txt文件里
结果
[root@master test]# more output.txt/root/anaconda-ks.cfg is a file!/root/dead.letter is a file!/root/downloads is a directory!/root/hadoop is a directory!/root/hello.sh is a file!/root/initial-setup-ks.cfg is a file!/root/src is a directory!/root/test is a directory!/root/testq is a file!
- Linux学习-shell脚本编程基础(节选)
- linux shell脚本编程学习(一)基础
- 【Linux 系统编程】shell 脚本基础学习(一)
- Linux Bash Shell学习(七):shell编程基础——运行Shell脚本、function
- shell 脚本编程学习(一) 基础
- Linux shell 脚本编程基础
- Linux Shell脚本编程基础
- Linux Shell脚本编程基础
- Linux Shell脚本编程基础
- Linux学习(八):shell脚本编程
- Linux学习-shell脚本编程基础之处理用户输入
- Linux Shell编程三:shell脚本基础
- Linux shell脚本基础学习
- Linux shell脚本基础学习
- Linux shell脚本基础学习
- 【shell脚本学习】1.linux shell基础
- 【Linux 系统编程】shell 脚本基础学习之函数(五)
- 嵌入式开发之linux基础第六天学习笔记 shell脚本编程(一)
- Android单点触控技术 旋转 平移 缩放等
- SSH原理与运用(一):远程登录
- 2、设计包含min函数的栈
- JSON解析——图解
- int与long 两种数据类型有什么区别?|__int64固定大小为8字节,不受运行环境(的CPU和操作系统位数)影响
- Linux学习-shell脚本编程基础(节选)
- 尼科彻斯定理
- eclipse编译生成APK如何可以调用平台中的系统函数
- 公共日志过程
- oracle恢复update以后数据的SQL语句
- 变形课
- linux的zero copy
- js实现可编辑表格一
- 关于Linux的进程和线程