[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/ ,如需转载,请注明出处,否则将追究法律责任。