Bash 回显一些行到终端,一些行到文件

Bash echo some lines to terminal and some lines to file

我有这个代码 bash 代码叫做 xyz_to_mol:

CARBONS=$(grep -ow "C"  | wc -l)
HYDROGENS=$(grep -ow "H"  | wc -l)
OXYGENS=$(grep -ow "O"  | wc -l)

ATYPES=0
ARRAY=($CARBONS $HYDROGENS $OXYGENS)

for i in "${ARRAY[@]}"
do
        if [ $i -gt 0 ]; then
                ((ATYPES+=1))
        fi
done

echo "Choose co0, co1 or co2 basis set"
read BASIS_SET

echo "BASIS"
echo "$BASIS_SET"
echo ""
echo ""
echo "Atomtypes="$ATYPES" Generators=0 Integrals=1.00D-15 Angstrom"
echo "Charge=6.0 Atoms="$CARBONS""
grep "C" 
echo "Charge=1.0 Atoms="$HYDROGENS""
grep "H" 
if [ $OXYGENS -gt 0 ]; then
    echo "Charge=8.0 Atoms="$OXYGENS""
    grep "O" 
fi

除一件事外,这完全符合我的要求:行 "Choose co0, co1 or co2 basis set" 不应写入输出文件,而应作为用户命令行的指令。所以如果我使用这样的脚本:

xyz_to_mol ketone.xyz > ketone.mol

用户应该看到一条说明:"Choose co0, co1 or co2 basis set"。然后他或她应该写 co0co1co2 并且输出文件 ketone.mol 不应包含行 "Choose co0, co1 or co2 basis set"。我该如何实施?

一种替代方法是将文件名作为参数发送,然后写入脚本中的输出文件:

echo "BASIS" > 
echo "$BASIS_SET" >> 
echo "" >> 
echo "" >> 
echo "Atomtypes="$ATYPES" Generators=0 Integrals=1.00D-15 Angstrom" >> 
echo "Charge=6.0 Atoms="$CARBONS"" >> 
grep "C"  >> 
echo "Charge=1.0 Atoms="$HYDROGENS"" >> 
grep "H"  >> 
if [ $OXYGENS -gt 0 ]; then
    echo "Charge=8.0 Atoms="$OXYGENS"" >> 
    grep "O"  >> 
fi

并像这样调用脚本:

xyz_to_mol ketone.xyz ketone.mol

提示通常写入标准错误,而不是标准输出。你可以明确地做到这一点:

echo "Choose co0, co1 or co2 basis set" >&2
read BASIS_SET

或隐含地使用 -p 选项 read:

read -p "Choose co0, co1, or co2 basis set" BASIS_SET