介于操作系统内核和用户之间的一个绝缘层,一种被称为解释性语言或者脚本语言的计算机语言。通过将系统调用、公共程序、工具和编译过的二进制程序”粘合“在一起来建立应用,所有的UNIX命令和工具再加上公共程序,对于shell脚本来说,都是可调用的

        

         .sh 文档里

#!/bin/bash 确定文件类型(可能被存放在 /sbin 、/usr/local/bin 、/usr/bin 、/usr/sbin 或 /usr/local/sbin 这样的目录下) 用来注释  exit 0 用来结尾  echo把字符串输出              到标准的输出中

sh bash source(.)执行脚本 脚本所有者执行权限chmod u+rx xx.sh 再用./xx.sh执行

重定向 > 覆盖

使用脚本清除文件 s=路径   cd $s    cat /dev/null >wtmp(sudo sh –c ” cat /dev/null >wtmp”) exit(sudo ./xx.h)管理权限

 

 Bash 特殊字符

           

           ;echo hello; echo there同一行写多个命令

:空命令 while :等价于while true 在if/then占位then :

变量扩展和子串替换:>xx.sh(覆盖) >>  用于注释

?在双括号里面三元运算((:))$引用变量的值

            

          (a=xx;)内部变量是在子shell 脚本本身不能引用

{}复制 t.txt 的内容到 t.back 中    cp t.{txt,back}

[] if语句

<>重定向 >覆盖(>>) &> est.sh 的 stdout(标准输出)和 stderr(标准错误)到 filename 中  >&2 重定向 test.sh 的 stdout 到stderr 中。

|  ls -l| ./test.sh大写输出#!/bin/bash  tr 'a-z' 'A-Z' exit0

-   #!/bin/bash

BACKUPFILE=backup-$(date +%m-%d-%Y)

# 在备份文件中嵌入时间.

archive=${1:-$BACKUPFILE}

#  如果在命令行中没有指定备份文件的文件名,

#+ 那么将默认使用"backup-MM-DD-YYYY.tar.gz".

tar cvf - `find . -mtime -1 -type f -print`> $archive.tar

gzip $archive.tar

echo "Directory $PWD backed up in archivefile \"$archive.tar.gz\"."

exit 0

变量与参数

变量

$variable事实上只是 ${variable}的简写形式引用变量的值n<10括号可以省略{}帮助

变量名和等号之间不能有空格。同时,变量名的命名须遵循如下规则:

首个字符必须为字母(a-z,A-Z)。

中间不能有空格,可以使用下划线(_)。

不能使用标点符号。

不能使用bash里的关键字(可用help命令查看保留关键字)。

除了显式地直接赋值,还可以用语句给变量赋值,如:for file in `ls /etc`

只读变量  readonly xx

局部变量环境变量

位置参数 $1 …. ${10}…

$# : 传递到脚本的参数个数

$* : 以一个单字符串显示所有向脚本传递的参数。与位置变量不同,此选项参数可超过 9个

$$ : 脚本运行的当前进程 ID号

$! : 后台运行的最后一个进程的进程 ID号

$@ : 与$#相同,但是使用时加引号,并在引号中返回每个参数

$: 显示shell使用的当前选项,与 set命令功能相同

$? : 显示最后命令的退出状态。 0表示没有错误,其他任何值表明有错误。

基本运算符

A=`expr $a + $b` expr表达式计算工具(awk)

表达式和运算符之间有空格  判断在[]  ==与变量有空格

关系运算符

逻辑运算符

            

字符串运算符

            

文件测试运算符

流程控制

    If-else

then没内容事加:

if [ condition ]
       then 
              if [ condition ]
             then 
                                commands1
             else    
                                 commands2
              fi
     else
             commands3
       fi

方式二

a=10

b=20

if [ $a == $b ]

then

echo "a == b"

elif [ $a -gt $b ]

then

echo "a > b"

elif [ $a -lt $b ]

then

echo "a < b"

else

echo "Ineligible" fi

 

多条件表示:

逻辑与

if [ condition1 -a condition2 ]

或   if [ condition1 ]&& [ condition2 ]

逻辑或

if [ condition1 -o condition2 ]

或   if [ condition1 ] || [condition2 ]

逻辑非(取反)

!

       For循环

for loop in 1 2 3 4 5

do

echo "The value is: $loop"

done

输出结果:

The value is: 1

The value is: 2

The value is: 3

The value is: 4

The value is: 5

 

      While

while(( $int<=5 ))

do

echo $int

let "int++" //减少$添加

done

while循环可用于读取键盘信息。下面的例子中,输入信息被设置为变量MAN,按结束循环。

echo 'press <CTRL-D> exit'

echo -n 'Who do you think is the most handsome:'

while read MAN

do

echo "Yes!$MAN is really handsome"

done

   无限循环

while:

do

command

done

或者

while true

do

command

done

或者

for (( ; ; ))

until循环  until condition

do

command

done

case选择

              echo'Enter a number between 1 and 4:'

echo 'The number you entered is:'

readaNum

case $aNum in

1) echo'You have chosen 1' 变量和常数 支持逻辑符号

;;

2) echo 'You have chosen 2'

;;

3) echo'You have chosen 3'

;;

4) echo 'You have chosen 4'

;;

*) echo'You did not enter a number between 1 and 4' ;;

Esac

跳出循环  break continue

 

 

函数

[ function ] funname [()]

