gpt4 book ai didi

bash - 从 bash 中的确切日期打印星期几

转载 作者:行者123 更新时间:2023-11-29 09:13:53 25 4
gpt4 key购买 nike

我正在尝试打印给定日期的星期几。此命令运行良好:

TARGET=$(date -u -d'2015-10-25' '+%u') 

但是在我的 bash 脚本中有一个错误,应该是什么错误?

#!/bin/bash
day=25
month=10
year=2015
command1='date -u -d'
command3=''\'
command2=$year-$month-$day
fullcommand=$command1$command3$command2$command3' '$command3'+%u'$command3
echo $fullcommand
TARGET=$($fullcommand)
echo $TARGET

出现错误:

date: the argument ‘'+%u'’ lacks a leading '+';

最佳答案

不需要使用那么多的临时变量,在 shell 中绝对不会在另一个单引号内转义单引号

简化成这样:

#!/bin/bash
day=25
month=10
year=2015
command1='date -u -d'
TARGET=$(date -u -d "$year-$month-$day" '+%u')
echo $TARGET

关于bash - 从 bash 中的确切日期打印星期几,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com