Я работаю над сценарием для случайных прогнозов и к сожалению вместо сценария, проводящего сплошные линии из двух случайных списков (Вещи и Эффекты), он печатает случайное слово от каждого. Может кто-то показывать мне, как иметь печать сценария сплошная линия из двух списков. Ниже мой сценарий:
#!/bin/bash
# Random lists picks
# This is a script for picking random choices as a prediction.
Things="Walk
Song
Talk
Friend
Spontaneous act
Call"
Effects="will bless you
will lift your spirit
is healthy
can help
will touch you
will make you smile
is what what you need
makes a difference in your life
can add comfort
isn't necessarily without virtue
adds something to your day"
things=($Things)
effects=($Effects)
num_things=${#things[*]}
num_effects=${#effects[*]}
echo -n " A ${things[$((RANDOM%num_things))]} "
echo ${effects[$((RANDOM%num_effects))]}
Необходимо установить IFS
(Внутренний Разделитель полей) переменная bash
к новой строке \n
при создании массива из переменной так, чтобы каждая новая строка отделилась, запись переменной будет взята в качестве элемента массива. В Вашем основном сценарии массив создается из записей переменной, разделенной на значении по умолчанию IFS
значение т.е. пространство, вкладка и новая строка.
, Короче говоря необходимо изменить те две строки объявления массива на:
IFS= Так, Ваш заключительный сценарий:
#!/bin/bash
# Random lists picks
# This is a script for picking random choices as a prediction.
Things="Walk
Song
Talk
Friend
Spontaneous act
Call"
Effects="will bless you
will lift your spirit
is healthy
can help
will touch you
will make you smile
is what what you need
makes a difference in your life
can add comfort
isn't necessarily without virtue
adds something to your day"
IFS=\n' things=( $Things )
IFS=\n' effects=( $Effects )
num_things=${#things[*]}
num_effects=${#effects[*]}
echo -n "A ${things[$((RANDOM%num_things))]} "
echo "${effects[$((RANDOM%num_effects))]}"
\n' things=( $Things )
IFS= Так, Ваш заключительный сценарий:
#!/bin/bash
# Random lists picks
# This is a script for picking random choices as a prediction.
Things="Walk
Song
Talk
Friend
Spontaneous act
Call"
Effects="will bless you
will lift your spirit
is healthy
can help
will touch you
will make you smile
is what what you need
makes a difference in your life
can add comfort
isn't necessarily without virtue
adds something to your day"
IFS=\n' things=( $Things )
IFS=\n' effects=( $Effects )
num_things=${#things[*]}
num_effects=${#effects[*]}
echo -n "A ${things[$((RANDOM%num_things))]} "
echo "${effects[$((RANDOM%num_effects))]}"
\n' effects=( $Effects )
Так, Ваш заключительный сценарий:
#!/bin/bash
# Random lists picks
# This is a script for picking random choices as a prediction.
Things="Walk
Song
Talk
Friend
Spontaneous act
Call"
Effects="will bless you
will lift your spirit
is healthy
can help
will touch you
will make you smile
is what what you need
makes a difference in your life
can add comfort
isn't necessarily without virtue
adds something to your day"
IFS=\n' things=( $Things )
IFS=\n' effects=( $Effects )
num_things=${#things[*]}
num_effects=${#effects[*]}
echo -n "A ${things[$((RANDOM%num_things))]} "
echo "${effects[$((RANDOM%num_effects))]}"
Я думаю, что у Вас было немного недоразумения с использованием массивов.
#!/bin/bash
# Random lists picks
# This is a script for picking random choices as a prediction.
# Create array "things"
things=('Walk'
'Song'
'Talk'
'Friend'
'Spontaneous act'
'Call')
# Create array "effects"
effects=('will bless you'
'will lift your spirit'
'is healthy'
'can help'
'will touch you'
'will make you smile'
'is what what you need'
'makes a difference in your life'
'can add comfort'
"isn't necessarily without virtue"
'adds something to your day')
# Place the array size
num_things=${#things[@]}
num_effects=${#effects[@]}
echo -n " A ${things[$((RANDOM%num_things))]} "
echo "${effects[$((RANDOM%num_effects))]}"