0

I have a list of data that I need to analyze. The first thing I do is check whether the file is indeed the one I want, then I sort the data according to the 4th column, and then I need to manually order the sorted line.

For example, I need to print the 3rd word in the line, and then the first word etc. Here is what I wrote:
mainScript :

#!/bin/bash
for file in `ls ${1}` ; do
    if [[ ! ($file = *.user) ]] ; then
        continue
    fi

    sort -nrk4 $file | source printer_script

done

printer_script :

#!/bin/bash
echo $3
echo $1
echo $2

Why nothing gets printed even though I send the sorted lines by pipeline?

Omar
  • 7,835
  • 14
  • 62
  • 108

2 Answers2

3

Because with the pipe the output of sort goes to the standard input of your script, and in it instead you are looking at the parameters; if you want to grab that output and pass it as parameters, you should do:

./printer_script $(sort -nrk4 $file) 
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • How can I iterate through all the parameters I received in printer_script? Can I get their number? – Omar May 20 '12 at 14:16
  • That's another question, but if you have many parameters I'd advise you to use a pipe and `read` in a cycle. – Matteo Italia May 20 '12 at 14:18
  • Grazie Matteo, I found the solution to the question I asked here: http://stackoverflow.com/questions/255898/how-to-iterate-over-arguments-in-bash-script – Omar May 20 '12 at 14:19
  • @Omar: good, but, again, if you have much data you shouldn't use the script arguments, but the standard input. This because there are some limits for script arguments, while communication via pipes is not subjected to restrictions. – Matteo Italia May 20 '12 at 14:25
1

If you want to read from pipe, printer_script should be:

#!/bin/bash
read a b c
echo $c
echo $a
echo $b
Draco Ater
  • 20,820
  • 8
  • 62
  • 86