Bash 脚本 - 使用空格作为字符串
Bash script - Using spaces as string
我正在尝试使用 bash 脚本构建一个非常简单的 TODO 列表。它应该允许用户添加和删除任务并查看整个列表。
我已经用下面的脚本完成了。但是我有问题允许给定的任务将空格作为字符串。例如,如果我使用命令添加一个任务:./programme_stack.sh add 1 start projet n1
,它只会添加一个使用 "start" 的任务。
我在网上读过一些东西,我知道,我应该用双引号引用变量,但在尝试之后,它不起作用。我一定是在路上丢了什么东西。
这是我的脚本:
#!/bin/bash
TACHES=$HOME/.todo_list
# functions
function remove() {
res_remove=$(sed -n "p" $TACHES)
sed -i "d" $TACHES
}
function list() {
nl $TACHES
}
function add() {
if [ ""$(($(wc -l $TACHES | cut -d " " -f 1) + 1))"" == "" ]
then
echo "- " >> $TACHES
else
sed -i "i - " $TACHES
fi
echo "Task \"\" has been add to the index "
}
function isNumber() {
re='^[0-9]+$'
if ! [[ $@ =~ $re ]] ; then
res_isNumber=true
else
res_isNumber=false
fi
}
# application
case in
list)
list
;;
done)
shift
isNumber $@
if ! [[ "$res_isNumber" = false ]] ; then
echo "done must be followed by an index number"
else
nb_taches=$(wc -l $TACHES | cut -d " " -f 1)
if [ "" -ge 1 ] && [ "" -le $nb_taches ]; then
remove
echo "Well done! Task $i ($res_remove) is completed"
else
echo "this task doesn't exists"
fi
fi
;;
add)
shift
isNumber
if ! [[ "$res_isNumber" = false ]] ; then
echo "add must be followed by an index number"
else
index_max=$(($(wc -l $TACHES | cut -d " " -f 1) + 1))
if [ "" -ge 1 ] && [ "" -le $index_max ]; then
add
else
echo "Idex must be between 1 and $index_max"
fi
fi
;;
*)
echo "./programme_stack.sh (list|add|done) [args]"
;;
esac
你们能看出我错过了什么吗?
非常感谢!!
要使脚本支持嵌入式 spaces,需要进行 2 处更改
1) 接受嵌入式 space - 或者
1A) 在引号 script add nnn "say hello"
中传入任务名称,或者
1B) 将所有输入参数连接成单个字符串。
2) 引用任务名称,防止被拆成单个单词
在代码中,实现1B和2
add)
...
if [ "" -ge 1 ] && [ "" -le $index_max ]; then
num=
shift
# Combine all remaining arguments
todo="$@"
add "$num" "$todo"
...
我正在尝试使用 bash 脚本构建一个非常简单的 TODO 列表。它应该允许用户添加和删除任务并查看整个列表。
我已经用下面的脚本完成了。但是我有问题允许给定的任务将空格作为字符串。例如,如果我使用命令添加一个任务:./programme_stack.sh add 1 start projet n1
,它只会添加一个使用 "start" 的任务。
我在网上读过一些东西,我知道,我应该用双引号引用变量,但在尝试之后,它不起作用。我一定是在路上丢了什么东西。
这是我的脚本:
#!/bin/bash
TACHES=$HOME/.todo_list
# functions
function remove() {
res_remove=$(sed -n "p" $TACHES)
sed -i "d" $TACHES
}
function list() {
nl $TACHES
}
function add() {
if [ ""$(($(wc -l $TACHES | cut -d " " -f 1) + 1))"" == "" ]
then
echo "- " >> $TACHES
else
sed -i "i - " $TACHES
fi
echo "Task \"\" has been add to the index "
}
function isNumber() {
re='^[0-9]+$'
if ! [[ $@ =~ $re ]] ; then
res_isNumber=true
else
res_isNumber=false
fi
}
# application
case in
list)
list
;;
done)
shift
isNumber $@
if ! [[ "$res_isNumber" = false ]] ; then
echo "done must be followed by an index number"
else
nb_taches=$(wc -l $TACHES | cut -d " " -f 1)
if [ "" -ge 1 ] && [ "" -le $nb_taches ]; then
remove
echo "Well done! Task $i ($res_remove) is completed"
else
echo "this task doesn't exists"
fi
fi
;;
add)
shift
isNumber
if ! [[ "$res_isNumber" = false ]] ; then
echo "add must be followed by an index number"
else
index_max=$(($(wc -l $TACHES | cut -d " " -f 1) + 1))
if [ "" -ge 1 ] && [ "" -le $index_max ]; then
add
else
echo "Idex must be between 1 and $index_max"
fi
fi
;;
*)
echo "./programme_stack.sh (list|add|done) [args]"
;;
esac
你们能看出我错过了什么吗? 非常感谢!!
要使脚本支持嵌入式 spaces,需要进行 2 处更改
1) 接受嵌入式 space - 或者
1A) 在引号 script add nnn "say hello"
中传入任务名称,或者
1B) 将所有输入参数连接成单个字符串。
2) 引用任务名称,防止被拆成单个单词
在代码中,实现1B和2
add)
...
if [ "" -ge 1 ] && [ "" -le $index_max ]; then
num=
shift
# Combine all remaining arguments
todo="$@"
add "$num" "$todo"
...