简体   繁体   English

Bash 脚本 - 使用空格作为字符串

[英]Bash script - Using spaces as string

I'm trying to build a really simple TODO list with a bash script.我正在尝试使用 bash 脚本构建一个非常简单的 TODO 列表。 It should allow user to add and remove a task and also see the entire list.它应该允许用户添加和删除任务并查看整个列表。 I've done it with the following script.我已经使用以下脚本完成了它。 But I've issues allowing a given task to take a whitespace as a string.但是我有问题允许给定任务将空格作为字符串。 For instance, if I'm adding a task with the command: ./programme_stack.sh add 1 start projet n1 , it will only add a task with "start".例如,如果我使用以下命令添加任务: ./programme_stack.sh add 1 start projet n1 ,它只会添加带有“start”的任务。 I've read a couple of things online, I know, I should double quote the variables but after trying this, it doesn't work.我已经在网上阅读了一些东西,我知道,我应该对变量进行双引号,但是在尝试了这个之后,它不起作用。 I must be missing something on the road.我一定是在路上遗漏了什么。

Here is my script:这是我的脚本:

#!/bin/bash

TACHES=$HOME/.todo_list
# functions
function remove() {
    res_remove=$(sed -n "$1p" $TACHES)
    sed -i "$1d" $TACHES
}

function list() {
    nl $TACHES
}

function add() {
    if [ ""$(($(wc -l $TACHES | cut -d " " -f 1) + 1))"" == "$1" ]
    then
        echo "- $2" >> $TACHES
    else
        sed -i "$1i - $2" $TACHES
    fi
    echo "Task \"$2\" has been add to the index $1"
}

function isNumber() {
    re='^[0-9]+$'
    if ! [[ $@ =~ $re ]] ; then
        res_isNumber=true
    else
        res_isNumber=false
    fi
}

# application
case $1 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 [ "$1" -ge 1 ] && [ "$1" -le $nb_taches ]; then
                remove $1
                echo "Well done! Task $i ($res_remove) is completed"
            else
                echo "this task doesn't exists"
            fi        
        fi
        ;;
    add)
        shift
        isNumber $1
        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 [ "$1" -ge 1 ] && [ "$1" -le $index_max ]; then
                add $1 $2
            else
                echo "Idex must be between 1 and $index_max"
            fi        
        fi
        ;;
    *)
        echo "./programme_stack.sh (list|add|done) [args]"
        ;;
esac

Can you guys see what I'm missing?你们能看到我错过了什么吗? Many thanks!!非常感谢!!

To get the script to support embedded spaces, 2 changes are needed 1) Accept embedded space - either 1A) Pass in the task name in quote script add nnn "say hello" , OR 1B) Concatenate all the input parameters into single string.要使脚本支持嵌入空间,需要进行 2 处更改 1) 接受嵌入空间 - 要么 1A) 在引用script add nnn "say hello" ,要么 1B) 将所有输入参数连接成单个字符串。 2) Quote the task name to prevent it from being broken into individual words 2)引用任务名称以防止它被分解成单个单词

In the code, implement 1B and 2在代码中,实现1B和2

add)
   ...
 if [ "$1" -ge 1 ] && [ "$1" -le $index_max ]; then
    num=$1
    shift
    # Combine all remaining arguments
    todo="$@"
    add "$num" "$todo"
 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM