带有选择命令的 Shell 脚本以显示文件列表

Dav*_*erf 2 command-line bash scripts filename

我已经编写了附加的bstls.shshell 脚本,它使用命令 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
Run Code Online (Sandbox Code Playgroud)

我的主要问题是如何在选择选项列表中显示带有空格的文件名。例如,如果我有一个文件夹temp,其中包含子文件夹foo和其中的文件hello world,则启动以下命令

./bstls.sh temp  
Run Code Online (Sandbox Code Playgroud)

应该让我从选项中选择

1)Exit 
2)foo 
3)hello 
4)world  
Run Code Online (Sandbox Code Playgroud)

(最后两个彼此分开)。

现在我要回答我真正的问题。我试图用 sed 命令用符号 £ 转换空格来解决这个问题。

list="Exit $(ls "$1" | sed 's/ /£/')"  
Run Code Online (Sandbox Code Playgroud)

这样,带有空格的名称可以被 select 命令作为一个整体处理。
然后,当我使用 ls 命令时,我再次用空格更改符号 £。

filename=$(echo "$option" | sed 's/£/ /')  
Run Code Online (Sandbox Code Playgroud)

所以现在,当启动

./bstls.sh temp  
Run Code Online (Sandbox Code Playgroud)

我有选择

1)Exit 
2)foo
3)hello£world
Run Code Online (Sandbox Code Playgroud)

这是问题(最后):有没有办法在没有£符号的情况下在选择菜单中输出文件名?

ste*_*ver 7

使用shell glob而不是ls

select option in "Exit" "$1"/*
.
.
.
elif [ -n "$option" ]; then
  ls "$option"
else
.
.
.
Run Code Online (Sandbox Code Playgroud)