I've a sequence of commands to be used along with lot of pipings, something like this:awk '{ if ($8 ~ isad) {print $2, $5, SE} else {print $2, $5, ANT} }' /var/log/apache2/other_vhosts_access.log | grep -v '127.0.0.1' | tr '[' '\0' | tr [:lower:] [:upper:] | sort -t' ' -s -k3This basically filters the Apache log and prints three cols of info. First col is IP address, second is time, third is a string. The output could be sorted based on any column so, I need to use '-m' with sort for the time field.  Sorting order could also be reversed. I want a string to store the arguments to sort, and let the combined strings get executed. Something like this:$AWK_CMD | $SORT_CMD $SORT_KEY $SORT_REVwhereSORT_CMD=sort -t' 'SORT_KEY=-k$1      # $1 from cmd line argSORT_REV=$2        # Either -r or blankAm able to build such strings; when I echo it, it looks fine. Problem is, how to execute it? I get some errors like:awk '{ if ($8 ~ isad) {print $2, $5, SE} else {print $2, $5, ANT} }' /var/log/apache2/other_vhosts_access.log | grep -v '127.0.0.1' | tr '[' '\0' | tr [:lower:] [:upper:] | sort -t' ' -s -k3Running ...awk: '{awk: ^ invalid char ''' in expressionPlease ignore if 'sort' doesn't sort properly, that can be fixed. I wish to know how I can build the final command string in steps. The ways I tried to execute this command in the script is by:$final_cmd`$final_cmd`Edit: The script that I'm trying to useKEY=$1      # Values {1, 2, 3}REVERSE=$2  # Values {0, 1} SORT_CMD=sort -t' ' -sSORT_KEY=-k$KEYif [[ $KEY -eq 2 ]]then    SORT_KEY=-m -k2fiif [[ $REVERSE -eq 1 ]]then    SORT_REV=-relse    SORT_REV=fifinal_cmd=awk '{ if (\$8 ~ \isad\) {print \$2, \$5, \SE\} else {print \$2, \$5, \ANT\} }' $LOG_FILE '|' grep -v '127.0.0.1' '|' tr '[' '\0' '|' tr [:lower:] [:upper:] '|' $SORT_CMD $SORT_KEY $SORT_REVecho $final_cmdecho Running ...$final_cmd
How to build a long command string?
command line;shell script;awk
The different parts can be put in shell functions :awkfilter() {    awk '{ if ($8 ~ isad) {print $2, $5, SE} else {print $2, $5, ANT} }' $1}toupper() {    tr [:lower:] [:upper:]}dosort() {    sort -t' ' -s -k3}awkfilter /var/log/apache2/other_vhosts_access.log | grep -v '127.0.0.1' | tr '[' '\0' | toupper | dosortthen you could make things optionnal more easily :dosort() {    rev=    if [ $2 == reverse ]    then        rev='-r'    fi    sort -t' ' -s -k$1 $rev}When your command-line is starting to be really long, writing it in a script and breaking it in parts (with functions) is usually really helpfull.