My python scripy print a string by print("declare -A gaps=( [2019-2-24]=4 )")
and I can run declare -A gaps=( [2019-2-24]=4 )
on a bash shell to create a dictionary named gaps
.
In my bash script, I use a variable named gap_string
to access the output of python scripy. Then I use backquote surrounded the gap_string
expected to create a dictionary which failed got and error: declare: “[2019-2-24]=4”: is not a valid identifier
.
More details:
code in my bash script:
declare -A birthdays=(["${year}0120"]="GG") gap_string=`/home/roach/.config/argos/LunarSolarConverter.py ${!birthdays[@]}` if [ $? -eq 0 ]; then `$gap_string` fi
code in my Python script:
if __name__ == '__main__': converter = LunarSolarConverter() gaps_string = ["declare -A gaps=("] today = datetime.datetime.today() today_date = today.date() year = today.year isleap = (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)) days_this_year = 366 if isleap else 365 for i in range(1, len(sys.argv)): year, month, day = int(sys.argv[i][:-4]), int(sys.argv[i][-4:-2]), int(sys.argv[i][-2:]) lunar = Lunar(year, month, day, isleap) solar = converter.LunarToSolar(lunar) gap = (datetime.date(solar.solarYear, solar.solarMonth, solar.solarDay) - today_date).days % days_this_year if gap <= 4: gaps_string.append(f"[{solar.solarYear}-{solar.solarMonth}-{solar.solarDay}]={gap}") gaps_string.append(")") if len(gaps_string) == 2: sys.exit(1) else: print(" ".join(gaps_string)) sys.exit(0)
What python script do is change Chinese lunardate to solardate, and then calculate the days between today and the specific solardate, and then to remind my family members' birthday to me.
gap_string
. And also the code Then I use backquote surrounded the gap_string expected to create a dictionarydeclare -A gaps=( [2019-2-24]=4 )
got this.