shell脚本语言简明教程

1.第一行的#!

 #! /bin/sh

2.变量

1.定义变量

定义变量时,变量名不加美元符号
变量名的命名须遵循如下规则:

  1. 首个字符必须为字母(a-z,A-Z)。
  2. 中间不能有空格,可以使用下划线(_)。
  3. 不能使用标点符号。
  4. 不能使用bash里的关键字(可用help命令查看保留关键字)。
 var1 = 'aaa'
 var2 = "$var1 b"
 echo $var1

2.字符串变量

1.单引号

单引号字符串的限制:

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

2.双引号

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

[root@localhost sh]# testvalue=100
  [root@localhost sh]# echo 'The testvalue is $testvalue'
  The testvalue is $testvalue
  [root@localhost sh]# echo "The testvalue is $testvalue"
  The testvalue is 100

3.获取字符串长度

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

4.截取字符串

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

5.printf输出

printf format-string [arguments…]

printf "1 2 %s\n" 3

3.访问shell脚本的参数

$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数,当参数超过9,需要用大括号框起来。
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误

echo $1
echo ${11}

4.test命令、 [、(( 和 [[

内置命令 test 根据表达式expr 求值的结果返回 0(真)或 1(假)。
test expr 和 [ expr ] 是等价的。
可以用 $? 检查返回值;可以使用 && 和 || 操作返回值;
注意:操作符前后的空格不能少,否则无法识别。

1.数字:

参数 说明
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真

[ 1 -eq 2 -a 2 -eq 1 ];echo $? 
...
if test 100 -eq 100
then
    echo 'The two numbers are equal!'
fi

2.字符串

参数 说明
= 等于则为真
!= 不相等则为真
-z 空字符串
-n 非空字符串

num1=100
num2=100
if test num1=num2
then
    echo 'The two strings are equal!'
fi

3.文件

参数 说明
-e 文件名 如果文件存在则为真
-r 文件名 如果文件存在且可读则为真
-w 文件名 如果文件存在且可写则为真
-x 文件名 如果文件存在且可执行则为真
-s 文件名 如果文件存在且至少有一个字符则为真
-d 文件名 如果文件存在且为目录则为真
-f 文件名 如果文件存在且为普通文件则为真
-c 文件名 如果文件存在且为字符型特殊文件则为真
-b 文件名 如果文件存在且为块特殊文件则为真

cd /bin
if test -e ./bash
then
    echo 'The file already exists!'
fi

4.与或非运算

与( -a )、或( -o )、非( ! )
其优先级为:”!”最高,”-a”次之,”-o”最低

cd /bin
if test -e ./notFile -o ./bash
then
    echo 'One file exists at least!'
else
    echo 'Both dose not exists!'
fi
...
if test -e 20150609_imei.txt -a 1 = 1; then  echo 'a'; fi 

测试分组

[ ! \( "a" = "$HOME" -o '(' 3 -lt 4 ')' ")" ]; echo $?

(( 和 [[

test 命令很难满足转义需求以及字符串和算术比较之间的区别。
(( ))复合命令 计算算术表达式,如果表达式求值为 0,则设置退出状态为 1;如果求值为非 0 值,则设置为 0。不需要对 (( 和 )) 之间的操作符转义。
复合命令 [[ ]] 可以对文件名和字符串使用更自然的语法,不需要转义。

[ian@pinguino ~]$ (( w=(y/x) + ( (~ ++x) & 0x0f ) )); echo $? $x $y $w
0 4 8 13
[ian@pinguino ~]$ [[ "abc def d,x" == a[abc]*\ ?d* ]]; echo $?
1

5.数组

用括号来表示数组,数组元素用”空格”符号分割开。

array_name=(value0 value1 value2 value3)
#或者直接赋值
array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen

echo ${array_name[n]} #读取数组元素
length=${#array_name[@]} #获取数组长度

6.流程控制

1.if else-if else

if condition
then command1 
    command2
    ...
    commandN
elif condition2
then command1 
    command2
    ...
else command
fi

2.for循环

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
    break
    continue
done
...
for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done

3.while语句

while condition
do
    command
done

4.case

case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac

7.函数

1.定义函数

#!/bin/bash
demoFun(){
    echo "This is your first shell function!"
    return 1
}
echo "Function begin..."
demoFun
echo "Function end!

2.函数参数

#!/bin/bash
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 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

8.bash在线编译工具

http://tool.runoob.com/index.php/Home/Index/compile/language/bash

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值