03 2022 档案
摘要:1. let命令 n1=1 n2=2 # 求和运算 let result=n1+n2 # 自增运算 let n1++ # 自减运算 let n2-- # 自身加5 let n1+=5 等号后面的运算表达式不能有空格 2. 使用[] n1=1 n2=2 sum=$[ $n1 + $n2 ] echo
阅读全文
摘要:1. 获取终端信息 # 获取终端的列数 tput cols # 获取终端的行数 tput lines # 获取终端名称 tput longname 2. 移动光标 # 移动光标到(10,80)处 tput cup 10 80 3. 设置终端 # 设置背景色 tputsetb n # n为0-7的数字
阅读全文
摘要:1. 使用选项-x bash -x script.sh 2. 使用set -x 进行部分调试 array=(1 2 3 4) for value in ${array[@]} do set -x # 开启调试 echo $value set +x # 关闭调试 done set -x # 在执行时显
阅读全文
摘要:1. 处理简单选项 ./test.sh -a -b -c while [ -n "$1" ] do case "$1" in -a) echo "have -a option" ;; -b) echo "have -b option" ;; -c) echo "have -c option" ;;
阅读全文
摘要:1. 读取参数 $1 获取第1个参数 $2 获取第2个参数 ${10} 获取第10个参数 ./test.sh 2 3 # test.sh 脚本内容 var1=$1 # 2 var2=$2 # 3 var3=$[ $var1 * $var2 ] echo $var3 # 6 获取超过第9个参数时要用花
阅读全文
摘要:1. for in 语句 基本格式 for var in list do commands done 代码示例 list="test1 test2 test3" for value in $list do echo $value done 设置列表的分隔符 # 保存原始的分隔符设置 ifs=$IFS
阅读全文
摘要:1. 定义数组 array=(1 2 3 4) 2. 读取数组的值 array=(1 2 3 4) # 读取数组第一个元素 echo ${array[0]} # 1 # 读取素组的所有元素 echo ${array[*]} # 1 2 3 4 读取数组所有元素也可以用${array[@]}代替 3.
阅读全文
摘要:1. sed命令格式 sed options script file cat test.txt # 输出test.txt内容 this is test 1 this is test 2 this is test 3 sed 's/test/Test/' test.txt # 将test.txt中所有
阅读全文
摘要:1.字符串操作符实现 str="" str1="default" str2=${str:-$str1} echo $str2 # default 2. 条件测试实现 str="" str1="default" str2=$([ -z "$str" ] && echo "$str1" || echo
阅读全文
摘要:1. 获取字符串长度 str="test" echo ${#str} 2. 获取匹配的子串的长度 str="substring" echo $(expr match "$str" "*.b") 3. 获取子串的索引 str="substring" echo $(expr index "$str" "
阅读全文
摘要:1.声明字典 declare -A dic 2. 初始化字典 dic=( ["key1"]="value1" ["key2"]="value2" ["key3"]="value3" ) 3. 获取字典的值 value=${dic["key3"]} // 获取某个键的值 values=${dic[*]
阅读全文
摘要:1. 在服务器端启用ssh服务 sudo /etc/init.d/ssh start 在服务端启用ssh服务 sudo update-rc.d ssh enable 将ssh服务设为开机自启动 2. 配置服务器ip为静态ip sudo vim /etc/dhcpcd.conf interface w
阅读全文