variable definition

本文介绍了一个具体的Shell脚本实例,用于记录订单存储使用率,并详细解释了如何在Shell脚本中正确地定义字符串和整数变量。通过实际案例展示了变量赋值时的常见误区。

A sample shell script

 

#!/bin/sh

viperooe_home=`dirname $0`

source $viperooe_home/env-viperooe.sh

instancename=$1

cd $viperooe_home/persistence/$1

currentDate=`date '+%Y%m%d'`

echo $currentDate

currentTime=`date '+%H%M%S'`

echo $currentTime

logName=orderStoreRatio-$currentDate-$currentTime.log

echo here is it! $viperooe_home/$logName

echo "orderstore usage info:" >> $viperooe_home/$logName

typeset -i used=`od -c orderstore |grep 'c   i   b   c' | head -1 | awk '{print $1}'`

echo "Currently used: $used" >> $viperooe_home/$logName

typeset -i total=`od -c orderstore |tail -1`

echo "Totally we have: $total" >> $viperooe_home/$logName

typeset -i ratio=used*100/total

echo "orderstore Usage ratio: $ratio%" >> $viperooe_home/$logName

 

 

 

how to define a string

 

Simply follow this way:

 

currentDate=`date '+%Y%m%d'`

echo $currentDate

 

Don’t use set like this:

 

set currentDate=`date '+%Y%m%d'`

echo $currentDate

 

Conclusion: It’s quite strange that, use keyword set, it doesn’t work. But if you call the above 2 lines directly in command line($) it works fine.

 

How to define a integer

typeset -i used=`od -c orderstore |grep 'c   i   b   c' | head -1 | awk '{print $1}'`

 

 

 

 

 

@cppcheck.checker def check_mandatory_comments(cfg, data): """ 检查以下代码元素是否必须有注释: - 宏定义 (#define) - 全局变量 - 静态变量 - 结构体/联合体/枚举定义 - 函数定义 注释位置要求:元素上方或同一行行尾 """ checked_vars = set() # 已检查的变量集合 # 跟踪当前作用域(用于识别全局变量) scope_stack = [{"is_global": True, "in_function": False}] # 第一遍:收集所有注释位置信息 comment_positions = {} # key: (linenr, colnr), value: 注释内容 for token in data.rawTokens: # 收集注释位置信息 if token.str.startswith(('//', '/*')): comment_positions[(token.linenr, token.column)] = token.str # 第二遍:检查需要注释的元素 tokens = list(data.rawTokens) for i, token in enumerate(tokens): # 1. 检查宏定义 if token.str == '#' and token.next and token.next.str == 'define': define_token = token.next.next check_element_comment( element_token=define_token, element_type="macro definition", comment_positions=comment_positions, tokens=tokens ) # 2. 检查结构体/联合体/枚举定义 if token.str in ['struct', 'union', 'enum']: # 获取类型名称(跳过匿名定义) name_token = token.next if name_token and name_token.str != '{': # 跳过匿名定义 check_element_comment( element_token=name_token, element_type=f"{token.str} definition", comment_positions=comment_positions, tokens=tokens ) # 3. 更新作用域状态 if token.str == '{': # 进入新作用域(默认为局部) scope_stack.append({ "is_global": False, "in_function": scope_stack[-1]["in_function"] }) elif token.str == '}': if len(scope_stack) > 1: scope_stack.pop() elif token.str == '(' and tokens[i-1].str in ['if', 'while', 'for', 'switch']: # 控制语句的条件,不改变全局状态 pass elif token.str == ';' and tokens[i-1].str == ')': # 函数声明结束 if scope_stack[-1]["in_function"]: scope_stack[-1]["in_function"] = False elif token.str == '(' and tokens[i-1].isName and not scope_stack[-1]["in_function"]: # 函数定义开始 scope_stack[-1]["in_function"] = True # 遍历所有变量 for var in cfg.variables: if not var.nameToken: # 确保变量有名称为Token continue var_name = var.nameToken.str if var_name in checked_vars: # 跳过已检查变量 continue checked_vars.add(var_name) if var.isGlobal: check_element_comment( element_token=var.nameToken, element_type=f"global variable definition", comment_positions=comment_positions, tokens=tokens ) elif var.isStatic: check_element_comment( element_token=var.nameToken, element_type=f"global variable definition", comment_positions=comment_positions, tokens=tokens ) # 遍历所有函数调用 for token in cfg.tokenlist: # 忽略非函数 if not token.function: continue # 检查是否是声明或定义 if ((token.function.tokenDef and token == token.function.tokenDef) or (token.function.token and token == token.function.token)): check_element_comment( element_token=token, element_type="function", comment_positions=comment_positions, tokens=tokens ) def check_element_comment(element_token, element_type, comment_positions, tokens): """检查指定元素是否有注释""" if has_comment(element_token, comment_positions, tokens): return # 生成错误消息 error_msg = f"Missing comment for {element_type} '{element_token.str}'" error_key = (element_token.linenr, element_token.column, error_msg) if error_key not in reported_errors: cppcheck.reportError( element_token, 'severity:style', error_msg ) reported_errors.add(error_key) def has_comment(element_token, comment_positions, tokens): """检查元素是否有关联注释""" # 1. 检查同行行尾注释 for pos in comment_positions: if pos[0] == element_token.linenr and pos[1] > element_token.column: return True # 2. 检查上方连续注释 current_line = element_token.linenr - 1 consecutive_comments = 0 # 向上查找最多3行连续注释 while current_line > 0 and consecutive_comments < 3: has_line_comment = False # 检查当前行是否有注释 for pos in comment_positions: if pos[0] == current_line: # 确保是整行注释(不是行尾注释) line_has_code = any( t.linenr == current_line and not t.str.startswith(('//', '/*')) and not t.str in ['', '{', '}'] # 忽略空token和括号 for t in tokens ) if not line_has_code: has_line_comment = True break if has_line_comment: consecutive_comments += 1 current_line -= 1 else: break # 如果有至少一行上方注释 return consecutive_comments > 0
最新发布
10-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值