Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于newb的基本Bash脚本_Bash_Shell - Fatal编程技术网

用于newb的基本Bash脚本

用于newb的基本Bash脚本,bash,shell,Bash,Shell,这是我的代码:我想在临时目录中创建一个临时变量。我创建了一个名为read series的函数,它读取整数直到ctrl-d,然后将它们附加到.tmp。然后它传递到奇偶数,奇偶数是偶数与赔率之和的乘积之和。然后调用Reduce以输出值。或多或少我是Bash新手,所以请明确回答 #!/bin/bash TMPDIR=${HOME}/tmpdir function readSeries () { while read -p "Enter an Integer: " number ; do

这是我的代码:我想在临时目录中创建一个临时变量。我创建了一个名为read series的函数,它读取整数直到ctrl-d,然后将它们附加到.tmp。然后它传递到奇偶数,奇偶数是偶数与赔率之和的乘积之和。然后调用Reduce以输出值。或多或少我是Bash新手,所以请明确回答

#!/bin/bash

TMPDIR=${HOME}/tmpdir

function readSeries () {
    while read -p "Enter an Integer: " number ; do
        echo $number
    done
    return 0;
} >> $$.tmp

function even-odd () {
    # unsure of how to reference TMPDIR
    while read $TMPDIR ; do
        evenp=$(($1 % 2))
        if [ $evenp -eq 0 ] ; then    # if 0 number is even
            return 0
        else                          # if 1 number is odd
            return 1
        fi
    done
}

function reduce () {
    # function to take sum of odds and product of evens
    # from lab 5 prompt
    even-odd $input
    cat $TMPDIR/$$.tmp | reduce
}

read-series

cat $TMPDIR/$$.tmp | reduce

我想那对你合适

#!/bin/bash

TMPDIR=${HOME}/tmpdir

function readSeries () {
    while read -p "Enter an Integer: " number ; do
        #
        # Using Regular Expression to ensure that value is Integer
        # If value is not integer then we return out from function
        # and won't promt again to enter
        #
        if ! [[ "$number" =~ ^[0-9]+$ ]] ; 
            then return 0;
        fi
        echo $number
    done
    return 0;
} >> $$.tmp

#function evenOdd () {
    # don't need that
#}

function reduce () {
    # function to take sum of odds and product of evens
    sumOfOdds=0;
    productOfEvens=0;

    # 
    # When a shell function is on the receiving end of a pipe, 
    # standard input is read by the first command executed inside the function. 
    # USE `read` to pull that data into function 
    # Syntax :  read variable_you_want_to_name  
    #
    while read data; do
        echo "    line by line data from tmp file "$data;
        rem=$(($data % 2))
        if [ $rem -eq 0 ] ; then    # if 0; number is even
            productOfEvens=$(($productOfEvens * $data));
        else                          # if 1; number is odd
            sumOfOdds=$(($sumOfOdds + $data));
        fi
    done

    echo " Sum of Odds    :  "$sumOfOdds;
    echo " ProductOfEvens :  "$productOfEvens;
}

readSeries

#cat $TMPDIR/$$.tmp
cat $TMPDIR/$$.tmp | reduce

或者,如果你想在这里得到更具体的答案,那么你必须像@Sheller所指出的那样在代码中保持清晰。

如果你想得到清晰的答案,你必须在代码中保持清晰。例如,其中定义了
read series
(位于脚本的第二行到最后一行),在
reduce
函数中调用
reduce
将导致问题。学习理解在代码顶部附近添加
set-x
的输出将对调试自己的代码有很大帮助。祝你好运