在bash脚本中创建时间戳变量

本文翻译自:Create timestamp variable in bash script

I am trying to create a timestamp variable in a shell script to make the logging a little easier. 我正在尝试在shell脚本中创建一个timestamp变量,以使记录变得更加容易。 I want to create the variable at the beginning of the script and have it print out the current time whenever I issue echo $timestamp . 我想在脚本的开头创建变量,并在每次发出echo $timestamp时将其打印出当前时间。 It proving to be more difficult then I thought. 事实证明,这比我想的要困难得多。 Here are some things I've tried: 这是我尝试过的一些方法:

timestamp="(date +"%T")" echo prints out (date +"%T") timestamp="(date +"%T")" echo输出(date +"%T")

timestamp="$(date +"%T")" echo prints the time when the variable was initialized. timestamp="$(date +"%T")" echo显示变量初始化的时间。

Other things I've tried are just slight variations that didn't work any better. 我尝试过的其他内容只是一些细微的变化,效果并不理想。 Does anyone know how to accomplish what I'm trying to do? 有谁知道我该怎么做?


#1楼

参考:https://stackoom.com/question/19bi6/在bash脚本中创建时间戳变量


#2楼

使用命令替换:

timestamp=$( date +%T )

#3楼

In order to get the current timestamp and not the time of when a fixed variable is defined, the trick is to use a function and not a variable: 为了获得当前时间戳而不是定义固定变量的时间,技巧是使用函数而不是变量:

#!/bin/bash

# Define a timestamp function
timestamp() {
  date +"%T"
}

# do something...
timestamp # print timestamp
# do something else...
timestamp # print another timestamp
# continue...

If you don't like the format given by the %T specifier you can combine the other time conversion specifiers accepted by date . 如果您不喜欢%T说明符给出的格式,则可以结合date接受的其他时间转换说明符。 For GNU date , you can find the complete list of these specifiers in the official documentation here: https://www.gnu.org/software/coreutils/manual/html_node/Time-conversion-specifiers.html#Time-conversion-specifiers 对于GNU date ,您可以在以下官方文档中找到这些说明符的完整列表: https : //www.gnu.org/software/coreutils/manual/html_node/Time-conversion-specifiers.html#Time-conversion-specifiers


#4楼

If you want to get unix timestamp, then you need to use: 如果要获取unix时间戳,则需要使用:

timestamp=$(date +%s)

%T will give you just the time; %T会给你时间。 same as %H:%M:%S (via http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/ ) %H:%M:%S (通过http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/


#5楼

You can use 您可以使用

timestamp=`date --rfc-3339=seconds`

This delivers in the format 2014-02-01 15:12:35-05:00 此格式为2014-02-01 15:12:35-05:00

The back-tick ( ` ) characters will cause what is between them to be evaluated and have the result included in the line. 反引号( ` )字符将导致对它们之间的内容进行评估,并将结果包括在该行中。 date --help has other options. date --help还有其他选项。


#6楼

timestamp=$(awk 'BEGIN {srand(); print srand()}')

没有值的srand在大多数Awk实现中都使用当前时间戳。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我会尝试用bash脚本来实现以下步骤: 1. 创建一个变量,用来保存用户输入的值; 2. 使用if语句来检查用户输入的值是否正确; 3. 使用case语句来处理不同的用户输入; 4. 使用循环(for,while,until)来处理不断变化的用户输入; 5. 利用函数来封装代码,使代码更加灵活可操作; 6. 利用数组来存储用户输入的值,并利用for循环来处理; 7. 利用指针来实现数组的动态管理; 8. 利用文本处理命令(grep,sed,awk)来对文本数据进行处理; 9. 利用正则表达式来匹配搜索字符串; 10. 利用curl命令来发送http请求,并读取响应结果。 ### 回答2: 如要写一个复杂的bash脚本,可以考虑以下示例: #!/bin/bash # 获取输入参数并验证 if [[ $# -ne 3 ]]; then echo "Usage: script.sh [parameter1] [parameter2] [parameter3]" exit 1 fi # 定义变量 param1=$1 param2=$2 param3=$3 # 执行逻辑 echo "脚本开始运行..." # 根据参数1的值执行不同的操作 case $param1 in "option1") echo "选项1被选择" # 执行相关操作 ;; "option2") echo "选项2被选择" # 执行相关操作 ;; *) echo "无效的选项" exit 1 ;; esac # 根据参数2的值执行不同的操作 if [[ $param2 -gt 10 ]]; then echo "参数2大于10" else echo "参数2小于等于10" fi # 根据参数3的值循环执行某个操作 echo "开始循环操作..." for ((i=1; i<=$param3; i++)); do echo "第 $i 次循环操作" # 执行循环操作 done echo "脚本运行结束。" 这个脚本具有以下功能: - 验证脚本输入参数的数量是否正确。 - 接受3个输入参数,并将其分配给bash变量。 - 使用case语句根据参数1的值执行不同的操作。 - 使用条件语句判断参数2的大小,并输出相应的信息。 - 使用循环语句根据参数3的值循环执行某个操作。 - 输出脚本的运行开始和结束信息。 当然,具体复杂的脚本可以根据实际需求进行修改和扩展。 ### 回答3: 复杂的Bash脚本的设计可以根据实际需求和场景来进行,我在这里给出一个示例: #!/bin/bash # 这个脚本用于统计指定目录下文件的数量和总大小,并将结果输出到指定的日志文件 # 设置要统计的目录和输出日志文件路径 directory="/path/to/directory" logfile="/path/to/logfile.log" # 创建一个函数用于计算文件数量和总大小 calculate_stats() { local count=0 local total_size=0 local filename # 使用find命令遍历目录下的所有文件,并进行计数和大小总计 while IFS= read -rd '' filename; do if [[ ! -d "$filename" ]]; then ((count++)) size=$(du -bs "$filename" | cut -f1) ((total_size+=size)) fi done < <(find "$directory" -type f -print0) # 输出统计结果到日志文件 echo "$(date +%Y-%m-%d %H:%M:%S) - Found $count files with a total size of $total_size bytes" >> "$logfile" } # 调用函数计算统计结果 calculate_stats 这个示例脚本实现了以下功能: 1. 设置要统计的目录和输出日志文件路径。 2. 定义了一个名为"calculate_stats"的函数用于计算文件数量和总大小。 3. 在函数内部使用了find命令遍历指定目录下所有文件,并对文件数量和总大小进行计算。 4. 使用echo命令将统计结果以时间戳的形式输出到指定的日志文件。 请注意,这只是一个简单的示例脚本,真实的复杂脚本可能包含更多的功能和逻辑。编写复杂的Bash脚本需要有一定的编程知识和经验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值