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?

2 Answers 2

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) 
Sign up to request clarification or add additional context in comments.

4 Comments

How can I iterate through all the parameters I received in printer_script? Can I get their number?
That's another question, but if you have many parameters I'd advise you to use a pipe and read in a cycle.
Grazie Matteo, I found the solution to the question I asked here: stackoverflow.com/questions/255898/…
@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.
1

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

#!/bin/bash
read a b c
echo $c
echo $a
echo $b

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.