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

ead -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 语句与比较运算符

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

参考Linux 之 shell 中的大括号、中括号、小括号的使用详解 + 多示例

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

整数比较符

-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脚本- (())、( )、`` 与 ${ } 的区别

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

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

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

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

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

反引号(``)很容易与单引号('') 搞混乱,尤其对初学者来说。
在多层次的复合替换中,反引号(``)必须要额外的跳脱处理(反斜线),而 $( ) 比较直观。
最后,$( ) 的弊端是,并不是所有的类 unix 系统都支持这种方式,但反引号是肯定支持的。

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

3)、${ } 变量替换

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

$ A=B
$ echo ${A}B
BB
还有之前的参数数组

arg=($@)
echo ${arg[0]}

4)、 $(()) 用法

算术运算和三目运算

#${ } 变量替换
a=c
echo ${a}b
#$(()) 算术运算与三元运算

echo $((3+2))
echo $((3*6))

echo $((24<3 ? 666 : 555))

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

Shell中的字符串

单引号的限制:

  1. 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
  2. 单引号字串中不能出现单引号(对单引号使用转义符后也不行)。

双引号的优点:

  1. 双引号里可以有变量
  2. 双引号里可以出现转义字符

拼接字符串:

country="China"
echo "hello, $country"
#也可以
echo "hello, "$country" "

获取字符串长度:

string="abcd"
echo ${#string} #输出 4

提取子字符串:

string="alibaba is a great company"
echo ${string:1:4} #输出liba

查找子字符串:

string="alibaba is a great company"
echo `expr index "$string" is`

处理路经的字符串:

例如:当一个路径为 /home/xiaoming/1.txt时,如何怎么它的路径(不带文件) 和如何得到它的文件名??

得到文件名使用 bashname命令:  

复制代码
#  参数:
#  -a,表示处理多个路径;
# -s, 用于去掉指定的文件的后缀名;

 basename /home/yin/1.txt          -> 1.txt

 basename -a /home/yin/1.txt /home/zhai/2.sh     -> 
1.txt
2.sh basename -s .txt /home/yin/1.txt    -> 1
 basename /home/yin/1.txt .txt       -> 1
复制代码

得到路径名(不带文件名)使用 dirname命令:

复制代码
参数:没有啥参数;

//例子:
 dirname /usr/bin/          -> /usr
 dirname dir1/str dir2/str  -> 
dir1
dir2
 dirname stdio.h            -> .
复制代码
 
 

Shell的数组:

bash支持一维数组, 不支持多维数组, 它的下标从0开始编号. 用下标[n] 获取数组元素;

定义数组:

在shell中用括号表示数组,元素用空格分开。 如:

array_name=(value0 value1 value2 value3)

也可以单独定义数组的各个分量,可以不使用连续的下标,而且下标的范围没有限制。如:

array_name[0]=value0
array_name[1]=value1
array_name[2]=value2

读取数组:

读取某个下标的元素一般格式为:

${array_name[index]}

读取数组的全部元素,用@或*

${array_name[*]}
${array_name[@]}

获取数组的信息:

取得数组元素的个数:

length=${#array_name[@]}
#或
length=${#array_name[*]}

获取数组的下标:

length=${!array_name[@]}
#或
length=${!array_name[*]}

取得数组单个元素的长度:

lengthn=${#array_name[n]}
 
 
 

printf函数:

它与c语言中的printf相似,不过也有不同,下面列出它的不同的地方:

  1. printf 命令不用加括号
  2. format-string 可以没有引号,但最好加上,单引号双引号均可。
  3. 参数多于格式控制符(%)时,format-string 可以重用,可以将所有参数都转换。
  4. arguments 使用空格分隔,不用逗号。

下面为例子:

复制代码
# format-string为双引号
$ printf "%d %s
" 1 "abc"
1 abc
# 单引号与双引号效果一样 
$ printf '%d %s
' 1 "abc" 
1 abc
# 没有引号也可以输出
$ printf %s abcdef
abcdef
# 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用
$ printf %s abc def
abcdef
$ printf "%s
" abc def
abc
def
$ printf "%s %s %s
" a b c d e f g h i j
a b c
d e f
g h i
j
# 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替
$ printf "%s and %d 
" 
and 0
# 如果以 %d 的格式来显示字符串,那么会有警告,提示无效的数字,此时默认置为 0
$ printf "The first program always prints'%s,%d
'" Hello Shell
-bash: printf: Shell: invalid number
The first program always prints 'Hello,0'
$
复制代码

Shell中条件语句

if 语句

包括:1, if [ 表达式 ] then  语句  fi

2. if [ 表达式 ] then 语句 else 语句 fi

3.  if [ 表达式] then 语句  elif[ 表达式 ] then 语句 elif[ 表达式 ] then 语句   …… fi

例子:

复制代码
a=10
b=20
if [ $a == $b ]
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi
复制代码

另外:if ... else 语句也可以写成一行,以命令的方式来运行,像这样:

if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;

其中,test 命令用于检查某个条件是否成立,与方括号([ ])类似。

case …… esac语句

case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构。case语句格式如下:

复制代码
case 值 in
模式1)
    command1
    command2
    command3
    ;;
模式2)
    command1
    command2
    command3
    ;;
*)
    command1
    command2
    command3
    ;;
esac
复制代码

其中, 1. 取值后面必须为关键字 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。2. 如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

Shell 的循环语句

for 循环 

  一般格式为:

复制代码
for 变量 in 列表
do
    command1
    command2
    ...
    commandN
done
复制代码

注意:列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。       例如:

顺序输出当前列表的数字:

for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done

显示主目录下以 .bash 开头的文件:

#!/bin/bash
for FILE in $HOME/.bash*
do
   echo $FILE
done

while循环

一般格式为:

while command
do
   Statement(s) to be executed if command is true
done

例如:

COUNTER=0
while [ $COUNTER -lt 5 ]
do
    COUNTER='expr $COUNTER+1'
    echo $COUNTER
done

until 循环

until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。    常用格式为:

until command
do
   Statement(s) to be executed until command is true
done

command 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环。

类似地, 在循环中使用 break 与continue 跳出循环。    另外,break 命令后面还可以跟一个整数,表示跳出第几层循环。

Shell函数

Shell函数必须先定义后使用,定义如下,

function_name () {
    list of commands
    [ return value ]
}

也可以加上function关键字:

function function_name () {
    list of commands
    [ return value ]
}

注意:1. 调用函数只需要给出函数名,不需要加括号。

2. 函数返回值,可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。

3. Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。

4. 函数的参数可以通过 $n  得到.如:

复制代码
funWithParam(){
    echo "The value of the first parameter is $1 !"
    echo "The value of the second parameter is $2 !"
    echo "The value of the tenth parameter is ${10} !"
    echo "The value of the eleventh parameter is ${11} !"
    echo "The amount of the parameters is $# !"  # 参数个数
    echo "The string of the parameters is $* !"  # 传递给函数的所有参数
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
复制代码
5. 像删除变量一样,删除函数也可以使用 unset 命令,不过要加上 .f 选项,如下所示:
unset .f function_name

shell的文件包含:

Shell 也可以包含外部脚本,将外部脚本的内容合并到当前脚本。使用:

. filename
#或
source filename

1. 两种方式的效果相同,简单起见,一般使用点号(.),但是注意点号(.)和文件名中间有一空格。

2. 被包含脚本不需要有执行权限。

 
 
 
 
 
 
 

作者:Jason____
链接:https://www.jianshu.com/p/48f2710d8ccd
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 
 
 
原文地址:https://www.cnblogs.com/yszr/p/14266768.html