该脚本的目的是充当目录导航器。目前,我正在尝试使用select循环中的第四个参数来打印当前目录中的所有目录。我了解我需要使用命令替换,但不了解如何正确实施反引号。
#! /bin/bash
echo"###################################################################"
pwd | ls -l
#problem with bad substitution below inbetween backticks
select choice in quit back jump ${`ls -l | egrep '^d' | awk $9`};
do
case $choice in
"quit")
echo "Quitting program"
exit 0
break
;;
"back")
cd ..
echo "Your have gone back to the previous directory: " `pwd`
pwd
ls -l
;;
"jump")
echo "Enter the directory you want to move into"
read inputDir
if [[ -d $inputdir ]]; then
cd $inputDir
pwd
ls -l
else
echo "Your input is not a directory, Please enter correct Di$
fi
;;
${ls -l | egrep '^d' | awk $9})
esac
done
答案 0 :(得分:1)
您应该真正考虑使用shellcheck来整理您的Shell脚本。
我使用mapfile
根据输出创建一个数组。我还使用find
代替ls
,因为它可以更好地处理非字母数字文件名。
然后我创建一个附加输出的数组。可以采用不同的方法,但这是最简单的方法。有关bash数组here的更多信息。
#! /bin/bash
echo"###############################################################"
pwd # Your script had a |, it doesn't do anything since ls -l, doesn't take
# input from stdin. I seperated them, because that's probably what you want
ls -l
mapfile -t output < <(find . -type d -maxdepth 1 -not -name '.*' | sed -e 's/^\.\///')
choices=(quit back jump "${output[@]}")
select choice in "${choices[@]}"; do
case $choice in
"quit")
echo "Quitting program"
exit 0
break
;;
"back")
cd ..
echo "Your have gone back to the previous directory: $(pwd)"
pwd
ls -l
;;
"jump")
echo "Enter the directory you want to move into"
read -r inputDir
if [[ -d $inputDir ]]; then
cd "$inputDir" || exit
pwd
ls -l
else
echo "Your input is not a directory, Please enter correct Di$"
fi
;;
esac
done