0

So I have googled this and thought I found the answers, but it still doesnt work for me.

The program computes the average and median of rows and columns in a file of numbers...

Using the file name works:

./stats -columns test_file

Using cat does not work

cat test_file | ./stats -columns

I am not sure why it doesnt work

#file name was given 
if [[ $# -eq 2 ]]
  then
      fileName=$2
  #file name was not given
  elif [[ $# -eq 1 ]]
  then
      #file name comes from the user
      fileName=/dev/stdin
  #incorrect number of arguments
  else
      echo "Usage: stats {-rows|-cols} [file]" 1>&2
      exit 1
  fi
9
  • 1
    "doesnt work" how exactly? Does it kick your cat? Does it eat your lunch? Does it throw an error? How is $fileName used later in the script? Commented Oct 9, 2015 at 2:01
  • $fileName is only used later to check if the file is a valid file: #check that file is readable if [[ ! -r "$fileName" ]] then echo "Cannot read file" 1>&2 exit 1 fi This is not a one liner, just how it prints here Commented Oct 9, 2015 at 2:10
  • It is not creating the correct input... using: ./stats -columns test_file I get the following result (which is correct): Averages: 5 4 5 5 4 Medians: 6 4 4 7 5 When using: cat test_file | ./stats -columns I get the following: Averages: 5 0 0 0 0 Medians: 6 6 6 6 6 Commented Oct 9, 2015 at 2:13
  • 1
    Not using $fileName there is silly since you have it and it contains what you want but that's a different issue. You have that literal code? Where does that echo output to? How are you actually using the contents of the file with that? How large is your script? Can you just paste it all into the question? Commented Oct 9, 2015 at 2:20
  • 1
    Yeah, that's likely the problem. You can't do that with standard input, not with the shell I don't think. You might be able to duplicate the fd and read from each one once but it is a much better idea to fix your script to stop reading the data twice. Commented Oct 9, 2015 at 2:21

1 Answer 1

1

A very simple program that accepts piped input:

#!/bin/sh

stdin(){
    while IFS= read -r i
    do   printf "%s" "$i"
    done

}

stdin

Test is as follows:

echo "This is piped output" | stdin

To put that into a script / utility similar to the one in the question you might do this:

#!/bin/sh

stdin(){
    while IFS= read -r i
    do   printf "%s" "$i"
    done

}
rowbool=0
colbool=0
for i in $@
do case "$i" in
    -rows) echo "rows set"
           rowbool=1
           shift
    ;;
    -cols) echo "cols set"
           colbool=1
           shift
    ;;
   esac

done

if [[ $# -gt 0 ]]
then
     fileName=$1
fi
if [[ $# -eq 0 ]]
then fileName=$(stdin)
fi

echo "$fileName"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.