惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
GbyAI
GbyAI
博客园 - Franky
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
InfoQ
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
博客园 - 【当耐特】
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Project Zero
Project Zero
量子位
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
美团技术团队
Attack and Defense Labs
Attack and Defense Labs
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
S
SegmentFault 最新的问题
L
LINUX DO - 最新话题
Simon Willison's Weblog
Simon Willison's Weblog
爱范儿
爱范儿
博客园 - 聂微东
A
About on SuperTechFans
PCI Perspectives
PCI Perspectives
D
Docker

Yulin Lewis' Blog

cron表达式 cmd - 常用命令 Eureka问题汇总 Excel常见用法 Linux常用命令 别了,向晚 必胜客自助餐 Prometheus简易入门 Chrome问题汇总 SSH命令问题汇总 PGP加解密 Apollo问题汇总 GitHub问题汇总 SmartGit问题汇总 HttpClient问题汇总 Oculus Quest2食用指南 背包英雄:匕首流无尽模式详细攻略 Linux问题汇总 Postman问题汇总 别了,珈乐 桌面窗口管理器占用内存过高 ELK系列(6) - Elasticsearch常用接口 ELK系列(5) - Elasticsearch性能调优 MyBatis问题汇总 PostgreSQL - SQL调优方案 Java - 字符编码 Spring Data Redis问题汇总 Java数值问题汇总 lombok问题汇总
shell脚本常用语法
雨临Lewis · 2024-11-25 · via Yulin Lewis' Blog

常用语法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 脚本的第一行申明是bash脚本
#!/bin/bash

# 获取进程id
pid=$(ps -ef|grep -v grep|grep java|awk '{print $2}')

# echo,输出文本到控制台,-n表示输出末尾不换行
echo "flag is true"
echo -n "flag is true"

# 多行条件判断,-z表示值为空,-n表示值不为空
# [[]]是[]的扩展语法,支持一些更高级的功能,但需要在bash环境中(`#!/bin/bash`)使用
if [[ -n "$sys" ]]; then
    sysType="x86";
    sys=`uname -a |grep aarch64`
elif [[ -n "$sys" ]]; then
    sysType="aarch64";
    sys=`uname -a |grep AIX`
elif [[ -n "$sys" ]]; then
    sysType="aix";
fi

# 单行条件判断,&&逻辑与,后面可以接另一个命令
[[ $flag == true ]] && echo -n "flag is true"

# 循环
time=5
for ((i=1;i<=$time;i++)); do
    [[ $isEcho == true ]] && echo -n "$i秒"
    sleep 1
done

# 读取用户输入
while true
do
    read -p "请输入y/n,[y继续将忽略/n退出]:" inpt
    case $inpt in
        y)
            exit 0
        ;;
        n)
            exit 1
        ;;
        *)
            echo -n "输入错误"
        ;;
    esac
done

# 获取调用当前脚本时的输入参数,按顺序$1、$2、$3……
# $0:脚本本身的名字
# $@:传给脚本的所有参数的列表
# $$:脚本运行的当前进程ID号
# $?:显示最后命令的退出状态,0表示没有错误,其他表示有错误
deployEnv=$1;
systemId=$2;
terminalId=$3;

sh test.sh
[[ $? != 0 ]] && exit 1

# 字符串大小写转换
deployEnv="test";
# 首字母变为大写
deployEnv=${deployEnv^}
# 全部变为大写
deployEnv=${deployEnv^^}
# 首字母变为小写
deployEnv=${deployEnv,}
# 全部变为小写
deployEnv=${deployEnv,,}

# tr命令转换大小写
# 小写转大写
echo test|tr '[a-z]' '[A-Z]'
# 大写转小写
echo test|tr '[A-Z]' '[a-z]'

# awk配合toupper()或tolower()进行大小写转换
echo test|awk '{print toupper($0)}'
echo TEST|awk '{print tolower($0)}'

# 函数定义
APP_DIR=/test
APP=test.jar
checkpid() {
    javaps=`jps -l | grep $APP_DIR/$APP`
    if [ -n "$javaps" ]; then
        psid=`echo $javaps | awk '{print $1}'`
    else
        psid=0
    fi
}
# 函数调用
checkpid

参考链接

注意

本文最后更新于 March 2, 2025,文中内容可能已过时,请谨慎使用。

赞赏支持

微信打赏 支付宝打赏