質問

Im trying to create a list of file-paths from a file but I don't seem to be able to get around the spaces in the file paths.

    # 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 

The file path in the Current_series list is: /Volumes/Lara's Hard Drive/LARA HARD DRIVE/Series/The Big Bang Theory 3/The.Big.Bang.Theory S03E11.avi

and

/Volumes/Lara's Hard Drive/LARA HARD DRIVE/Series/nakitaS03E11.avi

So i would like them two be 1 and 2 respectively in my list but I get the following result.

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: 
役に立ちましたか?

解決

You need to trick it a little bit:

# 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"

Executing:

$ ./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

The key point is that you have to convert a file into an array.

This part converts it into a "string one", "string two" format:

$ 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,

While this one creates an array in the variable list based on the comma delimiter that was set in the previous step:

IFS=, read -a list <<< "$Current_list"

他のヒント

You could try to read each line of Current_series_list.txt separately into an array element and select from the expanded array "${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 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top