随笔分类 -  Shell

shell 判断二进制是否可用
摘要:# 结果是yes checkCmd='docker --version' res=`eval $checkCmd 1>/dev/null 2>&1; echo -n $?` resStr=`echo -n $res` if [[ "$resStr" == "0" ]]; then echo "yes 阅读全文

posted @ 2025-12-14 16:38 王景迁 阅读(3) 评论(0) 推荐(0)

shell 容器执行脚本有异常时直接退出
摘要:# pod.yaml # set -e:当脚本中有命令执行返回非0时,脚本会立即终止执行并异常退出。 # set -u:当脚本中引用了未定义的变量时,脚本会立即终止执行并异常退出。 ... containers: - name: busybox image: docker.io/library/bu 阅读全文

posted @ 2025-12-14 15:49 王景迁 阅读(4) 评论(0) 推荐(0)

shell sort排序
摘要:# sort默认按字符排序,使用数值排序 sort -n # 按照指定列排序 sort -k [第几列|1] -t [间隔符|_] # 去除重复行 sort -u # sort默认升序,使用降序排列 sort -r 阅读全文

posted @ 2025-12-09 08:57 王景迁 阅读(4) 评论(0) 推荐(0)

shell eval作用
摘要:eval可以把字符串当成shell命令执行。 阅读全文

posted @ 2025-12-09 08:19 王景迁 阅读(5) 评论(0) 推荐(0)

shell脚本内使用alias
摘要:shopt -s expand_aliases放在所有alias定义前面。 root@root1:~/sh# cat test.sh #!/bin/bash shopt -s expand_aliases alias k="kubectl -n kube-system " k get pod 阅读全文

posted @ 2025-12-07 14:43 王景迁 阅读(3) 评论(0) 推荐(0)

Shell 循环
摘要:ids=(100 101 102) 使用索引遍历数组 for ((i=0; i<${#ids[@]}; i++)) do echo "$i:${ids[$i]}" done 直接遍历数组元素 for item in "${ids[@]}" do echo "$item" done 阅读全文

posted @ 2025-05-07 21:00 王景迁 阅读(12) 评论(0) 推荐(0)

Shell 按列取字符串并合并
摘要:cat a | awk '{print $2}' | tr "\n" " " | sed 's/^ *//;s/ *$//' 取第2列->去掉换行符合并->去掉首尾空格 阅读全文

posted @ 2025-01-19 11:20 王景迁 阅读(18) 评论(0) 推荐(0)

获取和解析shell函数返回值
摘要:根据调用函数的字符串参数,判断字符串是否为空,来返回结果。 function f1() { if [ -z $1 ] then return 1 else return 0 fi } f1 "" echo $? f1 "a" echo $? shell函数返回值只能是整数,0表示成功,其他值表示失败 阅读全文

posted @ 2024-08-20 08:46 王景迁 阅读(41) 评论(0) 推荐(0)

Shell 处理时间
摘要:场景1:打印时间 now_time=`date +"%Y-%m-%d %H:%M:%S"` echo $now_time # 使用双引号可以解析变量,单引号不行 echo 'time is $now_time' echo "time is $now_time" x=10 echo 'x is $x' 阅读全文

posted @ 2024-07-26 07:53 王景迁 阅读(65) 评论(0) 推荐(0)

监听ctrl+c和15信号
摘要:function quit() { time=$(date "+%Y-%m-%d %H:%M:%S") echo "$time quit" exit 0 } # 捕捉ctrl+c trap quit SIGINT # 捕捉15 trap quit SIGTERM while true do slee 阅读全文

posted @ 2024-06-29 17:44 王景迁 阅读(22) 评论(0) 推荐(0)

expect scp传输文件
摘要:set user "root" set password "root" set host "192.168.0.112" set timeout 10 spawn scp a $user@$host:/root expect { "*(yes/no)?" { send "yes\r" exp_con 阅读全文

posted @ 2024-04-02 22:43 王景迁 阅读(35) 评论(0) 推荐(0)

expect远程登录节点执行命令
摘要:set user "root" set password "root" set host "192.168.0.112" set timeout 10 spawn ssh $user@$host expect { "*(yes/no)?" { send "yes\r" exp_continue } 阅读全文

posted @ 2024-04-02 22:07 王景迁 阅读(46) 评论(0) 推荐(0)

Linux脚本内开异步进程和终端开异步进程区别
摘要:sleep.sh内容 sleep 1000 & echo $! 脚本内开异步进程是1号进程的子进程 终端开异步进程是当前bash进程的子进程 在关闭终端后该异步进程会停止 停止终端时想要继续执行,使用nohup sleep 1000 &,刚开始是终端bash进程的子进程,在终端停止后会变成1号进程的 阅读全文

posted @ 2023-10-13 08:51 王景迁 阅读(24) 评论(0) 推荐(0)

消耗完CPU
摘要:使用死循环来消耗完若干个CPU 脚本内容 #! /bin/sh # 参数个数不是1 if [ $# != 1 ] ; then echo "USAGE: $0 [CPU count]" exit 1 fi cpu_count=$1 for ((i=1; i<=cpu_count; i++)) do 阅读全文

posted @ 2023-03-11 09:39 王景迁 阅读(18) 评论(0) 推荐(0)

Shell总结
摘要:grep -i 不区分大小写 -c 统计包含匹配的行数 -n 输出行号 -v 反向匹配 -m 限制结果数量 & 后台执行,关闭会话终端后不再运行 nohup 表示关闭会话终端后继续运行 nohup ./test1 > /dev/null & date date "+%Y-%m-%d %H:%M:%S 阅读全文

posted @ 2023-03-11 08:59 王景迁 阅读(37) 评论(0) 推荐(0)

导航