0

我试图从文件创建文件路径列表,但我似乎无法绕过文件路径中的空格。

    # Show current series list
    PS3="Type a number or 'q' to quit: "
    # Create a list of files to display
    Current_list=`cat Current_series_list.txt`

    select fileName in $Current_list; do
        if [ -n "$fileName" ]; then
            Selected_series=${fileName}
        fi
        break
    done 

Current_series 列表中的文件路径为:/Volumes/Lara's Hard Drive/LARA HARD DRIVE/Series/The Big Bang Theory 3/The.Big.Bang.Theory S03E11.avi

/Volumes/Lara 的硬盘/LARA 硬盘/系列/nakitaS03E11.avi

所以我希望他们两个分别在我的列表中为 1 和 2,但我得到以下结果。

1) /Volumes/Lara's      6) Big
2) Hard             7) Bang
3) Drive/LARA       8) Theory
4) HARD         9) 3/The.Big.Bang.Theory
5) DRIVE/Series/The    10) S03E11.avi
Type a number or 'q' to quit: 
4

2 回答 2

0

您需要稍微欺骗一下:

# Show current series list
PS3="Type a number or 'q' to quit: "
# Create a list of files to display
Current_list=$(tr '\n' ',' < Current_series_list.txt)
IFS=, read -a list <<< "$Current_list"

select fileName in "${list[@]}"; do
     if [ -n "$fileName" ]; then
         Selected_series="${fileName}"
     fi
     break
done 

echo "you selected $fileName"

执行:

$ ./a
1) /Volumes/Lara's Hard Drive/LARA HARD DRIVE/Series/The Big Bang Theory3/The.Big.Bang.Theory S03E11.avi
2) /Volumes/Lara's Hard Drive/LARA HARD DRIVE/Series/nakitaS03E11.avi
Type a number or 'q' to quit: 2
you selected /Volumes/Lara's Hard Drive/LARA HARD DRIVE/Series/nakitaS03E11.avi

关键是您必须将文件转换为数组。

这部分将其转换为"string one", "string two"格式:

$ tr '\n' ',' < Current_series_list.txt 
/Volumes/Lara's Hard Drive/LARA HARD DRIVE/Series/The Big Bang Theory 3/The.Big.Bang.Theory S03E11.avi,/Volumes/Lara's Hard Drive/LARA HARD DRIVE/Series/nakitaS03E11.avi,

list虽然这个基于上一步中设置的逗号分隔符在变量中创建了一个数组:

IFS=, read -a list <<< "$Current_list"
于 2013-10-10T13:15:15.967 回答
0

您可以尝试将每一行Current_series_list.txt分别读入一个数组元素并从扩展数组中选择"${Current_array[@]}"

# Show current series list
PS3="Type a number or 'q' to quit: "
# Create an array of files to display
Current_array=()
while read line; do Current_array+=("$line"); done < Current_series_list.txt 

select fileName in "${Current_array[@]}"; do
    if [ -n "$fileName" ]; then
        Selected_series=${fileName}
    fi
    break
done 
于 2013-10-10T13:37:43.557 回答