Shell脚本编程入门(一)
来源:互联网 发布:用友网络 编辑:程序博客网 时间:2024/05/21 01:32
最近在学shell,记录一下。
if语句的使用:
1.判断两个参数大小
#!/bin/sh#a test about if statementa=10b=20if [ $a -eq $b ];thenecho "parameter a is equal to parameter b"elif [ $a -le $b ];thenecho "parameter a is less than parameter b"elif [ $a -gt $b ];thenecho "parameter a is greater than parameter b"elseecho "i don't know the result!"fi
2.执行脚本时动态传递参数
$1、$2、$3...分别代表接收到的参数$0 表示程序的名称$# 传递给程序的总的参数数目 $? 上一个代码或者shell程序在shell中退出的情况,如果正常退出则返回0,反之为非0值$* 传递给程序的所有参数组成的字符串$@ 以"参数1" "参数2" ... 形式保存所有参数 $$ 本程序的(进程ID号)PID $! 上一个命令的PID
脚本
#!/bin/sh#a test about if statementa=$1b=$2if [ $a -eq $b ];thenecho "parameter a is equal to parameter b"elif [ $a -le $b ];thenecho "parameter a is less than parameter b"elif [ $a -gt $b ];thenecho "parameter a is greater than parameter b"elseecho "i don't know the result!"fi
执行效果:
3.for循环的使用
#!/bin/bash#a test about for and while statementfor i in {1..5}do echo "hello world"$idone
注意:这里sh不支持这种写法,要用bash来运行
sh支持这种写法:
#!/bin/sh#a test about for and while statementfor i in 1 2 3 4 5do echo "hello world"$idone
4.在/root/test/test2文件夹中创建100文件夹,名称为test1~test100
#!/bin/bash#create 100 folder in /root/test/test2for i in {1..100}do`mkdir ./test2/test$i`done
5.编写乘法表,根据输入参数来输出某个数的乘法表
#!/bin/bashfor((i=1;i<=$1;i++)){ for((j=1;j<=${i};j++)){ ((ret=${i}*${j})) echo -ne ${i}*${j}=$ret"\t" } echo}
注意:参数中的-n表示输出后不换行,e表示支持转义字符
运行效果:
0 0
- Shell脚本编程入门(一)
- Linux脚本(shell)编程(一) 简单入门HelloWorld
- Linux脚本(shell)编程(一) 简单入门HelloWorld
- Linux shell脚本编程入门 (一)
- shell脚本编程(一)
- shell脚本编程(一)
- shell脚本编程(一)
- shell脚本编程(一)
- Shell入门(一)Shell脚本、环境
- shell脚本编程入门
- SHELL编程入门(一)
- shell编程入门(一)
- shell编程(一)--入门
- shell脚本编程(一)
- Shell脚本入门(一) - 简介
- Shell脚本编程-流控制(一)
- shell脚本编程实例(一)
- Shell 脚本编程总结(一)
- Leetcode46: Power of Two
- #136 Single Number
- hbase-1.0.1的60010页面无法打开
- xcode debug 2
- AC自动机
- Shell脚本编程入门(一)
- STL算法设计理念 - 预定义函数对象
- emgu在VS2010下可能出现的几种常见问题解决方案
- Struts2.之HelloWorld简单演示
- 使用 TFDConnection 的 pooled 连接池
- span中内容随着数字长度的增加而增大
- 寻找所有的素数的python实现
- 编程10大算法概念汇总
- Eclipse 设置调优使用