Arrays 在Mac OS X的bash/shell/unix中,不使用readarray或mapfile命令,将.txt文件的每一行读入数组
我正在用bash/shell为我的个人库编写一个简单的目录程序。我在OSX中开发,一直在思考如何在不访问UNIX命令(如Arrays 在Mac OS X的bash/shell/unix中,不使用readarray或mapfile命令,将.txt文件的每一行读入数组,arrays,bash,macos,shell,unix,Arrays,Bash,Macos,Shell,Unix,我正在用bash/shell为我的个人库编写一个简单的目录程序。我在OSX中开发,一直在思考如何在不访问UNIX命令(如readarray或mapfile)的情况下配置我的程序 以下是我尝试做的逻辑: 通过读取一系列配置文件初始化我的程序,这些文件包含在上一个会话中编目的各种属性书籍。我希望将每个.txt文件的信息(每行一个条目,每本书一个条目)读取并索引到一个数组中(例如titles.txt->titles=(),authors.txt->authors=(),etc…) 一旦数组中填充了编
readarray
或mapfile
)的情况下配置我的程序
以下是我尝试做的逻辑:
- 通过读取一系列配置文件初始化我的程序,这些文件包含在上一个会话中编目的各种属性书籍。我希望将每个.txt文件的信息(每行一个条目,每本书一个条目)读取并索引到一个数组中(例如titles.txt->titles=(),authors.txt->authors=(),etc…)
- 一旦数组中填充了编目信息,我想通过将新信息附加到每个数组中,将新书的属性输入到编目中。此新内容将在确认提示后推送到阵列中,如“您确定此信息正确吗,如果是,将保存到catlog,如果否,将丢弃”
- 在将新书属性成功添加到相应的数组后,我想将更新的数组更新/保存到用于初始化程序的相同.txt配置文件中,并将最新信息附加到每个文件的底部
mapfile
init_catalog(){
#read catalog entries into arrays
printf "%s\n" "Loading catalog..."
mapfile -t TITLES < titles.txt
mapfile -t AUTHORS < authors.txt
mapfile -t EDITORS < editors.txt
mapfile -t PUB_NAMES < pub-names.txt
mapfile -t PUB_LOCATIONS < pub-locations.txt
mapfile -t PUB_DATES < pub-dates.txt
mapfile -t PAGE_COUNTS < page-counts.txt
mapfile -t GENRES < genres.txt
mapfile -t ISBN_NUMS < isbn-nums.txt
printf "%s\n" "Catalog loaded..."
INITIALIZED="true"
}
save_catalog_entry(){
clear
printf "%s\n\n" "You have entered the following information for this work:"
printf "%s\n" "PENDING ENTRIES FOR THIS WORK..."
underline_above_text
printf "Title: $WORK_TITLE\n"
printf "Author(s): $WORK_AUTHOR\n"
printf "Editor(s): $WORK_EDITOR\n"
printf "Publisher: $WORK_PUB_NAME\n"
printf "Publication Location: $WORK_PUB_LOCATION\n"
printf "Publication Date: $WORK_PUB_DATE\n"
printf "Page Count: $WORK_PAGE_COUNT\n"
printf "Genre: $WORK_GENRE\n\n"
underline_above_text
printf "%s\n" "Is this information correct? If yes, all pending entries for this work will be saved to catalog. If no, all pending entries for this work will be discarded."
yes_or_no
if [ $USER_INPUT_YES_NO == "true" ]
then
#save input to catalog files
printf "%s\n" "${TITLES[@]}" > titles.txt
printf "%s\n" "${AUTHORS[@]}" > authors.txt
printf "%s\n" "${EDITORS[@]}" > editors.txt
printf "%s\n" "${PUB_NAMES[@]}" > pub-names.txt
printf "%s\n" "${PUB_LOCATIONS[@]}" > pub-locations.txt
printf "%s\n" "${PUB_DATES[@]}" > pub-dates.txt
printf "%s\n" "${PAGE_COUNTS[@]}" > page-counts.txt
printf "%s\n" "${GENRES[@]}" > genres.txt
printf "%s\n" "${ISBN_NUMS[@]}" > isbn-nums.txt
else
DISCARDING_ENTRIES="true"
printf "%s\n" "All input information for this work has been discarded. Please, try again."
fi
}
init_目录(){
#将目录条目读入数组
printf“%s\n”正在加载目录
映射文件-t TITLESTITLES.txt
printf“%s\n”${AUTHORS[@]}>AUTHORS.txt
printf“%s\n”${EDITORS[@]}>EDITORS.txt
printf“%s\n”${PUB_NAMES[@]}>PUB-NAMES.txt
printf“%s\n”${PUB_LOCATIONS[@]}>PUB-LOCATIONS.txt
printf“%s\n”${PUB_DATES[@]}>PUB-DATES.txt
printf“%s\n”${PAGE_COUNTS[@]}>PAGE-COUNTS.txt
printf“%s\n”${GENRES[@]}>GENRES.txt
printf“%s\n”${ISBN_NUMS[@]}>ISBN-NUMS.txt
其他的
丢弃\u条目=“真”
printf“%s\n”已放弃此工作的所有输入信息。请重试
fi
}
有人知道我应该/可以使用哪些UNIX命令从shell访问这些配置文件,将其内容读入数组,然后将数组的新内容保存回.txt配置文件吗?谢谢你的帮助 您可以使用常规数组构造方法(
arr[n]=“asdas”
和arr=(“123”45“ss gg”)
)。考虑这个Tythel.txt文件:
once upon a time
pink "cherry" troubles
obfuscator 2
只需使用bash的
f=$将其读入一个变量(如果行中包含带空格的短语,则解决方案无效。它将使每个单词成为一个数组元素,这与您自己的示例相反。
oifs=$IFS # save a copy of the input field separator character list
IFS=$'\n' arr=( $(< titles.txt) )
IFS=$oifs # restore
$ echo "${arr[@]}"
once upon a time pink "cherry" troubles obfuscator 2
$ echo "${arr[1]}"
pink "cherry" troubles
oifs=$IFS
IFS=$'\n' arr=( $(sed -n 's,",\\",g; s,^.*$,"&",p' titles.txt) )
IFS=$oifs
$ echo "${arr[@]}"
"once upon a time" "pink \"cherry\" troubles" "obfuscator 2"
$ echo "${arr[1]}"
"pink \"cherry\" troubles"