variable definition

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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}'`

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值