ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [20221111]bash eval设置变量问题.txt

[20221111]bash eval设置变量问题.txt

原创 Linux操作系统 作者:lfree 时间:2022-11-11 10:51:04 0 删除 编辑

[20221111]bash eval设置变量问题.txt

--//在我以前写的bash shell脚本中设置变量不合理,代码如下:
current_time=`/bin/date  +"%Y%m%d_%H:%M:%S"`
current_date=`/bin/date +"%Y%m%d"`

year=`/bin/date +"%Y"`
month=`/bin/date +"%m"`
day=`/bin/date +"%d"`
week=`/bin/date +"%w"`

--//相当于调用6次/bin/date命令,不合理!!能否调用一次完成全部赋值呢?

$ /bin/date  +"%Y%m%d_%H:%M:%S %Y%m%d %Y %m %d %w"
20221111_09:24:58 20221111 2022 11 11 5

$ /bin/date  +"current_time=%Y%m%d_%H:%M:%S;current_date=%Y%m%d;year=%Y;month=%m;day=%d;week=%w;"
current_time=20221111_09:30:29;current_date=20221111;year=2022;month=11;day=11;week=5;

--//如何将上面的输出变成赋值语句呢?使用内部命令eval.

$ eval $(/bin/date  +"current_time=%Y%m%d_%H:%M:%S;current_date=%Y%m%d;year=%Y;month=%m;day=%d;week=%w;")

$ echo $current_time $current_date $year $month $day $week
20221111_09:31:59 20221111 2022 11 11 5

--//OK,这样仅仅需要执行1次就实现多个变量赋值.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2922882/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7969886