如何製作顯示西方十二生肖的腳本?

如何製作顯示西方十二生肖的腳本?

我想製作顯示西方十二生肖的腳本,它在命令列上接受一個人的生日作為輸入並打印出以下內容:

該人出生在星期幾 他們的西方生肖(巨蟹座、獅子座、天秤座等) 他們的中國生肖(木兔、金蛇、火豬等) 他們的星座(使用 Linux 幸運餅乾)程序用於此)

有人做過這個嗎?

答案1

使用指令的格式選項尋找星期幾date

#!/bin/sh
date -d"${1:mm/dd/yyyy}" +"%A"

透過在表中找出年份來確定中國生肖,如下所示:

#!/bin/sh    
awk -v year="${1:?Year}" '
    $0 ~ year {
        print $1
        found = 1
        exit
    }
    END {
        if (!found)
            exit 1
    }
' <<EOF
    Rat 1912 1924 1936 1948 1960 1972 1984 1996 2008 2020
    Ox 1913 1925 1937 1949 1961 1973 1985 1997 2009 2021
    Tiger 1914 1926 1938 1950 1962 1974 1986 1998 2010 2022
    Rabbit 1915 1927 1939 1951 1963 1975 1987 1999 2011 2023
    Dragon 1916 1928 1940 1952 1964 1976 1988 2000 2012 2024
    Snake 1917 1929 1941 1953 1965 1977 1989 2001 2013 2025
    Horse 1918 1930 1942 1954 1966 1978 1990 2002 2014 2026
    Goat 1919 1931 1943 1955 1967 1979 1991 2003 2015 2027
    Monkey 1920 1932 1944 1956 1968 1980 1992 2004 2016 2028
    Rooster 1921 1933 1945 1957 1969 1981 1993 2005 2017 2029
    Dog 1922 1934 1946 1958 1970 1982 1994 2006 2018 2030
    Pig 1923 1935 1947 1959 1971 1983 1995 2007 2019 2031
EOF

要確定西方生肖,需要確定生日是否在某個時間範圍內。這需要一些邏輯。我們再次需要某種表格。

#!/bin/sh
awk --assign M="${1:?Month}" --assign D="${2:?Day}" '
    M == $1 && D >= $2  || M == $3 && D <= $4 {
        print $5
        found = 1
        exit
    }
    END {
        if (!found) {
            exit 1
        }
    }
' <<EOF
    3   21  4   19  Aries
    4   20  5   20  Taurus
    5   21  6   20  Gemini
    6   21  7   22  Cancer
    7   23  8   22  Leo
    8   23  9   22  Virgo
    9   23  10  22  Libra
    10  23  11  21  Scorpio
    11  22  12  21  Sagittarius
    12  22  1   19  Capricorn
    1   20  2   18  Aquarius
    2   19  3   20  Pisces
EOF

相關內容