【问题标题】:Save intermediate files as variables in bash script [duplicate]将中间文件保存为bash脚本中的变量[重复]
【发布时间】:2020-05-04 17:10:45
【问题描述】:

这是我第一次开始编写可执行的 bash 脚本。 我有四个命令行,我想将它们放入 .sh 文件中,然后在单个 csv 文件上运行。 我想避免一长串命令,例如command1 | command2 | command3 | command4 相反,我需要将中间输出保存在临时变量中,或者使用其他东西代替管道。

我试过了,但它没有给出正确的输出:

filename="$2"
outputname="$3"

if  [[ $1 = "-u" ]]; then
    out= cut -f1,3,7,8  "${filename}" | grep "*_11_0" | tr , ':'
    out2= awk '{print $1,$2}' "${out}"
    echo "${out}"

else
    echo "Error: You have to specify an option"
fi

【问题讨论】:

  • I want to avoid a single long line of commands 为什么?您可以在| 之后添加换行符或添加\ 反斜杠并换行。你对how to save output of a command to a variable这个话题做过研究吗?我推荐bashfaq
  • 感谢您的精彩提示。你能不能用我上面写的脚本做一个保存中间变量的示例命令,那对我来说可能更容易理解:)
  • [ "$1" = -u ] -- 你不需要引用其中没有任何空格、全局字符等的常量字符串;你确实需要引用参数扩展。

标签: bash shell terminal executable


【解决方案1】:

你能不能用我上面写的脚本做一个保存中间变量的示例命令

out=$(cut -f1,3,7,8  "$filename" | grep "*_11_0" | tr , ':')
out2=$(printf "%s\n" "$out" | awk '{print $1,$2}')
# or out2=$(awk '{print $1,$2}' <<<"$out") in bash

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 2011-03-09
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2018-05-23
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多