{

action;           返回数值0-255

[return int;]   :return返回 或返回最后一条指令运行结果

}

例如#!/bin/bash

funWithReturn(){

echo "This function will add the twonumbers of the input..."

echo "Enter the first number: "

read aNum

echo "Enter the second number: "

read anotherNum

echo "The two numbers are $ aNum and $anotherNum !"

return $(($aNum+$anotherNum))

}

funWithReturn

echo "The sum of the two numbers enteredis $? !"

 

       函数参数  小于10不加{}

          #!/bin/bash

funWithParam(){

echo "The first parameter is $1!"

echo "The second parameter is $2!"

echo "The tenth parameter is $10!"

echo "The tenth parameter is ${10}!"

echo "The eleventh parameter is ${11}!"

echo "The total number of parametersis $# !"

echo "Outputs all parameters as astring $* !"

}

funWithParam1 2 3 4 5 6 7 8 9 34 73

输出结果:

The firstparameter is 1 !

The secondparameter is 2 !

The tenthparameter is 10 !

The tenthparameter is 34 !

The eleventhparameter is 73 !

The totalnumber of parameters is 11 !

Outputs allparameters as a string 1 2 3 4 5 6 7 8 9 34 73 !

bash命令解释器(shell)相关推荐

  1. BASH命令和SHELL脚本学习

    BASH命令和SHELL脚本学习 转载于:https://www.cnblogs.com/huolong123/p/6228049.html

  2. linux的命令解释器-----shell

    什么是shell Shell是系统的         用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行 . 实际上Shell是一个命令解释器,它解释由用户输入 ...

  3. 【Shell 脚本进阶】使用 bash 命令对 shell 脚本进行调试

    文章目录 1. 调试常用选项 2. 输出调试信息 2.1 复杂语句展开 2.2 行号信息显示 3. 调试日志打印 4. 常见错误处理 4.1 不存在的变量 4.2 检查语法错误 4.3 发生错误时终止 ...

  4. C++基础课 —命令解释器(SHELL)的用法

    今天听的李汝波老师的课,虽然Ubuntu.Shell也都接触过,不过今天讲的非常的细,收获还是蛮多的,shell指令很久没用了,如果不讲课直接让我想的话,可能会比较生疏,记得不是很清楚了,不过现在都没 ...

  5. linux shell编程 ppt,Linux常用命令与Shell基本编程.ppt

    Linux常用命令与Shell基本编程.ppt Shell 脚本基本编程,无线产品部 katanazhang 2009-11-09,课程目标,linux 常用命令 shell 脚本编程 awk 的用法 ...

  6. bash命令的使用方法

    小编给大家分享一下bash命令的使用方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!  Bash(Bash)是 Bour ...

  7. bash命令使用详解

    这篇文章是分享的关于bash命令使用的技巧和方法,有需要的小伙伴可以学习下. 在Linux上采用bash作为标准,它描述了对带有".sh"扩展名的vim编辑器等文本的处理与执行. ...

  8. 关于bash -c “bash命令“ 和 python -c “python命令“的用法

    一.bash -c "bash命令" 使用shell去运行脚本,通常有两种方法: 一种是 bash xxx.sh 另外一种就是 bash -c "cmd string&q ...

  9. shell脚本删除冒号空格_从Bash漏洞学Shell脚本(冒号)

    前天,爆发了Bash安全漏洞,非常恐怖.在网络上开始飞速传播,附带了非常友好的检测工具. $ env x='() { :;}; echo vulnerable' bash -c "echo ...

最新文章

  1. 百万数据修改索引,百万数据修改主键
  2. 【Java 网络编程】UDP 服务器 客户端 通信 ( DatagramSocket | DatagramPacket | UDP 发送数据包 | UDP 接收数据包 | 端口号分配使用机制 )
  3. Office 365:如何管理Office 365中的Ophaned Mailbox
  4. CAN 多于8字节的拆包组包协议
  5. 游戏编程设计模式-state
  6. 用c语言编译二叉树,C语言实现二叉树的基本操作
  7. 中继器、集线器、网桥、网关产品介绍
  8. mysql 岩机_mysql数据库基本应用
  9. ECNU·AntNLP主页船新上线!
  10. 神经网络优化中的病态问题
  11. Java编程思想(一)
  12. Edraw Max(亿图图示):新手如何快速美化思维导图?
  13. Shell - cp
  14. 聊聊DevOps制品管理-不止是存储制品这么简单
  15. Python数据分析实战之一--某网站二手房交易信息(数据处理)
  16. SonarQube的安装部署过程中踩过的坑
  17. c++生成DLL文件(visual studio 2019)面向小白萌新
  18. 性能功能LocustJmeter LoadRunner优缺点
  19. linux蓝牙串口 rfcomm,蓝牙RFCOMM通信
  20. 推荐8款插件,浏览器使用体验上升500%

热门文章

  1. 恰如其分的批评与莫名其妙的表扬
  2. window下的MySQL基础知识
  3. 2019CCPC女生赛
  4. Learning Actor Relation Graphs for Group Activity Recognition 论文阅读
  5. 在学Python+web,看到这句话,崩溃了……
  6. 远程桌面清理历史记录
  7. 软考信息系统项目管理师和各种考试的比较
  8. 联想笔记本电脑连接不到无线网络的解决办法
  9. 高频交易数据如何产生和处理?
  10. 修改C盘系统文件 Hosts 两种方法