Shell笔记
来源:互联网 发布:跟易企秀类似的软件 编辑:程序博客网 时间:2024/05/20 23:55
shell:shell是一个内核,是一个拥有很多命令的集合,不需要编译。
bash:bash是shell中的一种。
shell脚本:是一个以.sh结尾的文件。内部是一些shell命令组合,可被执行。
备注使用“#”,后面的内容为备注内容。
变量:变量引用:“$变量名称”,如$PATH、$BASH;
变量赋值:如:age=18,不能有空格,否则会报错。
获取用户输入:“read name”,那么用户的输入将会作为name这个变量的值
打印:echo
提示语: read -p “How old are you:” age ,-p 意思是提示
-s是输入密码类型,输入过程不被看见
参数:脚本中传入参数语句:args("$@") ,执行脚本语句后,接收输入相应的参数
使用参数:${args[1]}、${args[2]}
当脚标为0时,表示执行此脚本的语句,即${args[0]}表示:./xxx.sh
使用参数列表:$@
计算参数个数:$#
if then语句:判断数值型if(( "$args" = 3 )) then ……fi 其中fi表示结束if语句
判断字符串 if[ $args = "abcd" ] then……fi
- 整数比较符
- -eq : (equal to)相等 例如: if [ "$a" -eq "$b" ]
- -ne : (not equal to)相等 例如: if [ "$a" -ne "$b" ]
- -gt : (greater than)大于 例如: if [ "$a" -gt "$b" ]
- -ge : (greater than or equal to)大于或等于 例如: if [ "$a" -ge "$b" ]
- -lt : (less than)小于 例如: if [ "$a" -lt "$b" ]
- -le : (less than or equal to)小于或等于 例如: if [ "$a" -le "$b" ]
- < : 小于 例如: if (( "$a" < "$b" ))
- <= : 小于等于 例如: if (( "$a" <= "$b" ))
- > : 大于 例如: if (( "$a" > "$b" ))
- >= : 大于等于 例如: if (( "$a" >= "$b" ))
- 字符串比较
- = : 等于 例如: if [ "$a" = "$b" ]
- == : 等于 例如: if [ "$a" == "$b" ]
- != : 不等于 例如: if [ "$a" != "$b" ]
- < : 小于(ASCII字母顺序) 例如: if [[ "$a" < "$b" ]]
- > : 大于(ASCII字母顺序) 例如: if [[ "$a" > "$b" ]]
- -z : 字符不为空
if [ -e $file1 ] 判断文件file1是否存在-d 判断是否为目录-f判断是否为文件-s判断文件是否为空-w判断是否有写入权限
逻辑运算:与&& 或||
第一种写法:if[ $age -gt 10 ] &&[ $age -lt 20 ]
第二种写法:[ $age -gt 10 -a $age -lt 20 ]
第三种写法:[[ $age -gt 10 && $age -lt 20 ]]
数学运算:bash默认只支持整型计算,不支持浮点型(如要计算可安装bc)
第一种写法:echo $(( num1 + num2 ))
第二种写法:echo $( expr $num1 + $num2 ) echo $( expr $num1\* $num2 ) #乘法要转义
case用法: 下面这段代码的意思是获取输入的第一个参数,匹配对应的值给相应的结果
- #! /bin/bash
- sex=$1
- case $score in
- "man")
- echo "you are a man" ;;
- "woman")
- echo "you are a woman" ;;
- * )
- echo "Default,not a human" ;;
- esac
数组:
- #! /bin/bash
- os=('linux' 'unix' 'windows' 'Mac')
- os[4]='Android' # add a element
- #unset os[1] # remove a element
- echo "${os[@]}" #print all
- echo "${os[1]}" #print unix
- echo "${!os[@]}" #print 0 1 2 3 4
- echo "${#os[@]}" #print count,5
while循环: 中间可插入一行sleep 1 表示暂停一秒
- #! /bin/bash
- n=1
- sum=0
- while (( $n <= 10 ))
- do
- sum=$((n + sum ))
- (( n++ ))
- done
- =============复制于网络=========================
- for VARIABLE in 12345...N
- do
- command1
- command2
- commandN
- done
- OR=======================================
- for VARIABLE in file1 file2 file3
- do
- command1 on $VARIABLE
- command2
- commandN
- done
- OR=======================================
- for OUTPUT in $(Linux or unix commond here)
- do
- command1 on $OUTPUT
- command2 on $OUTPUT
- commandN
- done
- OR=======================================
- for (( 表达式1; 表达式2; 表达式3 ))
- do
- command1
- command2
- commandN
- done
- =======================================
for循环执行命令:
#! /bin/bashfor command in ls pwd datedoecho"-----------$command----------"$commanddone
- [shell]shell脚本笔记
- shell笔记
- shell笔记
- shell笔记
- shell笔记
- Shell笔记
- Shell笔记
- shell笔记
- Shell 笔记
- shell 笔记
- shell笔记
- Shell 笔记
- shell笔记
- shell笔记
- shell笔记
- shell笔记
- shell笔记
- Shell笔记
- web 前端多语言的支持
- 在ASP.NET中实现多文件上传
- IDEA里面为项目添加编写Groovy支持
- 进制转换
- LeetCode 328. Odd Even Linked List
- Shell笔记
- c#和java语言变量赋值的新方式的畅想
- 针对Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3的解决方案
- 开源项目收集
- ASP.NET 中得到网站绝对路径的几种方法
- 计算机中的正数,负数到底是什么?
- Java Web后端--入职技能任务单(Maven子工程创建)二
- 【第四届蓝桥杯】幻方填空
- osgEarth细节纹理案例 14.detail_texture.earth