bash命令解释器(shell)

介于操作系统内核和用户之间的一个绝缘层,一种被称为解释性语言或者脚本语言的计算机语言。通过将系统调用、公共程序、工具和编译过的二进制程序”粘合“在一起来建立应用,所有的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=KaTeX parse error: Expected '}', got 'EOF' at end of input: {1:-BACKUPFILE}

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

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

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

gzip $archive.tar

echo “Directory KaTeX parse error: Can't use function '\"' in math mode at position 30: …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 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 !
小编整理了一些java进阶学习资料和面试题,需要资料的请加JAVA高阶学习Q群:701136382 这是小编创建的java高阶学习交流群,加群一起交流学习深造。群里也有小编整理的2019年最新最全的java高阶学习资料!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值