我正在 Linux 中编写脚本,直到用户按下零按钮,脚本将从 dialog --input
获取文件名和路径(例如,格式如下:/etc/bob
),然后它会列出位置供用户使用 dialog --menu
选择。然后用户在菜单中选择一个项目,脚本搜索文件,如果文件存在,脚本将选择的文件复制到用户的主目录。
截至目前,它抛出 错误:需要 2 个参数,仅找到 1 个
。
首先,用户输入他想要搜索的文件的数量,因此该数量将在 for
循环步骤和 dialog --menu
中设置为项目数 ($nu
)。然后dialog --inputbox
获取用户位置并将其放入loc
,然后放入asd
;之后,在 db
中。最后,dialog --menu
从 db
文件中获取带有 head
和 tail
的行条目。
它适用于一个,有时是 2 个,甚至 4 个项目;但它随后停止了
即使我将 dialog --menu
放在 for
循环之外,放在 while
循环中 - 这也不起作用.
我该如何解决这个问题?
wh=1
while [ $wh -ne 0 ]
do
dialog --inputbox "Please enter numbers of FILE you want to search" 8 60 2>/tmp/num
nu=`cat /tmp/num`
for i in `seq -w 1 $nu` #((i=1;i<=nu;i++));do
dialog --title "Search File" --inputbox "Please enter File name and Location in following Format to search .. [EX. /etc/passwd" 8 60 2> loc #lo-$i
asd=`cat loc`
echo $asd >> db
dialog --menu "test" 15 50 $nu "1" `cat db | head -n 1`i "2" `cat db | head -n 2 | tail -n 1` "3" `cat db | head -n 3 | tail -n 2` 2>p 1>secl
done
done
最佳答案
到目前为止,我想到了这个,一切都很好。但问题是我无法弄清楚如何确定用户键入的内容以及我应该搜索或查找命令搜索的文件。
我可以用 4 或 5 if 检查用户输入,但是如果用户在脚本开头输入 6 7 或 8 路径怎么办?
请帮忙 - 提前致谢
clear
wh=1
while [ $wh -ne 0 ];do
echo "Please enter FileName and Location to search for."
read loc
if [ $loc = 0 ];then
wh=0
fi
echo "$loc" >> loc
done
clear
let lc=`wc -l < loc`-1
wz=`cat loc | head -n $lc`
echo "$wz" > eloc
while true;do
clear
echo "$(tput setaf 2)=====================================================================$(tput sgr 0)"
echo "$(tput setaf 2)========================== list of files ============================$(tput sgr 0)"
echo "$(tput setaf 2)=====================================================================$(tput sgr 0)"
for ((i=1;i<=$lc;i++));do
echo -e " $i : "`cat eloc | head -n $i | tail -n 1` #print each line
eval d$i=`cat eloc | head -n $i | tail -n 1` #put each line in a variable
done
echo "$(tput setaf 2)============================ q to exit ==============================$(tput sgr 0)"
echo "$(tput setaf 2)== if file is existed, will be copy to current user Home directory ==$(tput sgr 0)"
echo -e "\n"
echo -e "Enter your selection :\c"
read selec
#--------------------------------------------
if [[ "$selec" == q ]];then break; fi
#--------------------------------------------
if [[ "$selec" == "" ]];then
echo "$selec: not a valid selection."
continue
fi
#--------------------------------------------
echo -e "Press enter to continue \c"
read input
done
rm -Rf loc eloc
关于linux - 如何从文件中添加对话框菜单项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54389449/