bash 基础

整理汇总从网上、书上看到的bash脚本的基本用法。

1. -x和read

set -x 设置显示shell执行时的没一行内容

set +x 禁用 -x功能

read 接收输入

demo01.sh

#!/bin/bash

set -x
message="Hello"
read name
set +x
echo "$message $name"

oliver@bigdatadev:~/_src/shell$ chmod +x demo01.sh
oliver@bigdatadev:~/_src/shell$ ./demo01.sh
+ message=Hello
+ read name
Jason
+ set +x
Hello Jason
oliver@bigdatadev:~/_src/shell$ 

2. 引号的使用方法

在双引号中包含的变量可以用其他值来替换,在单引号中则保持变量名。

demo02.sh

#!/bin/bash
message="Hello"
echo "$message world"
echo "--------------"
echo '$message world'

oliver@bigdatadev:~/_src/shell$ chmod +x demo02.sh
oliver@bigdatadev:~/_src/shell$ ./demo02.sh
Hello world
--------------
$message world
oliver@bigdatadev:~/_src/shell$ 

如果有通配符*,加上引号,不进行路径名扩展;不加引号,进行路径名扩展。

oliver@bigdatadev:~/_src/shell$ touch HelloWorld.txt
oliver@bigdatadev:~/_src/shell$ message="Hello*"
oliver@bigdatadev:~/_src/shell$ echo "$message"
Hello*
oliver@bigdatadev:~/_src/shell$ echo $message
HelloWorld.txt
oliver@bigdatadev:~/_src/shell$ rm HelloWorld.txt
oliver@bigdatadev:~/_src/shell$ echo $message
Hello*
oliver@bigdatadev:~/_src/shell$ 
当需要将变量名和其它字符串连接时,用{}将变量名括起来。

oliver@bigdatadev:~/_src/shell$ message="Hello"
oliver@bigdatadev:~/_src/shell$ echo "${message}World"
HelloWorld
oliver@bigdatadev:~/_src/shell$ 
双引号中使用双引号,使用反斜杠\转义

oliver@bigdatadev:~/_src/shell$ echo "Hello \"World\""
Hello "World"
单引号中不能使用转义,因此单引号中不能使用单引号,但可用特殊方法达到效果。

oliver@bigdatadev:~/_src/shell$ echo Hello \'world\'
Hello 'world'
oliver@bigdatadev:~/_src/shell$ echo 'Hello' \''world'\'
Hello 'world'
oliver@bigdatadev:~/_src/shell$ 
3. 条件判断

bash的条件判断有三种实习方法,test、[ ]和[[ ]]。test命令是根据返回值进行判断,使用test命令,变量名必须使双引号,[ ]是test命令的另一种写法。另外在使用test,当有通配符时,要注意路径名扩展问题。

#!/bin/bash

message="Hello"

if test "$message" = "Hello"; then
    echo "Hello World 1"
fi

if [ "$message" = "Hello" ];  then
    echo "Hello World 2"
fi

if [[ $message == "Hello" ]]; then
    echo "Hello World 3"
fi

字符串比较[[ <条件> ]]

条件说明
字符串1 == 字符串2字符串匹配
字符串1 != 字符串2字符串不匹配
-z 字符串字符串为空
-n 字符串字符串不为空
字符串 == 模式字符串于模式匹配
字符串 != 模式字符串于模式不匹配

数值比较[[ <条件> ]]

条件说明
数值1 -eq 数值2数值相等(equal)
数值1 -ne 数值2数值不相等(not equal)
数值1 -lt 数值2数值1小于数值2(less than)
数值1 -le 数值2数值1小于等于数值2(less or equal)
数值1 -gt 数值2数值1大于数值2(greater than)
数值1 -ge 数值2数值1大于等于数值2(greater or equal)

文件比较[[ <条件> ]]

条件说明
-e 文件名该文件存在
-d 文件名是目录
-h 文件名是符号链接
-f 文件名是常规文件

条件逻辑运算[[ <条件> ]]

条件说明
条件1 && 条件2两者都成立
条件1 || 条件2两者之一成立
! 条件条件不成立
true始终成立
false始终不成立

4. 数组和位置参数

demo04.sh

