Я написал прилагаемый сценарий оболочки bstls.sh, который использует команду select, чтобы выбрать подпапку или файл в папке, переданной как аргумент, в котором выполняется команда ls.
#! /bin/bash
#Personal version of shell command ls which presents to user the list of files with shell command select
#Usage: bstls.sh folder
#if parameter numbers is different from one, exit
if [ $# -ne 1 ]
then
echo -e "Usage:\n\tbstls folder"
exit 1
fi
PS3='Which element to ls?'
#command sed substitutes blank spaces with £ in file or folder names
#in this way user can select files or folders with blank spaces in between
list="Exit $(ls "$1" | sed 's/ /£/')"
select option in $list
do
if [ "$option" = "Exit" ] #if user selects Exit, then exit the program
then
exit 0
elif [ -n "$option" ] #if name is valid, shows the files inside
then
#reuse sed command to reconvert to original file name
filename=$(echo "$option" | sed 's/£/ /')
ls "$1"/"$filename"
else #if the number of the choice given by user is wrong, exit
echo "Invalid choice ($REPLY)!"
fi
done
Моя основная проблема - показать имена файлов с пробелами в списке опций выбора. Например, если у меня есть временная папка с подпапкой foo и файлом hello world внутри нее, тогда запуск следующего
./bstls.sh temp
должен заставить меня выбрать из опции
1)Exit
2)foo
3)hello
4)world
[d6 ] (последние два отделились друг от друга). Теперь я перехожу к своему реальному вопросу. Я попытался решить эту проблему, преобразуя пробелы с символом £ с командой sed.
list="Exit $(ls "$1" | sed 's/ /£/')"
Таким образом, имена с пробелами могут обрабатываться как одна командой select. Затем я снова использую символ £ с пробелом, когда я использую команду ls.
filename=$(echo "$option" | sed 's/£/ /')
Итак, при запуске
./bstls.sh temp
Я получаю выбор
1)Exit
2)foo
3)hello£world
Вот вопрос (наконец): есть ли способ вывода в имена файлов меню выбора без символа £?