随笔分类 -  shell 编程

关于shell 编程的技巧
摘要:请求配置 host="http://localhost:3000" baseUrl="/api/v1/test" filePath="./data/data.json" 1. 发送get请求 默认发送get请求 url="${host}${baseUrl}" curl $url 显式声明get请求 阅读全文
posted @ 2024-04-26 21:49 箫笛 阅读(396) 评论(0) 推荐(0) 编辑
摘要: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 阅读全文
posted @ 2022-03-31 19:35 箫笛 阅读(45) 评论(0) 推荐(0) 编辑
摘要:1. 获取终端信息 # 获取终端的列数 tput cols # 获取终端的行数 tput lines # 获取终端名称 tput longname 2. 移动光标 # 移动光标到(10,80)处 tput cup 10 80 3. 设置终端 # 设置背景色 tputsetb n # n为0-7的数字 阅读全文
posted @ 2022-03-31 16:21 箫笛 阅读(80) 评论(0) 推荐(0) 编辑
摘要: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 # 在执行时显 阅读全文
posted @ 2022-03-29 18:22 箫笛 阅读(62) 评论(0) 推荐(0) 编辑
摘要: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" ;; 阅读全文
posted @ 2022-03-24 18:38 箫笛 阅读(182) 评论(0) 推荐(0) 编辑
摘要: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个参数时要用花 阅读全文
posted @ 2022-03-24 01:30 箫笛 阅读(223) 评论(0) 推荐(0) 编辑
摘要:1. for in 语句 基本格式 for var in list do commands done 代码示例 list="test1 test2 test3" for value in $list do echo $value done 设置列表的分隔符 # 保存原始的分隔符设置 ifs=$IFS 阅读全文
posted @ 2022-03-23 18:34 箫笛 阅读(84) 评论(0) 推荐(0) 编辑
摘要:1. 定义数组 array=(1 2 3 4) 2. 读取数组的值 array=(1 2 3 4) # 读取数组第一个元素 echo ${array[0]} # 1 # 读取素组的所有元素 echo ${array[*]} # 1 2 3 4 读取数组所有元素也可以用${array[@]}代替 3. 阅读全文
posted @ 2022-03-23 14:57 箫笛 阅读(63) 评论(0) 推荐(0) 编辑
摘要: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中所有 阅读全文
posted @ 2022-03-21 13:04 箫笛 阅读(127) 评论(0) 推荐(0) 编辑
摘要:1.字符串操作符实现 str="" str1="default" str2=${str:-$str1} echo $str2 # default 2. 条件测试实现 str="" str1="default" str2=$([ -z "$str" ] && echo "$str1" || echo 阅读全文
posted @ 2022-03-20 13:56 箫笛 阅读(488) 评论(0) 推荐(0) 编辑
摘要:1. 获取字符串长度 str="test" echo ${#str} 2. 获取匹配的子串的长度 str="substring" echo $(expr match "$str" "*.b") 3. 获取子串的索引 str="substring" echo $(expr index "$str" " 阅读全文
posted @ 2022-03-20 12:11 箫笛 阅读(63) 评论(0) 推荐(0) 编辑
摘要:1.声明字典 declare -A dic 2. 初始化字典 dic=( ["key1"]="value1" ["key2"]="value2" ["key3"]="value3" ) 3. 获取字典的值 value=${dic["key3"]} // 获取某个键的值 values=${dic[*] 阅读全文
posted @ 2022-03-20 09:57 箫笛 阅读(2885) 评论(0) 推荐(0) 编辑
摘要:1. 条件测试格式 简单条件测试 if [ condition ] then commands fi 注意condition条件表达式在前后方括号之后和之前各有一个空格,否则会出现语法错误 逻辑与条件测试 if [ condition1 ] && [ condition2 ] then comman 阅读全文
posted @ 2022-02-14 16:24 箫笛 阅读(70) 评论(0) 推荐(0) 编辑
摘要:1. if-then 语句 if command then commands fi 2. if-then-else 语句 if command then commands else commands fi 3. if-then-elif-then-else 语句 if command1 then c 阅读全文
posted @ 2022-02-14 15:38 箫笛 阅读(34) 评论(0) 推荐(0) 编辑
摘要:1. 函数默认退出状态码 function isDirExist { local dir=$1 ls $dir } dir=/path/test/ isDirExist "$dir" if [ $? -eq 1 ] then echo "The $dir is not exist" exit fi 阅读全文
posted @ 2022-02-12 14:18 箫笛 阅读(47) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示