#!/bin/bash
fruits[0]="Apple"
fruits[1]="Grape"
fruits[2]="Orange"
echo "${fruits[0]} ${fruits[1]} ${fruits[2]} "

fruits=( "Apple" "Grape" "Orange" )
echo "${fruits[0]} ${fruits[1]} ${fruits[2]} "

echo "${#fruits[@]}"

echo "${fruits[@]}"
oliver@bigdatadev:~/_src/shell$ chmod +x demo04.sh
oliver@bigdatadev:~/_src/shell$ ./demo04.sh
Apple Grape Orange 

Apple Grape Orange 
3
Apple Grape Orange
oliver@bigdatadev:~/_src/shell$ 
$@参数列表

#!/bin/bash

echo $0
for item in "$@"; do
    echo $item
done
oliver@bigdatadev:~/_src/shell$ chmod +x demo05.sh
oliver@bigdatadev:~/_src/shell$ ./demo05.sh 1 2 3 4 5 6
./demo05.sh
1
2
3
4
5
6
oliver@bigdatadev:~/_src/shell$ 
shift命令移动参数

#!/bin/bash
while [[ "$@" != "" ]]; do
    echo "$@"
    shift
done
oliver@bigdatadev:~/_src/shell$ chmod +x demo06.sh
oliver@bigdatadev:~/_src/shell$ ./demo06.sh one two three
one two three
two three
three
oliver@bigdatadev:~/_src/shell$ 

5. 命令置换和数值演算

用$()将输出结果的命令括起来,将标准命令的输出结果作为字符串使用。也可以用``。

oliver@bigdatadev:~/_src/shell$ message="sh命令的完整路径是$(which sh)。"
oliver@bigdatadev:~/_src/shell$ echo $message
sh命令的完整路径是/bin/sh。
oliver@bigdatadev:~/_src/shell$ 
在bash中,可以用$(( )) 进行数值演算,语法类似c语言。

运算符

符号说明
+-*/加减乘除
**平方
%余数
<<向左移位
>>向右移位
&按位AND
|按位OR
~按位NOT
^按位XOR

#!/bin/bash

first=$1
step=$2
last=$3

c=$first
while [[ $c -le $last ]]; do
    echo $c
    c=$(( c + step ))
done
oliver@bigdatadev:~/_src/shell$ chmod +x demo07.sh
oliver@bigdatadev:~/_src/shell$ ./demo07.sh 10 2 20
10
12
14
16
18
20
oliver@bigdatadev:~/_src/shell$ 

6. case逻辑判断

#!/bin/bash
read -p "Input a number: " n
a=$[$n%2]  #$[] 数学计算,
case $a in
1)
     echo "The number is odd."
     ;;
0)
     echo "The number is even."
     ;;
*)
     echo "It's not a number!"
     ;;
esac
oliver@bigdatadev:~/_src/shell$ chmod +x demo08.sh
oliver@bigdatadev:~/_src/shell$ ./demo08.sh
Input a number: 15
The number is odd.
oliver@bigdatadev:~/_src/shell$ ./demo08.sh
Input a number: 12
The number is even.
7. for循环

oliver@bigdatadev:~$ for i in `seq 1 5` ; do echo $i; done 
1
2
3
4
5
oliver@bigdatadev:~$ 
oliver@bigdatadev:~/_src/shell$ for file in `ls`; do echo $file; done
demo01.sh
demo02.sh
demo03.sh
demo04.sh
demo05.sh
demo06.sh
demo07.sh
demo08.sh
oliver@bigdatadev:~/_src/shell$ 

类似C语言的方法

for (( i=1; i<=5, i++ )) ; do echo $i; done

8. while循环

oliver@bigdatadev:~/_src/shell$ a=5
oliver@bigdatadev:~/_src/shell$ while [ $a -ge 1 ]; do echo $a; a=$[$a-1]; done
5
4
3
2
1
oliver@bigdatadev:~/_src/shell$ 
9. 函数

oliver@bigdatadev:~/_src/shell$ vi demo09.sh
#!/bin/bash

function sum(){
    sum=$[$1+$2]
    echo $sum
}

sum $1 $2
oliver@bigdatadev:~/_src/shell$ demo09.sh 6 9
15
oliver@bigdatadev:~/_src/shell$ 

















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值