linux 脚本教程,shell脚本教程

1.shell脚本-单引号转义

新建一个1.sh文件,内容:

#! /bin/bash

#$0表示文件名

echo '$0的含义':$0

输出:

$0的含义:1.sh

单引号:$0转义,就是$0

如果是双引号:

#! /bin/bash

echo "$0的含义":$0

输出:

1.sh的含义:1.sh

双引号:$0并没有转义,依然表示文件名

2.shell脚本-变量

变量赋值的时候,注意不要有空格

#正确

a="aaaa"

#错误

a = "aaaa"

#变量引用,前面加$

echo $a

3.shell脚本-获取输入

#! /bin/bash

echo "enter your name:"

read name

echo 'your name is :' $name

read 在下一行显示输入信息(输入joke)

显示:

enter your name:

joke

your name is : joke

read -p (- p 是 promote,提示的意思,就是可以让用户在提示语相同一行输入内容)

#! /bin/bash

read -p "enter your name:" name

echo 'your name is :' $name

显示:

nter your name:joke

your name is : joke

read -sp ( 一般在密码输入的时候用到,输入过程看不到输入的内容)

#! /bin/bash

read -sp "enter your password:" pw

echo 'your name is :' $pw

显示:

enter your password:

#上面这一行提示你输入,但是看不到输入的字符

#echo显示内容

enter your password:your name is : 123

4.shell脚本-参数

1.变量说明:

$$ ——Shell 本身的 PID(ProcessID)

$! ——Shell 最后运行的后台 Process 的 PID

$? —— 最后运行的命令的结束代码(返回值)

$- —— 使用 Set 命令设定的 Flag 一览

$* —— 所有参数列表。如 "$*" 用「"」括起来的情况、以"$1 $2 … $n" 的形式输出所有参数。

$@ —— 所有参数列表。如 "$@" 用「"」括起来的情况、以"$1""$2" … "$n" 的形式输出所有参数。

$# —— 添加到 Shell 的参数个数

$0 ——Shell 本身的文件名

$1~$n —— 添加到 Shell 的各参数值。$1 是第 1 参数、$2 是第 2 参数…。

2.$@与$* 异同

1)、$@与 $* 没有双引号2个同义

但是每个参数都是一个独立的 "" 引用字串,这就意味着参数被完整地传递,并没有被解释和扩展。这也意味着,每个参数列表中的每个参数都被当成一个独立的

2)、$@ 和 $* 只在被双引号包起来的时候才会有差异

"$*" 将所有的参数认为是一个字段

"$@"以 IFS(默认为空格)来划分字段,如果空格在 “” 里面,不划分。采用 LS 的脚本运行./test 1 "2 3" 4 来发现差异

将$@与 $* 作为 数组, {arg[0]}表示第一个元素,{arg[0]}当 "$*" 有双引号就表示全部参数,由此看出差异:

#! /bin/bash

arg=("$@")

echo ${arg[0]} #打印第一个参数

echo $@

echo "=============="

arg=("$*")

echo '$*'有双引号: ${arg[0]}

echo $*

echo "------------下面结果一样--------------"

arg=($@)

echo ${arg[0]}

echo $@

echo "=============="

arg=($*)

echo '$*'没有双引号: ${arg[0]}

echo $*

运行文件

三个参数 a,b,c

bash 1.sh a b c

显示:

a

a b c

==============

$*有双引号: a b c

a b c

------------下面结果一样--------------

a

a b c

==============

$*没有双引号: a

a b c

5.shell脚本-if-then 语句与比较运算符

最重要就是记住括号等运用

注意:

中括号[] 与字符之间一定要保留一个空格,否则报错

整数比较符

-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 : 字符不为空

6.shell脚本-case

1)、星号 (*) 相当于其他语言中的 default;

2)、双分号 (;;) 是必须的,相当于 java 中的 break;

3)、竖线 (|) 用于分割多个模式,相当于 or;

echo 'Input a number:'

read Num

case $Num in

1) echo 'You select 1'

;;

2) echo 'You select 2'

;;

3) echo 'You select 3'

;;

4|5) echo 'You select 4 or 5'

;;

*) echo 'default'

;;

esac

7.shell脚本-

math?formula=(())%E3%80%81( )、`` 与 ${ } 的区别

1)、在 bash 中,$( ) 与 反引号``都是用来作命令替换的。

命令替换与变量替换差不多,都是用来重组命令行的,先完成引号里的命令行,然后将其结果替换出来,再重组成新的命令行。

$ echo today is $(date "+%Y-%m-%d")

today is 2014-07-01

2)、$( ) 与``在操作上,这两者都是达到相应的效果

