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

设置环境变量的方法:

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

  1. export test=12312

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

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

查看环境变量:

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

unset消除环境变量

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

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

  1. a=123
  2. b=123$a
  3. echo $b

输出以下:

  1. 123123

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

输出为:

  1. 123$a

a=”123″ 变量解析–一般用此种方法定义变量

  1. a=123
  2. b="123$a"
  3. echo $b
  1. a=$(ls)

name=”testName”

方法一:

  1. echo ${#name}

输出结果:

  1. 8

方法二:

  1. echo ${name}|wc -L

方法三:

  1. expr length "${name}"

首先定义字符串:

  1. name=testNametestName

截取字符串:

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

输出结果:

  1. stNametestName
  1. echo ${name#t*N} 从头开始最短匹配

输出为:
ametestName

最长匹配:

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

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

  1. echo ${name%N*e}

输出为:

  1. testNametest

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

  1. echo ${name/test/test1} 从头匹配替换第一个
  2. echo ${name//test/test1} 从头匹配替换所有
  1. test-all-1.txt test-all-2.txt test-all-3.txt test-all-4.txt test-all.txt

脚本为:

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

方法一:

  1. echo $((2+2))
  2. 4

方法二:

  1. let a=12+12
  2. echo ${a}
  3. 24

方法三:

  1. expr 12 % 3
  2. 0
  3. echo "123 122" |awk '{print ($1-$2)}'
  4. $[] echo $[12+12] a=$[12+12+12]

方法一:

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

方法二:

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

方法三:

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

字符串测试:

  1. [ -z "" ] && echo false -z 字符串长度为0的时候为真
  2. [ -n "test" ] && echo true || echo false -n 字符串长度不为0的时候为真
  3. [ "test" == "test" ] && echo true 字符串是否相等 同= !=
  4. 等号和中括号两端需要有空格
  5. 整数的比较:
  6. [ 2 -eq 3 ] && echo true || echo false
  7. 逻辑操作符:
  8. -a -o 与或非
  9. [ 2 -eq 2 -a 3 -eq 3 ] && echo true || echo false
  1. 单分支结构:
  2. if<条件表达式>
  3. then
  4. 指令
  5. fi
  6. if <条件表达式>; then
  7. 指令
  8. fi

多分支:

  1. if<条件表达式>
  2. then
  3. else
  4. fi

多分支:

  1. if<条件表达式>
  2. then
  3. elif<条件表达式>
  4. then
  5. else
  6. fi

functiontest.sh脚本内容:

  1. function testFun(){
  2. echo "function test! hello $1";
  3. }
  4. testFun $1
  5. sh functiontest.sh testname

while和until循环

  1. while<条件表达式>
  2. do
  3. 命令
  4. done
  5. until<表达式>
  6. do
  7. done

脚本后台运行:

  1. sh functiontest.sh&
  2. control + c 停止
  3. control + z 暂停

for 语句

  1. for((i=1;i<5;i++))
  2. do
  3. echo $i
  4. done

select 语句

  1. select name in ywp hf csq
  2. do
  3. echo $name
  4. done

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

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

方法二:

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

for循环打印数组内容

  1. array=(test1 test2 test3)
  2. for name in ${array[*]}
  3. do
  4. echo ${name}
  5. done

动态数组:

  1. array=($(ls))
  2. echo ${array[*]}
  3. echo ${#array[*]} 打印数组长度
  1. 1.全局变量 全部大写
  2. 2.局部变量 驼峰
  3. 3.变量引用 ${}
  4. 4.字符串变量引用 "${}"
  5. 5.统一使用.sh命名
  6. 6.启动和停止统一使用startstop开头
  7. 7.通用变量放在config目录下
  8. 8.中括号两边添加空格

shell脚本调试:

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

vim 配置:

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

版权声明:本文为vitasyuan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/vitasyuan/p/9650630.html