vitasyuan

1.linux中的变量

linux中的变量分为环境变量和普通变量,其中环境变量可以理解为全局变量,在所有shell的子程序中都可以引用,普通变量只能在自己的shell程序中使用,程序结束后变量无法保留。

设置环境变量的方法:

1.export命令 用户退出后失效

export test=12312

2..bash_profile、.bashrc、etc/bashrc或者etc/profile中定义

用户的环境变量:
    ls /root/.bashrc(推荐文件)
    ls /root/.bash_profile
全局变量配置
    /etc/profile
    etc/bashrc(推荐)
    
所有环境变量应该为大写
    

查看环境变量:

env查看环境变量列表
set 输出所有变量(包括环境变量和普通变量)

unset消除环境变量

unset $PATH
2.变量定义
本地变量定义三种方法:

a=123+ $a 变量会被解析

a=123
b=123$a
echo $b

输出以下:

123123

a='123 + $a' 变量不会被解析,单引号中的字符串原样输出
a=123
b='123$a'
echo $b

输出为:

123$a

a="123" 变量解析--一般用此种方法定义变量

a=123
b="123$a"
echo $b
命令结果作为变量的值(常见用法):
a=$(ls)

3.对于字符串的操作

输出字符串长度:

name="testName"

方法一:

echo ${#name}

输出结果:

8

方法二:

echo ${name}|wc -L

方法三:

expr length "${name}"
截取字符串内容:

首先定义字符串:

name=testNametestName

截取字符串:

echo ${name:2} 从第二位开始截取

输出结果:

stNametestName
字符串匹配
echo ${name#t*N} 从头开始最短匹配

输出为:
ametestName

最长匹配:

echo ${name##t*N} 从头开始最长匹配

输出为:
ame
从尾开始匹配:

echo  ${name%N*e}

输出为:

testNametest

最长匹配:
echo ${name%%N*e}
test

字符串的替换:
echo ${name/test/test1} 从头匹配替换第一个

echo ${name//test/test1} 从头匹配替换所有
将以下文件名中的all去掉
test-all-1.txt  test-all-2.txt  test-all-3.txt  test-all-4.txt  test-all.txt

脚本为:

 for f in $(ls *.txt)
    do
       mv ${f} ${f//all/}
    done

4.算术运算

方法一:

echo $((2+2))
4

方法二:

let a=12+12
echo ${a}
24

方法三:

expr 12 % 3
0

echo "123 122" |awk '{print ($1-$2)}'
$[]  echo $[12+12]  a=$[12+12+12]

5.条件判断语句:

方法一:

test
test -f test4.txt && echo true || echo false

方法二:

[]
[ -f test4.txt ] && echo true || echo false 文件test4.txt存在输出true,不存在输出false

方法三:

[[]] 括号前后加空格
[[ -f test4.txt ]] && echo true || echo false

字符串测试:

[ -z "" ] && echo false  -z 字符串长度为0的时候为真
[ -n "test" ] && echo true || echo false -n 字符串长度不为0的时候为真

[ "test" == "test" ] && echo true 字符串是否相等 同= !=

等号和中括号两端需要有空格

整数的比较:
[ 2 -eq 3 ] && echo true || echo false

逻辑操作符:
-a -o ! 与或非
[ 2 -eq 2 -a 3 -eq 3 ] && echo true || echo false

6.if条件语句

单分支结构:
if<条件表达式>
then
    指令
fi

if <条件表达式>; then
指令
fi

多分支:

if<条件表达式>
    then
else

fi

多分支:

if<条件表达式>
    then
elif<条件表达式>
    then
else
fi

7.shell函数

functiontest.sh脚本内容:

function testFun(){
        echo "function test! hello $1";
}

testFun $1

sh functiontest.sh testname

while和until循环

while<条件表达式>
do
    命令
done

until<表达式>
do
done

脚本后台运行:

sh functiontest.sh&
control + c 停止
control + z 暂停

for 语句

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

select 语句

select name in ywp hf csq
do
    echo $name
done

break n 跳出整个循环
continue n 跳出本次循环

数组:
array=(ywp hf jc yc)
echo ${array[1]}

方法二:

array=([1]=one [2]=two [3]=threee )
echo ${array[1]}
echo ${array[*]} *打印整个数组的内容

for循环打印数组内容

 array=(test1 test2 test3)
 for name in ${array[*]}
 do
    echo ${name}
done

动态数组:

array=($(ls))
echo ${array[*]}
echo ${#array[*]} 打印数组长度

9.shell脚本开发规范

1.全局变量 全部大写
2.局部变量 驼峰
3.变量引用 ${}
4.字符串变量引用 "${}"
5.统一使用.sh命名
6.启动和停止统一使用start和stop开头
7.通用变量放在config目录下
8.中括号两边添加空格

shell脚本调试:

sh [-nvx] test.sh
-n 不执行,仅检查语法问题
-x将执行的脚本输出到屏幕上

vim 配置:

echo 'alias vi=vim' >>/Users/xxx/.bash_profile
source /Users/xxx/.bash_profile

相关文章: