这个 bash 脚本中详细发生了什么?
What happens in detail in this bash script?
根据了解 bash 脚本,我发现这个有趣的 bash 脚本。
我很好奇这是如何工作的细节,但我是 bash 脚本的初学者。
我明白,一个分号分隔了几个命令,并且有一些预定义的变量被使用,比如 $LINES
和 $RANDOM
一些东西稍后会通过管道传输,但仅此而已。
为什么只有粘贴在一行中才能执行?
信息:
我在 Windows 10
上使用 Git Bash
也许有经验的人可以走一走?需要额外的解释。
echo -e "\e[1;40m" ; clear ; while :; do echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 )) ;sleep 0.05; done|awk '{ letters="ABCDEFGHIJKLMNOPQRSTUVWXYZムキノフレアイ√メリ0123456789()₿₹₵иηøρгţчςợгµαϤδχϞϗγπϥ";c=; letter=substr(letters,c,1);a[]=0;for (x in a) {o=a[x];a[x]=a[x]+1; printf "3[%s;%sH3[2;32m%s",o,x,letter; printf "3[%s;%sH3[1;37m%s3[0;0H",a[x],x,letter;if (a[x] >= ) { a[x]=0; } }}'
非常有趣的脚本。为了清楚起见,让我分解一下 one-liner 添加一些评论并稍作修改:
#!/bin/bash
shopt -s checkwinsize # bash updates the values of LINES and COLUMNS
echo -e "\e[1;40m"
clear
while :; do
echo "$LINES" "$COLUMNS" "$(( $RANDOM % $COLUMNS))" "$(( $RANDOM % 72 ))"
sleep 0.05
done | awk '
{letters="ABCDEFGHIJKLMNOPQRSTUVWXYZムキノフレアイ√メリ0123456789()₿₹₵иηøρгţчςợгµαϤδχϞϗγπϥ"
c=
letter=substr(letters, c, 1) # pick a character from letters in random
a[]=0 # a[x] holds a row position of column x
# pick a column in random to put a new character
for (x in a) { # loop over the columns which has characters on it
o=a[x] # original row position of column x
a[x]=a[x]+1 # increment the row position of column x
# to express the character falls down
printf "3[%s;%sH3[2;32m%s", o, x, letter
printf "3[%s;%sH3[1;37m%s3[0;0H", a[x], x, letter
if (a[x] >= ) { # if the row position of column x reaches the bottom line
a[x]=0 # then reset the row position
}
}
}'
转义序列,动画的重点,需要额外的解释:
echo -e "\e[1;40m" set the character bold, and the background black
printf "3[%s;%sH", o, x move cursor to row "o", column "x"
printf "3[2;32m%s", letter put "letter" with dim green color
printf "3[%s;%sH", a[x], x move cursor down to row "a[x]", column "x"
printf "3[1;37m%s", letter put "letter" with white color
printf "3[0;0H" move cursor to the top-left corner
根据了解 bash 脚本,我发现这个有趣的 bash 脚本。
我很好奇这是如何工作的细节,但我是 bash 脚本的初学者。
我明白,一个分号分隔了几个命令,并且有一些预定义的变量被使用,比如 $LINES
和 $RANDOM
一些东西稍后会通过管道传输,但仅此而已。
为什么只有粘贴在一行中才能执行?
信息: 我在 Windows 10
上使用 Git Bash也许有经验的人可以走一走?需要额外的解释。
echo -e "\e[1;40m" ; clear ; while :; do echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 )) ;sleep 0.05; done|awk '{ letters="ABCDEFGHIJKLMNOPQRSTUVWXYZムキノフレアイ√メリ0123456789()₿₹₵иηøρгţчςợгµαϤδχϞϗγπϥ";c=; letter=substr(letters,c,1);a[]=0;for (x in a) {o=a[x];a[x]=a[x]+1; printf "3[%s;%sH3[2;32m%s",o,x,letter; printf "3[%s;%sH3[1;37m%s3[0;0H",a[x],x,letter;if (a[x] >= ) { a[x]=0; } }}'
非常有趣的脚本。为了清楚起见,让我分解一下 one-liner 添加一些评论并稍作修改:
#!/bin/bash
shopt -s checkwinsize # bash updates the values of LINES and COLUMNS
echo -e "\e[1;40m"
clear
while :; do
echo "$LINES" "$COLUMNS" "$(( $RANDOM % $COLUMNS))" "$(( $RANDOM % 72 ))"
sleep 0.05
done | awk '
{letters="ABCDEFGHIJKLMNOPQRSTUVWXYZムキノフレアイ√メリ0123456789()₿₹₵иηøρгţчςợгµαϤδχϞϗγπϥ"
c=
letter=substr(letters, c, 1) # pick a character from letters in random
a[]=0 # a[x] holds a row position of column x
# pick a column in random to put a new character
for (x in a) { # loop over the columns which has characters on it
o=a[x] # original row position of column x
a[x]=a[x]+1 # increment the row position of column x
# to express the character falls down
printf "3[%s;%sH3[2;32m%s", o, x, letter
printf "3[%s;%sH3[1;37m%s3[0;0H", a[x], x, letter
if (a[x] >= ) { # if the row position of column x reaches the bottom line
a[x]=0 # then reset the row position
}
}
}'
转义序列,动画的重点,需要额外的解释:
echo -e "\e[1;40m" set the character bold, and the background black
printf "3[%s;%sH", o, x move cursor to row "o", column "x"
printf "3[2;32m%s", letter put "letter" with dim green color
printf "3[%s;%sH", a[x], x move cursor down to row "a[x]", column "x"
printf "3[1;37m%s", letter put "letter" with white color
printf "3[0;0H" move cursor to the top-left corner