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
$fileNameused later in the script?$fileNameis 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 fiThis is not a one liner, just how it prints here./stats -columns test_fileI 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 -columnsI get the following: Averages: 5 0 0 0 0 Medians: 6 6 6 6 6$fileNamethere 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 thatechooutput 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?