Bash-有时只创建空输出

Bash-有时只创建空输出,bash,output,Bash,Output,我试图创建一个bash字典脚本,它接受第一个参数并创建以该参数命名的文件,然后脚本接受下一个参数(即同一文件夹中的文件),并将其内容输出到文件(第一个参数)中。它还排序、删除符号等,但主要问题是,有时ouptut文件是空的(我传递的是一个非空文件和一个不存在的文件),在删除和运行脚本几次之后,它有时是空的,有时不是 #!/bin/bash numberoffileargs=$(( $# - 1 )) exitstat=0 counterexit=0 acceptingstdin=0; >

我试图创建一个bash字典脚本,它接受第一个参数并创建以该参数命名的文件,然后脚本接受下一个参数(即同一文件夹中的文件),并将其内容输出到文件(第一个参数)中。它还排序、删除符号等,但主要问题是,有时ouptut文件是空的(我传递的是一个非空文件和一个不存在的文件),在删除和运行脚本几次之后,它有时是空的,有时不是

#!/bin/bash

numberoffileargs=$(( $# - 1 ))
exitstat=0
counterexit=0
acceptingstdin=0;
> "$1";
#check if we have given input files given
if [ "$#" -gt 1 ]; then
    #for cycle going through input files
    for i in "${@:2}"
        do
    #check whether input file is readable
            if [ -r "${i}" ]; then
                cat "${i}" >> "$1"      
    #else redirect to standard output
            else
            exitstat=2
            counterexit=$((counterexit + 1))
            echo "file does not exist" 1>&2
            fi
       done 
else
    echo "stdin code to be done"
    acceptingstdin=1
        #stdin input to output file
#stdin=$(cat)
fi
    #one word for each line, alphabetical sort, alphabet only, remove duplicates
#all lowercase
#sort -u >> "$1"



if [ "$counterexit" -eq "$numberoffileargs" ] && [ "$acceptingstdin" -eq 0 ]; then 
    exitstat=3
fi
cat "$1" | sed -r 's/[^a-zA-Z\-]+/ /g' | tr A-Z a-z | tr ' ' '\n' | sort -u | sed '/^$/d'     > "$1"
echo "$numberoffileargs"
echo "$counterexit"
echo "$exitstat"
exit  $exitstat

这是您的脚本,其中包含一些语法改进。您的问题来自这样一个事实,即字典在您的管道上同时具有输入和输出功能;我添加了一个临时文件来修复它

#!/bin/bash

(($# >= 1)) || { echo "Usage: $0 dictionary file ..." >&2 ; exit 1;}
dict="$1"
shift

echo "Creating $dict ..."
>| "$dict" || { echo "Failed." >&2 ; exit 1;}

numberoffileargs=$#
exitstat=0
counterexit=0
acceptingstdin=0

if (($# > 0)); then
    for i ; do
        #check whether input file is readable
        if [ -r "${i}" ]; then
            cat "${i}" >> "$dict"
        else
            exitstat=2
            let counterexit++
            echo "file does not exist" >&2
        fi
    done 
else
    echo "stdin code to be done"
    acceptingstdin=1
fi

if ((counterexit == numberoffileargs && acceptingstdin == 0)); then 
    exitstat=3
fi

sed -r 's/[^a-zA-Z\-]+/ /g' < "$dict" | tr '[:upper:]' '[:lower:]' | tr ' ' '\n' | 
sort -u | sed '/^$/d' >| tmp$$

mv -f tmp$$ "$dict"

echo "$numberoffileargs"
echo "$counterexit"
echo "$exitstat"
exit  $exitstat
#/bin/bash
(($#>=1))|{echo“用法:$0字典文件…”>&2;退出1;}
dict=“$1”
转移
echo“正在创建$dict…”
>|“$dict”|{echo”失败。“>&2;退出1;}
numberoffileargs=$#
exitstat=0
反向出口=0
AcceptingsDin=0
如果($#>0));然后
为了我;做
#检查输入文件是否可读
如果[-r“${i}”];然后
猫“${i}”>>“$dict”
其他的
exitstat=2
让我们退出++
echo“文件不存在”>&2
fi
完成
其他的
回显“待完成的标准输入代码”
接受TDIN=1
fi
if((counterexit==numberofileargs&&acceptingstdin==0));然后
exitstat=3
fi
sed-r's/[^a-zA-Z\-]+//g'<“$dict”| tr'[:upper:]['[:lower:][]| tr''\n''
排序-u | sed'/^$/d'>| tmp$$
mv-f tmp$$“$dict”
回显“$numberoffileargs”
回显“$counterexit”
回显“$exitstat”
退出$exitstat

管道可能会得到改进。

不知道为什么,但终端说“第19行-目录或文件不存在”,重定向应该创建它,对吗?是的,这是我的猜测。