【问题标题】:Handling files with spaces in a bash selection menu在 bash 选择菜单中处理带有空格的文件
【发布时间】:2018-01-29 21:53:41
【问题描述】:

我正在尝试使此脚本处理带有空格的文件。它应该显示并执行目录中文件的内容。当我选择一个包含空格的文件时,bash 失败并显示 bash: foo: no such file or directory, 我缺少什么来正确处理文件

# /etc/skel/.bashrc
#Interactive shell detection
if [[ $- != *i* ]] ; then
# Shell is non-interactive.  Be done now!
return
fi
#kv-bash (easy) var database & setup of info
echo "type 'menu' for a bash menu"
#done####################
#to easily launch crouton enviroments
addentry() {
cd ~/.sslm
echo "Name your menu entry."
read entry
sleep 1s

if [ -e "$entry " ]
then
echo "Error, Menu entry already exists"
addentry
else
echo "what do you want the entry to do?"
read entryexec
echo "$entryexec && menu"> ~/.sslm/"$entry"
echo "done"
cd ~/
fi 
sleep 1s
}
###################
delentry() {
cd ~/.sslm
ls -x
echo "what entry do you want to delete?"
read del
rm "$del"
echo "the work has been done, he is dead"
}
###################
menu() {
clear
cd ~/.sslm
echo "-- simple shell launcher menu v1.o --"

# set the prompt used by select, replacing "#?"
PS3="Use number to select a file or 'exit' to leave: "

# allow the user to choose a file
select filename in *
do
# leave the loop if the user says 'stop'
if [[ "$REPLY" == exit ]]; then 
cd ~/
break
fi

# complain if no file was selected, and loop to ask again
if [[ "$filename" == "" ]]
then
    echo "'$REPLY' is not a valid number"
sleep 1s
continue
fi

# now we can use the selected file, trying to get it to run the shell 
script
. $filename
# it'll ask for another unless we leave the loop
break
done
}
menu

另外,这是在 chromebook 上,所以没有 apt。

【问题讨论】:

  • 也许引用这个会有所帮助:. "$filename".
  • 我会试试的。
  • 它返回 'bash: . foo foo2:找不到命令
  • 没关系,我的格式不正确。

标签: bash menu filenames


【解决方案1】:

在这部分:

  script
. $filename

我只需要更改为 . “$文件名” 谢谢@PesaThe

【讨论】:

    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    相关资源
    最近更新 更多