但是建议使用 $( ),理由如下:

反引号(``)很容易与单引号('') 搞混乱,尤其对初学者来说。

在多层次的复合替换中,反引号(``)必须要额外的跳脱处理(反斜线),而 $( ) 比较直观。

最后,$( ) 的弊端是,并不是所有的类 unix 系统都支持这种方式,但反引号是肯定支持的。

# 将cmd1执行结果作为cmd2参数,再将cmd2结果作为cmd3的参数

cmd3 $(cmd2 $(cmd1))

如果是用反引号,直接引用是不行的,还需要作跳脱处理

cmd3 `cmd2 \`cmd1\``

3)、${ } 变量替换

一般情况下,$var 与 ${var} 是没有区别的,但是用 ${ } 会比较精确的界定变量名称的范围

如下:

$ A=B

$ echo ${A}B

BB

还有之前的参数数组

arg=($@)

echo ${arg[0]}

4)、 $(()) 用法

算术运算和三目运算

48f2710d8ccd

8.shell脚本- 数组

#! /bin/bash

os=('aa' 'bb' 'cc' 'dd')

os[4]='ee' # 增加一个元素

#unset os[1] # 删除数组第2个元素

echo "${os[@]}"

echo "${os[1]}"

echo "${!os[@]}"

echo "${#os[@]}"

# 字符串也是数组(全部字符串都在下标为0的元素里)

string=abcdef

echo "${string[0]}" #打印全部元素

echo "${string[1]}" #打印为空

运行 1.sh 显示

ldeepin@ldeepin-PC:~$ bash 1.sh

abcdef

9.shell脚本- while

1)、数值判断循环

#! /bin/bash

n=1

while [ $n -le 10 ]

do

echo "$n"

n=$((n+1))

done

自增也可以这样写法:

#! /bin/bash

n=1

while (( $n <= 10 ))

do

echo "$n"

(( n++ ))

done

2)、循环读取文件内容

准备一个test.txt文本,里面任意添加一些内容,while循环读取每一行内容

#! /bin/bash

while read line

do

echo $line

done < test.txt

.第二种写法:

#! /bin/bash

cat test.txt |while read line

do

echo $line

done

10.shell脚本- until

#! /bin/bash

n=1

sumall=0

until (( $n > 100 ))

do

sumall=$(( sumall+n ))

(( n++ ))

done

echo $sumall

注意:sumall=$(( sumall+n )) 前面的sumall没有$

11.shell脚本- for

1)、100以内的书相加:

#! /bin/bash

sumall=0

for (( i=1;i<=100;i++ )) #类似js的语法

do

sumall=$(( sumall + i ))

done

echo $sumall

2)、逐个打印数组内的元素:

#! /bin/bash

os=('aa' 'bb ''cc' 'dd')

for (( i=0;i<4;i++ ))

do

echo ${os[i]}

done

12.shell脚本- if语句判断文件

-f :判断是否是文件

-d:判断是否是文件夹

-e:判断是否存在

-s:判断文件是否为空

1)、如下判断文件是否存在

#! /bin/bash

read -p "enter your filename:" filename

if [ -e $filename ]

then

echo "$filename exists"

else

echo "$filename isnot exists"

fi

2)、如下判断 文件 是否为空:

#! /bin/bash

read -p "enter your filename:" filename

if [ -s $filename ]

then

echo "$filename is not empty"

else

echo "$filename is empty"

fi

3)、判断 文件夹 是否为空:采取如下办法:

#! /bin/bash

read -p "enter your filename:" dir

file=$(ls $dir) #ls命令获取输出文字

if [ -z $file ]

then

echo "$file is empty"

else

echo "$file is not empty"

fi

4)、详细的文件判断参数

-a file exists.

-b file exists and is a block special file.

-c file exists and is a character special file.

-d file exists and is a directory.

-e file exists (just the same as -a).

-f file exists and is a regular file.

-g file exists and has its setgid(2) bit set.

-G file exists and has the same group ID as this process.

-k file exists and has its sticky bit set.

-L file exists and is a symbolic link.

-n string length is not zero.

-o Named option is set on.

-O file exists and is owned by the user ID of this process.

-p file exists and is a first in, first out (FIFO) special file or

named pipe.

-r file exists and is readable by the current process.

-s file exists and has a size greater than zero.

-S file exists and is a socket.

-t file descriptor number fildes is open and associated with a

terminal device.

-u file exists and has its setuid(2) bit set.

-w file exists and is writable by the current process.

-x file exists and is executable by the current process.

-z string length is zero.

13.shell脚本-扩展阅读awk

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值