我正在开发一个简单的 BASH 脚本来观看给定的指南针项目。我的脚本如下所示。
我想允许用户选择输入自定义路径或使用默认http_path
值。我不确定如何使用当前脚本将path
变量设置为等于http_path
我的默认 Apache 目录所在的变量。如果有人对学习如何编写 BASH 脚本有更好的建议和/或资源,请分享!
笔记:我已经在这个网站、Google 和 stackoverflow 上搜索过答案。如果您找到可以解决我的问题的人,请告诉我!
#!/bin/bash
# This script will watch a given compass project
http_path=/var/www/
echo -n "Enter in a new path and press. To use the default path $http_path press [ENTER] twice"
while read path; do
if [ -z "${path}" ]; then
clear
# these two lines enable the script to work if I do not
# I do not prompt for default path. It's been tested and works
# echo "That was empty, do it again!"
# echo -n "Enter in a compass project directory and press [ENTER]"
echo "Are you sure you would like to use the default path? Press [ENTER] to continue..."
$path=$http_path
else
echo "Checking now..."
break
fi
done
echo -n "Enter in a project to watch and prss [ENTER]"
while read project; do
if [ -z "${project}" ]; then
clear
echo "That was empty, do it again!"
echo -n "Enter in a compass project name and press [ENTER]"
else
echo "Checking now..."
break
fi
done
echo "Watching" $project "project in directory" $path
echo "To exit project press Command/Control + C"
cd $path
compass watch $project
查看问题解决方案这里
答案1
为 赋值时path
,请省略$
第一个 while 循环中的前导符号。
你只需要做:path=$http_path
.
编辑
我发帖后才意识到你也需要学习 bash 的资源。我个人发现高级 Bash 脚本指南变得有用。如果我没记错的话,它有点过时了,但已经足够了。 Freenode 上也是#bash
一个很棒的地方。他们并不总是最友好的,但他们绝对是知识渊博的。
另外,永远不要害怕在命令行上测试某些东西。这就是您的交互式解释器(如果您完成过任何 ruby 或 python 编程)。
编辑2
实际的问题是,按下 Enter 键后,用户不会退出循环,因此需要将代码更改为:
while read path; do
if [ -z "${path}" ]; then
clear
echo "Are you sure you would like to ..."
read prompt
if [ -n "${prompt}" ] ; then
continue
fi
path=$http_path
break
else
# etc.
fi
done