试图在 ksh 中写“<<<”

Trying to write "<<<" in ksh

我在使用以下代码时遇到问题:

IFS=: read c1 c2 c3 c4 rest <<< "$line"

不要误会我的意思,这段代码运行良好,但它似乎并不用于 ksh。我基本上需要编写没有“<<<”的相同代码。网上关于“<<<”的资料不多。如果有人有任何想法,将不胜感激。

编辑:

好的,整个编程部分的代码如下:

m|M)
            #Create Modify Message
            clear
            echo "                                 Modify Record                                 "
            echo -en '\n'
            echo -en '\n'
            while true
            do
                echo "What is the last name of the person you would like to modify:"
                read last_name
                if line=$(grep -i "^${last_name}:" "")
                then
                    oldIFS=$IFS 
                    IFS=:
                    set -- $line
                    IFS=$oldIFS
                    c1=
                    c2=
                    c3=
                    c4=
                    shift; shift; shift; shift 
                    rest="$*"
                    echo -e "Last Name: \nFirst Name: \nState: "
                    while true
                    do
                        echo "What would you like to change the state to?:"
                        read state
                            if echo $state | egrep -q '^[A-Z]{2}$'
                            then
                                echo "State: $state"
                                echo "This is a valid input"
                                break
                            else
                                echo "Not a valid input:"
                            fi
                    done
                    echo -e "Last Name: $c1\nFirst Name: $c2\nState: $state"
                    echo "State value changed"
                    break                       
                else
                    echo "ERROR: $last_name is not in database"
                    echo "Would you like to search again (y/n):"
                    read modify_choice
                    case $modify_choice in [Nn]) break;; esac
                fi  
            done

;;

好的,所以除了

之外一切正常
echo -e "Last Name: $c1\nFirst Name: $c2\nState: $state" 

它只会显示:

姓氏:

名字:

州:

所以我可以看到它没有正确地将它添加到我的 echo 中。

最终编辑

代码:

#Case statement for modifying an entry
        m|M)
            #Create Modify Message
            clear
            echo "                                 Modify Record                                 "
            echo -en '\n'
            echo -en '\n'
            while true
            do
                echo "What is the last name of the person you would like to modify:"
                read last_name
                if line=$(grep -i "^${last_name}:" "")
                then
                    echo "$line" |
                    while IFS=: read c1 c2 c3 c4 rest; do
                        echo -e "Last Name: $c1\nFirst Name: $c2\nState: $c4"
                        last=$c1
                        first=$c2
                    done
                    while true
                    do
                        echo "What would you like to change the state to?:"
                        read state
                            if echo $state | egrep -q '^[A-Z]{2}$'
                            then
                                echo "State: $state"
                                echo "This is a valid input"
                                break
                            else
                                echo "Not a valid input:"
                            fi
                    done
                    echo -e "Last Name: $last\nFirst Name: $first\nState: $state"
                    echo "State value changed"
                    break           
                else
                    echo "ERROR: $last_name is not in database"
                    echo "Would you like to search again (y/n):"
                    read modify_choice
                    case $modify_choice in [Nn]) break;; esac
                fi  
            done
 ;;

A here string 在 Bash

command <<<"string"

基本等同于

echo "string" | command

明显的例外是后者使用管道,这意味着您不能有意义地使用它,特别是 read。一个常见的解决方法是使用 set 内置函数从字符串或外部命令中捕获标记:

oldIFS=$IFS
IFS=:
set -- $line   # no quotes
IFS=$oldIFS
c1=
c2=
c3=
c4=
shift; shift; shift; shift
rest="$*"    # loses spacing / quoting

另一种解决方法是使用只迭代一次的循环;这乍一看似乎很优雅,但如果伪循环的主体很长或很复杂,可能会导致代码相当笨拙。

echo "$line" |
while IFS=: read c1 c2 c3 c4 rest; do
    : stuff which uses those variables
done

这解决了 echo stuff | read variable 将在子进程中 运行 read 并因此立即忘记 variable 的值的问题 - while 循环与 read 发生的过程完全相同,因此它初始化的变量值在循环内可见。

另一个类似的解决方法是将读取和处理委托给一个函数;

process () {
    IFS=: read c1 c2 c3 c4 rest
    : stuff which uses those variables
}

echo "$line" | process

这是笨拙还是优雅很大程度上取决于函数中发生的事情。如果它被整齐地封装起来,它会相当吸引人;但是如果你最终传递了一堆不相关的变量(或者更糟的是,在函数内部修改全局变量!),情况可能恰恰相反。