0

This is my first time started to write an executable bash script. I have four command lines that I want to put in a .sh file then to run on a single csv file. I want to avoid a single long line of commands e.g. command1 | command2 | command3 | command4 instead, I need either save intermediate outputs inside temporary variables or use something instead of pipe.

I tried this but it doesn't give proper output:

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

Apex
  • 1,055
  • 4
  • 22
  • 2
    `I want to avoid a single long line of commands` why? You can put newlines after `|` or add \ backslash and break the lines. Did you make any research on the topic [how to save output of a command to a variable](https://www.google.com/search?q=bash+how+to+save+command+output+to+a+variable)? I recommend [bashfaq](https://mywiki.wooledge.org/BashFAQ/002) – KamilCuk May 04 '20 at 17:14
  • Thank you for the great hints. Could you please make an example command of saving intermediate variables with my written script above, then it might be more understandable for me :) – Apex May 04 '20 at 17:31
  • 1
    `[ "$1" = -u ]` -- you don't need to quote constant strings that don't have any spaces, glob characters, etc. in them; you *do* need to quote parameter expansions. – Charles Duffy May 04 '20 at 17:48

1 Answers1

1

Could you please make an example command of saving intermediate variables with my written script above

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
KamilCuk
  • 120,984
  • 8
  • 59
  • 111